TensorFlow — 相关 API

TensorFlow相关函数

1.TensorFlow_normal

truncated_normal(
    shape,
    mean=0.0,
    stddev=1.0,
    dtype=tf.float32,
    seed=None,
    name=None
)
功能说明
产生截断正太分布随机数,取值范围为
[mean - 2 * stddev, mean + 2 * stddev]。
参数列表
* 参数名* 必选 类型 * 说明*
shape 1维整型张量或array 输出张量的纬度
mean 0维张量或数值 均值
stddev 0维张量或数值 标准差
dtype dtype 输出类型
seed 数值 随机种子,若seed赋值,每次产生相同随机数
name string 运算名称
示例代码
#!/usr/bin/python
# truncated_normal.py

import tensorflow as tf
initial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1)
print tf.Session().run(initial)

2.tf.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补全
示例代码
#!/usr/bin/python
# constant.py

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)
执行结果
  • a: 2x3 维张量;
  • b: 3x2 维张量;
  • c: 2x2 维张量;
  • e: 2x2x3 维张量;
  • f: 2x3x2 维张量;
  • g: 2x2x2 维张量。

3.tf.placeholder

placeholder(
    dtype,
    shape=None,
    name=None
)
功能说明

是一种占位符,在执行时候需要为其提供数据

参数列表
* 参数名* 必选 类型 * 说明*
dtype dtype 占位符数据类型
shape 1 维整形张量或 array 占位符维度
name string 占位符名称
示例代码
#!/usr/bin/python

import tensorflow as tf
import numpy as np

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

4.tf.nn.bias_add

bias_add(
    value,
    bias,
    data_format=None,
    name=None
)
功能说明

将偏差项 bias 加到 value 上面,可以看做是 tf.add 的一个特例,其中 bias 必须是一维的,并且维度和 value 的最后一维相同,数据类型必须和 value 相同。

参数列表
* 参数名* 必选 类型 * 说明*
value 张量 数据类型为 float, double, int64, int32, uint8, int16, int8, complex64, or complex128
bias 1维张量 维度必须和value最后一维维度相等
data_format string 数据格式,支持’NHWC’和’NCHW’
name string 运算名称
示例代码
#!/usr/bin/python

import tensorflow as tf
import numpy as np

a = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]])
b = tf.constant([2.0,1.0])
c = tf.constant([1.0])
sess = tf.Session()
print sess.run(tf.nn.bias_add(a, b)) 
#print sess.run(tf.nn.bias_add(a,c)) error
print ("##################################")
print sess.run(tf.add(a, b))
print ("##################################")
print sess.run(tf.add(a, c))

5.tf.reduce_mean

reduce_mean(
    input_tensor,
    axis=None,
    keep_dims=False,
    name=None,
    reduction_indices=None
)
功能说明

计算张量 input_tensor 平均值

参数列表
* 参数名* 必选 类型 * 说明*
input_tensor 张量 输入待求平均值的张量
axis None、0、1 None:全局求平均值;0:求每一列平均值;1:求每一行平均值
keep_dims Boolean 保留原来的维度,降为 1
name string 运算名称
reduction_indices None 和axis等价,被弃用
示例代码
#!/usr/bin/python

import tensorflow as tf
import numpy as np

initial = [[1.,1.],[2.,2.]]
x = tf.Variable(initial,dtype=tf.float32)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print sess.run(tf.reduce_mean(x))
    print sess.run(tf.reduce_mean(x,0)) #Column
    print sess.run(tf.reduce_mean(x,1)) #row
执行结果:
1.5
[ 1.5  1.5]
[ 1.  2.]

6.tf.squared_difference

squared_difference(
    x,
    y,
    name=None
)
功能说明

计算张量 x、y 对应元素差平方

参数说明
* 参数名* 必选 类型 * 说明*
x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
y 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
name string 运算名称
示例代码
#!/usr/bin/python

import tensorflow as tf
import numpy as np

initial_x = [[1.,1.],[2.,2.]]
x = tf.Variable(initial_x,dtype=tf.float32)
initial_y = [[3.,3.],[4.,4.]]
y = tf.Variable(initial_y,dtype=tf.float32)
diff = tf.squared_difference(x,y)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print sess.run(diff)
执行结果
[[ 4.  4.]
 [ 4.  4.]]

7.tf.square

square(
    x,
    name=None
)
功能说明

计算张量对应元素平方

参数列表
* 参数名* 必选 类型 * 说明*
x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
name string 运算名称
示例代码
#!/usr/bin/python
import tensorflow as tf
import numpy as np

initial_x = [[1.,1.],[2.,2.]]
x = tf.Variable(initial_x,dtype=tf.float32)
x2 = tf.square(x)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print sess.run(x2)
执行结果
[[ 1.  1.]
 [ 4.  4.]]

TensorFlow相关类

tf.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
)
功能说明

维护图在执行过程中的状态信息,例如神经网络权重值的变化。

参数列表
* 参数名* 类型 * 说明*
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 要是设置了,那么初始的值会是这种维度
示例代码
#!/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)
点这里TensorFlow中文社区

猜你喜欢

转载自blog.csdn.net/qq_35882901/article/details/78128416