Reprinted: https: //blog.csdn.net/jasonzzj/article/details/60811035
TensorFlow, you want to increase the dimensions of a dimension, you can use the tf.expand_dims(input, dim, name=None)
function. Of course, we used tf.reshape (input, shape = [] ) can also achieve the same effect, but sometimes during the construction of the FIG, placeholder feed is not a specific value, then the packet will be following error: TypeError: Expected binary or unicode string, got 1
in this under case, we can consider using expand_dims to add a dimension 1. For example, conditions encountered in the code myself, after a two-dimensional image dimensions down to do a specific action, to restore the four-dimensional [batch, height, width, channels ], before and after each increase of one-dimensional. If reshape, given the reasons given above
one_img2 = tf.reshape(one_img, shape=[1, one_img.get_shape()[0].value, one_img.get_shape()[1].value, 1])
Can be achieved using the following method:
= tf.expand_dims one_img (one_img, 0) one_img = tf.expand_dims (one_img, -1) indicates the last dimension -1 #
In the end, we are given an official explanation and examples
# 't' is a tensor of shape [2] shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] # 't2' is a tensor of shape [2, 3, 5] shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
Args:
input: A Tensor.
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.
Reprinted: https: //blog.csdn.net/jasonzzj/article/details/60811035
TensorFlow, you want to increase the dimensions of a dimension, you can use the tf.expand_dims(input, dim, name=None)
function. Of course, we used tf.reshape (input, shape = [] ) can also achieve the same effect, but sometimes during the construction of the FIG, placeholder feed is not a specific value, then the packet will be following error: TypeError: Expected binary or unicode string, got 1
in this under case, we can consider using expand_dims to add a dimension 1. For example, conditions encountered in the code myself, after a two-dimensional image dimensions down to do a specific action, to restore the four-dimensional [batch, height, width, channels ], before and after each increase of one-dimensional. If reshape, given the reasons given above
one_img2 = tf.reshape(one_img, shape=[1, one_img.get_shape()[0].value, one_img.get_shape()[1].value, 1])
Can be achieved using the following method:
= tf.expand_dims one_img (one_img, 0) one_img = tf.expand_dims (one_img, -1) indicates the last dimension -1 #
In the end, we are given an official explanation and examples
# 't' is a tensor of shape [2] shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] # 't2' is a tensor of shape [2, 3, 5] shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
Args:
input: A Tensor.
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.