# tf.reshape()

## tf.reshape

tf.reshape(
tensor,
shape,
name=None
)

### 例如：

• 将 1x9矩阵 ==> 3x3矩阵
import tensorflow as tf
import numpy as np

A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

t = tf.reshape(A, [3, 3])
with tf.Session() as sess:
print(sess.run(t))

# 输出
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
• 将 3x2x3矩阵 ==> 1x18矩阵(也就是整个矩阵平铺)
import tensorflow as tf
import numpy as np

A = np.array([[[1, 1, 1],
[2, 2, 2]],
[[3, 3, 3],
[4, 4, 4]],
[[5, 5, 5],
[6, 6, 6]]])

t = tf.reshape(A, [-1])
with tf.Session() as sess:
print(sess.run(t))

# 输出
[1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6]
• 将 3x2x3矩阵 ==> 2x9矩阵([2, -1]表示列项平铺)
import tensorflow as tf
import numpy as np

A = np.array([[[1, 1, 1],
[2, 2, 2]],
[[3, 3, 3],
[4, 4, 4]],
[[5, 5, 5],
[6, 6, 6]]])

t = tf.reshape(A, [2, -1])
with tf.Session() as sess:
print(sess.run(t))

# 输出
[[1 1 1 2 2 2 3 3 3]
[4 4 4 5 5 5 6 6 6]]
• 将 3x2x3矩阵 ==> 2x3x3矩阵([2, -1, 3]表示行项平铺)
import tensorflow as tf
import numpy as np

A = np.array([[[1, 1, 1],
[2, 2, 2]],
[[3, 3, 3],
[4, 4, 4]],
[[5, 5, 5],
[6, 6, 6]]])

t = tf.reshape(A, [2, -1, 3])
with tf.Session() as sess:
print(sess.run(t))

# 输出
[[[1 1 1]
[2 2 2]
[3 3 3]]

[[4 4 4]
[5 5 5]
[6 6 6]]]
• 将1x1矩阵(只能为1x1的矩阵，否则形状不符，Occur ValueError) ==> Scalar(标量)，也就是一个数
import tensorflow as tf
import numpy as np

A = np.array([7])

t = tf.reshape(A, [])
with tf.Session() as sess:
print(sess.run(t))

# 输出
7

### 参数

• tensor：输入的张量
• shape：表示重新设置的张量形状，必须是int32或int64类型
• name：表示这个op名字，在tensorboard中才会用

### 返回值

• 有着重新设置过形状的张量
07-27 7032
05-23 63

08-17 6881
12-03 9210
11-12 11万+
05-11 4554
01-03 8533
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客