TensorFlow函数
- 1、tf.one_hot()
- 2、tf.placeholder(dtype, shape=None, name=None)
- 3、tf.matmul(a,b)
- 4、tf.multiply(a,b)
- 5、tf.math.squared_difference(x,y,name=None)
- 6、tf.reduce_mean()
- 7、tf.get_collection(key,scope=None)
- 8、tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)
- 9、tf.device()
- 10、tf.subtract(x,y,name=None)
这一系列文章呢用来记录我在使用TensorFlow的过程中遇到的函数,嘻嘻!
1、tf.one_hot()
tf.one_hot 函数定义如下:
tf.one_hot(
indices, #输入的tensor,在深度学习中一般是给定的labels,通常是数字列表,属于一维输入,也可以是多维。
depth, #一个标量,用于定义一个 one hot 维度的深度
on_value=None, #定义在 indices[j] = i 时填充输出的值的标量,默认为1
off_value=None, #定义在 indices[j] != i 时填充输出的值的标量,默认为0
axis=None, #要填充的轴,默认为-1,即一个新的最内层轴
dtype=None,
name=None
)
下面举个例子:
import tensorflow as tf
labels = [0, 2, -1, 1]
res = tf.one_hot(indices=labels, depth=4, on_value=1.0, off_value=0.0, axis=-1)
with tf.Session() as sess:
print (sess.run(res))
输出:
[[ 1. 0. 0. 0.]
[ 0. 0. 1. 0.]
[ 0. 0. 0. 0.]
[ 0. 1. 0. 0.]]
其中:
labels:维度为一维;
depth=4 :代表输出长度为4的向量,可以看到结果中每一个向量都是4维;
on_value,off_value:分别设置为1,0,不设置也可以,默认就为1,0,可以看到第三个向量全为0,因为labels[2]=-1:所以该向量不存在值为1的维度;
axis=-1:代表要填充的轴是新的最内层轴,等价于axis=1。
axis=1,表示按行操作;axis=0,表示按列操作。
参考链接:tf.one_hot
2、tf.placeholder(dtype, shape=None, name=None)
此函数用于定义过程,在执行的时候再赋具体的值.
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以多维,比如:[None,3],表示列是3,行不一定
name:名称。
3、tf.matmul(a,b)
将矩阵a乘以矩阵b,生成a * b。
4、tf.multiply(a,b)
两个矩阵中对应元素各自相乘
5、tf.math.squared_difference(x,y,name=None)
返回值:(x-y)的平方
6、tf.reduce_mean()
tf.reduce_mean(input_tensor,axis=None,keep_dims=False,name=None,reduction_indices=None)
用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
input_tensor: 输入的待降维的tensor;
axis: 指定的轴,如果不指定,则计算所有元素的均值;
keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
name: 操作的名称;
reduction_indices:在以前版本中用来指定轴,已弃用;
7、tf.get_collection(key,scope=None)
该函数的作用是从一个collection中取出全部变量,形成列个列表,key参数中输入的是collection的名称。该函数常常与tf.get_variable和tf.add_to_collection配合使用。
8、tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)
函数完成了将value赋值给ref的作用。
其中:ref 必须是tf.Variable创建的tensor,如果ref=tf.constant()会报错!
同时,shape(value)==shape(ref)
9、tf.device()
指定tensorflow运行的GPU或CPU设备
参考链接:tf.device()
10、tf.subtract(x,y,name=None)
返回x-y的元素。
函数参数:
x:一个 Tensor,必须是下列类型之一:half,bfloat16,float32,float64,uint8,int8,uint16,int16,int32,int64,complex64,complex128.
y:一个 Tensor,必须与 x 具有相同的类型.
name:操作的名称(可选).
函数返回值:
tf.subtract函数返回一个Tensor,与 x 具有相同的类型.