“TensorFlow:概念和语法”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“ == 常数 constant == <nowiki>constant( value, dtype=None, shape=None, name='Const', verify_shape=False )</nowiki> === 功能说明 ===…”)
(没有差异)

2018年8月5日 (日) 07:37的版本

常数 constant

constant(
    value,
    dtype=None,
    shape=None,
    name='Const',
    verify_shape=False
)


功能说明

根据 value 的值生成一个 shape 维度的常量张量


参数列表

参数名 	必选 	类型 	说明
value 	是 	常量数值或者 list 	输出张量的值
dtype 	否 	dtype 	输出张量元素类型
shape 	否 	1 维整形张量或 array 	输出张量的维度
name 	否 	string 	张量名称
verify_shape 	否 	Boolean 	检测 shape 是否和 value 的 shape 一致,若为 Fasle,不一致时,会用最后一个元素将 shape 补全


示例代码

现在您可以在 /home/ubuntu 目录下创建源文件 constant.py,内容可参考:

示例代码:/home/ubuntu/constant.py

#!/usr/bin/python

import tensorflow as tf
import numpy as np
a = tf.constant([1,2,3,4,5,6],shape=[2,3])
b = tf.constant(-1,shape=[3,2])
c = tf.matmul(a,b)

e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3])
f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2])
g = tf.matmul(e,f)
with tf.Session() as sess:
    print (sess.run(a))
    print ("##################################")
    print (sess.run(b))
    print ("##################################")
    print (sess.run(c))
    print ("##################################")
    print (sess.run(e))
    print ("##################################")
    print (sess.run(f))
    print ("##################################")
    print (sess.run(g))

然后执行:

python /home/ubuntu/constant.py

执行结果:

a: 2x3 维张量;
b: 3x2 维张量;
c: 2x2 维张量;
e: 2x2x3 维张量;
f: 2x3x2 维张量;
g: 2x2x2 维张量。

占位符placeholder

使用TensorFlow之前,首先导入它:

import tensorflow as tf


placeholder(
    dtype,
    shape=None,
    name=None
)


功能说明

我们通过操作符号变量来描述这些可交互的操作单元,可以用下面的方式创建一个:

x = tf.placeholder("float", [None, 784])

x不是一个特定的值,而是一个占位符placeholder,我们在TensorFlow运行计算时输入这个值。我们用2维的浮点数张量来表示待处理的图像,这个张量的形状是[None,784 ],这里的None表示此张量的第一个维度可以是任何长度的,None后面的784表示每一张图展平成784维的向量。


参数列表

参数名 	必选 	类型 	说明
dtype 	是 	dtype 	占位符数据类型
shape 	否 	1 维整形张量或 array 	占位符维度
name 	否 	string 	占位符名称


示例代码

现在您可以在 /home/ubuntu 目录下创建源文件 placeholder.py,内容可参考:

示例代码:/home/ubuntu/placeholder.py

#!/usr/bin/python

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32,[None,3])
y = tf.matmul(x,x)
with tf.Session() as sess:
    rand_array = np.random.rand(3,3)
    print(sess.run(y,feed_dict={x:rand_array}))

然后执行:

python /home/ubuntu/placeholder.py

执行结果:

输出一个 3x3 的张量


变量variable

__init__(
    initial_value=None,
    trainable=True,
    collections=None,
    validate_shape=True,
    caching_device=None,
    name=None,
    variable_def=None,
    dtype=None,
    expected_shape=None,
    import_scope=None
)


功能说明

我们的模型也需要权重值和偏置量,在TensorFlow里面用Variable来表示它们 。 一个Variable代表一个可修改的张量,存在在TensorFlow的用于描述交互性操作的图中。它们可以用于计算输入值,也可以在计算中被修改。对于各种机器学习应用,一般都会有模型参数,可以用Variable表示。

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

我们赋予tf.Variable不同的初值来创建不同的Variable:


参数列表

参数名 	类型 	说明
initial_value 	张量 	Variable 类的初始值,这个变量必须指定 shape 信息,否则后面 validate_shape 需设为 False
trainable 	Boolean 	是否把变量添加到 collection GraphKeys.TRAINABLE_VARIABLES 中(collection 是一种全局存储,不受变量名生存空间影响,一处保存,到处可取)
collections 	Graph collections 	全局存储,默认是 GraphKeys.GLOBAL_VARIABLES
validate_shape 	Boolean 	是否允许被未知维度的 initial_value 初始化
caching_device 	string 	指明哪个 device 用来缓存变量
name 	string 	变量名
dtype 	dtype 	如果被设置,初始化的值就会按照这个类型初始化
expected_shape 	TensorShape 	要是设置了,那么初始的值会是这种维度


示例代码

现在您可以在 /home/ubuntu 目录下创建源文件 Variable.py,内容可参考:

示例代码:/home/ubuntu/Variable.py

#!/usr/bin/python

import tensorflow as tf
initial = tf.truncated_normal(shape=[10,10],mean=0,stddev=1)
W=tf.Variable(initial)
list = [[1.,1.],[2.,2.]]
X = tf.Variable(list,dtype=tf.float32)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print ("##################(1)################")
    print (sess.run(W))
    print ("##################(2)################")
    print (sess.run(W[:2,:2]))
    op = W[:2,:2].assign(22.*tf.ones((2,2)))
    print ("###################(3)###############")
    print (sess.run(op))
    print ("###################(4)###############")
    print (W.eval(sess)) #computes and returns the value of this variable
    print ("####################(5)##############")
    print (W.eval())  #Usage with the default session
    print ("#####################(6)#############")
    print (W.dtype)
    print (sess.run(W.initial_value))
    print (sess.run(W.op))
    print (W.shape)
    print ("###################(7)###############")
    print (sess.run(X))

然后执行:

python /home/ubuntu/Variable.py