TensorFlow常用函数汇总

终于找到函数使用手册https://tensorflow.google.cn/api_docs/python/
就说嘛没有这个是不合理的,不用自己造了,本文停止更新!

想找一个函数使用手册,找了半天没找到,算了,自己造一个吧!

constant

tf.constant(value,dtype=None,shape=None,name=’Const’)
value可以是一个数,也可以是一个list
如果是一个数,那么这个常亮中所有值的按该数来赋值。

y_hat = tf.constant(36, name='y_hat')     # Define y_hat constant. Set to 36.
X = tf.constant(np.random.randn(3,1), name = "X")

Variable

tf.Variable(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)
loss = tf.Variable((y - y_hat)**2, name='loss')  # Create a variable for the loss

get_variable

tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, custom_getter=None)

name: 变量的名字
shape: 变量的形状,[]表示一个数,[3]表示长为3的向量,[2,3]表示矩阵或者张量(tensor)
dtype: 变量的数据格式,主要有tf.int32, tf.float32, tf.float64等等
initializer: 初始化工具,有tf.zero_initializer, tf.ones_initializer, tf.constant_initializer, tf.random_uniform_initializer, tf.random_normal_initializer, tf.truncated_normal_initializer等

W1 = tf.get_variable("W1", [25, 12288], initializer = tf.contrib.layers.xavier_initializer(seed=1))
    b1 = tf.get_variable("b1", [25, 1], initializer= tf.zeros_initializer())

variable get_variable区别

由于tf.Variable() 每次都在创建新对象,所有reuse=True 和它并没有什么关系。对于get_variable(),来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的

import tensorflow as tf

with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")

print(w1 is w1_p, w2 is w2_p)
#输出
#True  False

placeholder

tf.placeholder(dtype, shape=None, name=None)
参数:
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
name:名称。

x = tf.placeholder(tf.float32, shape=(1024, 1024), name="x")  
Y = tf.placeholder(tf.float32, (None, n_y), name = "Y")  #None意思是不确定有多少行,使用时确定。

ones/zeros

tf.ones(shape,type=tf.float32,name=None)

ones_like/zeros_like

tf.ones_like(tensor,dype=None,name=None)
tf.zeros_like(tensor,dype=None,name=None)

fill

tf.fill(shape,value,name=None)

print(sess.run(tf.fill([2,3],2)))
#[[2 2 2],
# [2 2 2]]

transpose

tf.transpose(a, perm=None, name=’transpose’)
a: A Tensor.
perm: A permutation of the dimensions of a.
name: A name for the operation (optional).

import tensorflow as tf
x = tf.constant([[1, 2 ,3],[4, 5, 6]])
b=tf.transpose(x)

# Equivalently
c=tf.transpose(x ,[1,0])
d=tf.transpose(x, [0,1])

with tf.Session() as sess:
    print (sess.run(x))
    print (sess.run(b))
    print (sess.run(c))
    print (sess.run(d))
[[1 2 3]
 [4 5 6]]
[[1 4]
 [2 5]
 [3 6]]
[[1 4]
 [2 5]
 [3 6]]
[[1 2 3]
 [4 5 6]]

conv2d

tf.nn.conv2d(input, w, strides, padding)
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
input:Tensor, 为输入图像,格式为[batch, height, width, channels], 分别为【输入的批次数量、图像的高(行数)、宽(列数)、通道(彩色为3,灰色为1)】
w:Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,filter的通道数要求与input的in_channels一致
strides: 为滑动窗口尺寸,分别为[1, height, width, 1], 通常 strides[0]=strdes[3]=1,因为一般不会在一个个图像,一个个通道之间滑动
padding:string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同的卷积方式(后面会介绍)
use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true。

padding 为扩展方式,有两种 vaild 和 same
1)VALID是采用丢弃的方式,比如上述的input_width=13,只允许滑动2次,多余的元素全部丢掉
2)SAME的方式,采用的是补全的方式,对于上述的情况,允许滑动3次,但是需要补3个元素,左奇右偶,在左边补一个0,右边补2个0
For the SAME padding, the output height and width are computed as:
out_height = ceil(float(in_height) / float(strides[1]))
out_width = ceil(float(in_width) / float(strides[2]))

For the VALID padding, the output height and width are computed as:
out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))
Returns: A Tensor. Has the same type as input.也就是返回一个和输入同类型的张量,也就是Z

Z1 = tf.nn.conv2d(X, W1, strides = [1,1,1,1], padding = 'SAME')

max_pool

tf.nn.max_pool(value, ksize, strides, padding, name=None)
value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape。
ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
padding:和卷积类似,可以取’VALID’ 或者’SAME’

返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

A1 = tf.nn.max_pool(A1, ksize=[1,8,8,1], strides=[1,8,8,1], padding="SAME")

relu

A1 = tf.nn.relu(Z1)

flatten

F = tf.contrib.layers.flatten(A2)

Returns: A flattened tensor with shape [batch_size, k]即按样本展开,每个样本一行

fully_connected

Z3 = tf.contrib.layers.fully_connected(F, num_outputs=6,activation_fn=None)

softmax_cross_entropy_with_logits

tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)
除去name参数用以指定该操作的name,与方法有关的一共两个参数:
logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[batchsize,num_classes],单样本的话,大小就是num_classes
labels:实际的标签,大小同上
这个函数的返回值并不是一个数,而是一个向量,即各样本的loss,如果要求交叉熵,我们要再做一步tf.reduce_sum操作,就是对向量里面所有元素求和,如果求loss,则要做一步tf.reduce_mean操作,对向量求均值!

reduce_mean/reduce_max

求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求和reduce_sum( input_tensor, axis=None, keep_dims=False,
name=None, reduction_indices=None)
input_tensor:待求值的tensor。
reduction_indices:在哪一维上求解,0是在列上操作,1是在行上操作

# 'x' is [[1, 1, 1]
#         [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
tf.reduce_sum(x, [0, 1]) ==> 6

argmax

equal

cast

boolean_mask(a,b)

tensorflow 里的一个函数,在做目标检测(YOLO)时常常用到。其中b一般是bool型的n维向量,若a.shape=[3,3,3]    b.shape=[3,3]   则  tf.boolean_mask(a,b) 将使a (m维)矩阵仅保留与b中“True”元素同下标的部分。

猜你喜欢

转载自blog.csdn.net/weixin_41043240/article/details/79622876