最近看去年出的相关教程,发现tf不同版本接口名字不一样,有的参数名字也换了……记录一下。
tfGirls教学:(up主挺好玩的)
video:https://space.bilibili.com/16696495#!/channel/detail?cid=1588
code:https://github.com/CreatCodeBuild/TensorFlow-and-DeepLearning-Tutorial/
在看项目代码,记一下api用法。
tf.summary系列
tf.summary.image
tf.image_summary ---> tf.summary.image
官方文档:
https://www.tensorflow.org/api_docs/python/tf/summary/image
image(
name,
tensor,
max_outputs=3,
collections=None,
family=None
)
参数:max_images -> max_outputs
tf.scalar_summary ---> tf.summary.scalar
tf.histogram_summary ---> tf.summary.histogram
tf.merge_all_summery ---> tf.summary.merge_all()
tf.nn 系列
tf.nn.conv2d
参考:http://blog.csdn.net/mao_xiao_feng/article/details/78004522
文档:https://www.tensorflow.org/api_docs/python/tf/nn/conv2d
tf.nn.conv2d(
input, # 四维tensor,[batch, in_height, in_width, in_channels]
filter, # 四维tensor,[filter_height, filter_width, in_channels, out_channels]
strides, # 1x4维tensor,表示每一维步长。1x4的一维向量
padding, # string,卷积方式
use_cudnn_on_gpu=None, # 是否使用GPU,默认True
data_format='NHWC', # #保存格式 N->batch_size,H->Height,W->width,C->channels
name=None
)
input: 指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]
这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
filter: 相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]
这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
padding: string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同的卷积方式(后面会介绍)
use_cudnn_on_gpu: bool类型,是否使用cudnn加速,默认为true
Returns:
A Tensor. Has the same type as input. A 4-D tensor. The dimension order is determined by the value of data_format, see below for details.
tf.nn.conv3d
文档:https://www.tensorflow.org/api_docs/python/tf/nn/conv3d
tf.nn.conv3d(
input, # 5维tensor,[batch, in_depth, in_height, in_width, in_channels]
filter,# 5维tensor, [filter_depth, filter_height, filter_width, in_channels, out_channels]
strides, #整数list,lenth>=5,
padding,# same as conv2d
data_format='NDHWC',#保存格式 N->batch_size,D->depth,H->Height,W->width,C->channels
name=None
)
input: A Tensor. Must be one of the following types: float32, float64. Shape [batch, in_depth, in_height, in_width, in_channels].
filter: A Tensor. Must have the same type as input. Shape [filter_depth, filter_height, filter_width, in_channels, out_channels]. in_channels must match between input and filter.
strides: A list of ints that has length >= 5. 1-D tensor of length 5. The stride of the sliding window for each dimension of input. Must have strides[0] = strides[4] = 1.
padding: A string from: “SAME”, “VALID”. The type of padding algorithm to use.
data_format: An optional string from: “NDHWC”, “NCDHW”. Defaults to “NDHWC”. The data format of the input and output data. With the default format “NDHWC”, the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels]. Alternatively, the format could be “NCDHW”, the data storage order is: [batch, in_channels, in_depth, in_height, in_width].
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input.
tf.nn.max_pool3d
https://www.tensorflow.org/api_docs/python/tf/nn/max_pool3d
max_pool3d(
input, # 输入,必须是rank5,默认NDHWC
ksize, # 窗口每一维的大小
strides, # 每一维的步长
padding, # SAME orVALID
data_format='NDHWC',
name=None
)
Args:
input: A Tensor. Must be one of the following types: float32. Shape [batch, depth, rows, cols, channels] tensor to pool over.
ksize: A list of ints that has length >= 5. 1-D tensor of length 5. The size of the window for each dimension of the input tensor. Must have ksize[0] = ksize[4] = 1.
strides: A list of ints that has length >= 5. 1-D tensor of length 5. The stride of the sliding window for each dimension of input. Must have strides[0] = strides[4] = 1.
padding: A string from: “SAME”, “VALID”. The type of padding algorithm to use.
data_format: An optional string from: “NDHWC”, “NCDHW”. Defaults to “NDHWC”. The data format of the input and output data. With the default format “NDHWC”, the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels]. Alternatively, the format could be “NCDHW”, the data storage order is: [batch, in_channels, in_depth, in_height, in_width].
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input. The max pooled output tensor.
tf.nn.max_pool
http://blog.csdn.net/mao_xiao_feng/article/details/53453926
tf.nn.max_pool(value, ksize, strides, padding, name=None)
tf.nn.max_pool(
value,#输入,[batch, height, width, channels]
ksize, #池化窗口大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
strides, #和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
padding, #VALID or SAME
name=None
)
返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式
tf系列
tf.reshape
docs: https://www.tensorflow.org/versions/r1.2/api_docs/python/tf/reshape
reshape(
tensor,
shape,
name=None
)
Args:
tensor: A Tensor.
shape: A Tensor. Must be one of the following types: int32, int64. Defines the shape of the output tensor.
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as tensor.
EXAMPLE:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]]
# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7
tf.matmul
矩阵乘法。(并非点乘哦)
matmul(
a, #矩阵a
b, #矩阵b
transpose_a=False, #if True,a在相乘之前转置
transpose_b=False, # if True,b在相乘之前转置
adjoint_a=False, # If True, a is conjugated and transposed before multiplication.在相乘之前共轭转置
adjoint_b=False,
a_is_sparse=False, # if True,a is treated as a sparse matrix
b_is_sparse=False,
name=None
)
(what is 共轭转置:共轭转置)
tf.multiply
点乘。
multiply(
x,
y,
name=None
)
Args:
- x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
- y: A Tensor. Must have the same type as x.
- name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as x.