Tensorflow define · Dyting's Blog

Tensorflow 概念

Tensor

Tensor是TensorFlow中主要的数据结构,是一个多维数组。例如可以讲一小组图像集表示成一个四维的浮点数数组,这四个维度分别是[batch,height,width,channels].

创建tensor有两种方式,一是直接用tensorflow自带的函数创建,二是用Python的numpy库创建。
第一种如下:

1

2
import tensorflow as tf

tf.zeros([row_dim,col_dim])

第二种方式:

1

2

3
import numpy as np

x_data=np.array([[1,2,3],[4,5,6]])

tf.convert_to_tensor(x_data,dtype=tf.float32)

graph图

一个完整的TF代码主要分成2个部分,定义(构建)和执行。通常在构建阶段创建一个图表示和训练神经网络。
如下图所示:

Tensor在一个或者多个由节点和边组成的图中流动,边代表tensors,节点代表对tensors的操作。
如图,节点a接收了一个1-D tensor,该tensor从节点a流出后,分别流向节点b和c。。
一旦开始一个任务,一个默认的图已经创建好了,可以通过调用tf.get_default_graph)()来访问。在默认的图里面添加操作,如下例子所示:

1

2

3

4

5
import tensorflow as tf

import numpy as np

c=tf.constant(value=1)

print(c.graph)

print(tf.get_default_graph)

另一个用法就是可以创建图覆盖默认图:

1

2

3

4

5
import tensorflow as tf

import numpy as np

with tf.Graph.as_default() as g:

d=tf.constant(value=1)

print(d.graph)

Session 会话

TF阶段分为两个构建和执行,构建是建造图,执行就是启动图,启动图的第一步就是创建一盒Session对象。

1

2

3

4

5

6

7
#启动默认图

sess=tf.Session()

....

....

result=sess.run(d)

#结束关闭

sess.close()

可以用with代码自动关闭进程。

1

2
with tf.Session() as sess:

result=sess.run([product])

Session可以交互使用,避免一个变量持有会话。
用InteractiveSession代替Session,使用Tensor.eval()和Operation.run()方法代替Session.run().

1

2

3

4

5

6

7

8

9

10
import tensorflow as tf

sess=tf.InteractiveSession()

x=tf.Variable([1,2])

a=tf.constant([3,3])

x.initializer.run()#变量必须初始化才能使用。

sub=tf.sub(x,a)

print sub.eval()

在执行run之前操作都不会被真正的执行
Session.run()方法有两个参数,分别是fetches和feed_dict. 传递给fetches的参数既可以是tensor也可以是operation,也可以是list。feed_dict 作用是替换图中某个tensor的值。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
sess.run(fetches=d)

sess.run(d)

a=tf.add(2,5)

b=tf.multiply(a,3)

sess=tf.Session()

sess.run(b)

replace_dict={a:15}

sess.run(b,feed_dict=replace_dict)#结果是45,直接提供a=15,不经过2+5

#feed_dict用来设置graph的输入值

input1=tf.placeholder(tf.float32)

input2=tf.placeholder(tf.float32)

output=tf.mul(input1,input2)

with tf.Session() as sess:

print sess.run([output],feed_dict={input1:[7.],input2:[2.])

placeholder

上面提到placeholder,input1和input2不是 tensor而是placeholder,没有具体的值,在后面的使用和一般的tensor一样,不过在运行的时候,需要用feed_dict 把具体的值提供给placeholder。

Variable 变量

可以改变自身状态的值,但是必须要在Session中初始化才能显示。

1

2

3

4

5

6

7

8

9

10
my_var=tf.Variable(1)

my_var_times_two=my_var.assign(my_var*2)#赋值

#初始化操作

init=tf.global_variable_initializer()

sess=tf.Session()

#初始化变量

sess.run(init)

print sess.run(my_var_times_two)

TensorFlow-进阶

保存和加载模型

保存

利用Saver类实现,它处理图中数据的保存和恢复,我们需要做的就是告诉Saver类我们需要保存哪个图和哪些变量。默认情况下,Sever处理默认图中所有变量,但是,也可以创建更多的图来保存任何想要的子图。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
import tensorflow as tf

v1=tf.Variable(1,name=‘v1‘)

v2=tf.Variable(2,name=‘v2‘)

a=tf.add(v1,v2)

all_saver=tf.train.Saver()#保存所有的变量

v2_saver=tf.tf.train.Saver("v2":v2)#保存想要保存的变量

with tf.Sessin() as sess:

#初始化所有变量

sess.run(tf.global_variables_initializer())

#保存变量

all_saver.save(sess,‘data.chpk‘)

v2_sever.save(sess,‘data-v2.chkp‘)

提取

1

2

3

4

5

6

7

8

9

10

11

12

13

14
# Create some variables.

v1 = tf.Variable(..., name="v1")

v2 = tf.Variable(..., name="v2")

...

# Add ops to save and restore all the variables.

saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and

# do some work with the model.

with tf.Session() as sess:

# Restore variables from disk.

saver.restore(sess, "/tmp/model.ckpt")

print "Model restored."

# Do some work with the model

checkpoint文档就是用来保存变量的。

原文链接 大专栏  https://www.dazhuanlan.com/2019/08/24/5d6119336bc29/

Tensorflow define · Dyting's Blog

原文地址:https://www.cnblogs.com/chinatrump/p/11415208.html

时间: 2024-08-30 10:48:12

Tensorflow define · Dyting's Blog的相关文章

Introduction to TensorFlow

Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by researchers at Google as a single infrastructure for machine learning in both production and research. Later, an implementation of it was open sourced unde

tensorflow 入门

1.  tensorflow 官方文档中文版(下载) 2.  tensorflow mac安装参考 http://www.tuicool.com/articles/Fni2Yr 3. 源码例子目录 lib/python2.7/site-packages/tensorflow/models/image/minst 4. 简单语法 hello import tensorflow as tf // import tensorflow hello = tf.constant('Hello, Tensor

Ubuntu16.04基于Anaconda(py3.6)安装TensorFlow(CPU)的方法

安装tensorflow(cpu版) 对anaconda命令的熟悉,可以参考http://www.jianshu.com/p/d2e15200ee9b 官方的建议是即时你有gpu,但也可以先装一个cpu版,创建环境的命令为: conda create -n tensorflow python=3.6 (一定要指定python版本,我一开始没有写python=3.6,后面各种失败) 先下载安装包,下载路径为:https://storage.googleapis.com/tensorflow/lin

windows下搭建tensorflow的环境

这年头,不会点人工智能和神经网络,都不好意思跟人打招呼了.之前搞了一下sklearn,今天觉得应该要了解一下google这个传说中的人工智能开源神器. 最近终于有时间了,凡事从hello world开始,先从搭环境开始吧. 1.python环境 安装Python:https://www.python.org/ 安装各种库,依赖,因为已经装过太多,我已经忘记需要装什么了.但是有时候会出现装不上的情况,那么这个时候轮子就派上用场了. 各种轮子下载地址:http://www.lfd.uci.edu/~

TensorFlow学习路径【转】

作者:黄璞链接:https://www.zhihu.com/question/41667903/answer/109611087来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 前言:其实TensorFlow本身仅仅是一个分布式的高性能计算框架,想要用TF做深度学习,仅仅学习这个框架本身是没有太大意义的.因此应该将TF看作技术路线中的一个核心点,去掌握整个开发所需要的必要技术,知识.尤其是深度学习的基本原理,这对日后搭建模型,模型调参以至提出新的模型都是极其有用的.

Tensorflow快速入门2--实现手写数字识别

Tensorflow快速入门2–实现手写数字识别 环境: 虚拟机ubuntun16.0.4 Tensorflow(仅使用cpu版) Tensorflow安装见: http://blog.csdn.net/yhhyhhyhhyhh/article/details/54429034 或者: http://www.tensorfly.cn/tfdoc/get_started/os_setup.html 本文将利用Tensorflow以softmax回归和卷积神经网络两种模型简单测试MNIST数据集,快

typedef与define基本使用

参考: typedef用法 http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html#define用法:http://blog.csdn.net/benny5609/article/details/2314541 ====typedef===== 使用typedef目的一般有两个:一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明.eg:typedef int size;typedef unsigne

python tensorflow 安装

我是先下载tensorflow-1.5.0rc1-cp36-cp36m-win32.whl,再执行命令行安装的 下载地址:https://pypi.python.org/pypi/tensorflow/1.5.0rc1 pip install tensorflow-1.5.0rc1-cp36-cp36m-win32.whl pip安装报错:is not a supported wheel on this platform https://www.cnblogs.com/nice-forever/

TensorFlow实战-TensorFlow第一步-第3章

3 TensorFlow第一步 39 3.1 TensorFlow的编译及安装 39 install ancondalink CPU versionconda install tensorflow GPU versionconda install tensorflow-gpu 3.2 TensorFlow实现SoftmaxRegression识别手写数字 46 导入数据 MNIST是一个非常简单的机器视觉数据集,由几万张28x28像素的手写数字组成,这些图片只包含灰度信息. 123456 fro