文章目录
tf.reshape(tensor, shape, name=None)
Reshapes a tensor
.
Given tensor, this operation returns a tensor
that has the same values as tensor
with shape shape
.
The number of elements implied by shape
must be the same as the number of elements in tensor
.
Args:
tensor
: A Tensor.shape
: A Tensor of type int32. Defines the shape of the output tensor.name
: A name for the operation (optional).
Returns:
A Tensor. Has the same type as tensor
.
For 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]
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]
tf.nn
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
Computes a 2-D convolution given 4-D input
and filter
tensors.
Given an input tensor of shape [batch, in_height, in_width, in_channels]
and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels]
, this op performs the following:
- Flattens the filter to a 2-D matrix with shape
[filter_height * filter_width * in_channels, output_channels]
. - Extracts image patches from the the input tensor to form a virtual tensor of shape
[batch, out_height, out_width, filter_height * filter_width * in_channels]
. - For each patch, right-multiplies the filter matrix and the image patch vector.
In detail,
output[b, i, j, k] =
sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *
filter[di, dj, q, k]
Must have strides[0] = strides[3] = 1. For the most common case of the same horizontal and vertices strides, strides = [1, stride, stride, 1].
Args:
input
: A Tensor. Must be one of the following types: float32, float64.filter
: A Tensor. Must have the same type as input.strides
: A list of ints. 1-D of length 4. The stride of the sliding window for each dimension of input.(注:一维长度为4的数组,但实际上只有H,W对应位置才有效,其它两个位置填1)padding
: A string from: “SAME”, “VALID”. The type of padding algorithm to use.use_cudnn_on_gpu
: An optional bool. Defaults to True.name
: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input.
tf.nn.relu(features, name=None)
Computes rectified linear: max(features, 0)
.
Args:
features
: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8.name
: A name for the operation (optional).
Returns:
A Tensor. Has the same type as features.
tf.nn.max_pool(value, ksize, strides, padding, name=None)
Performs the max pooling on the input.
Args:
value
: A 4-D Tensor with shape[batch, height, width, channels]
and type float32, float64, qint8, quint8, qint32.ksize
: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.strides
: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.padding
: A string, either'VALID'
or'SAME'
. The padding algorithm.name
: Optional name for the operation.
Returns:
A Tensor with the same type as value
. The max pooled output tensor.
tf.nn,tf.layers, tf.contrib模块
下面是对三个模块的简述:
tf.nn :提供神经网络相关操作的支持,包括卷积操作(conv)、池化操作(pooling)、归一化、loss、分类操作、embedding、RNN、Evaluation。
tf.layers:主要提供的高层的神经网络,主要和卷积相关的,tf.nn会更底层一些。
tf.contrib:tf.contrib.layers提供够将计算图中的 网络层、正则化、摘要操作、是构建计算图的高级操作,但是tf.contrib包含不稳定和实验代码,有可能以后API会改变。