shape和get_shape
import tensorflow as tfx1 = tf.placeholder(tf.int32,shape=[2,2])
print(tf.shape(x1))
print(x1.get_shape())
输出结果:
"C:\Program Files\Anaconda3\python.exe" D:/pycharmprogram/tensorflow_learn/shape_learn/shape_get_shape.py
Tensor("Shape:0", shape=(2,), dtype=int32)
(2, 2)Process finished with exit code 0
shape返回值是一个tensor,而get_shape返回的是一个tuple.
reshape和set_shape
import tensorflow as tfx1 = tf.placeholder(tf.int32)
x2=tf.reshape(x1,[2,2])
print(tf.shape(x1))sess = tf.Session()
print(sess.run(tf.shape(x2), feed_dict={x1:[0,1,2,3]}))
print(sess.run(tf.shape(x2), feed_dict={x1: [[0, 1], [2, 3]]}))
reshape生成一个新的shape,x2跟x1是一个不同的tensor
import tensorflow as tfx1 = tf.placeholder(tf.int32)
x1.set_shape([2,2])
print(tf.shape(x1))sess = tf.Session()
print(sess.run(tf.shape(x1), feed_dict={x1:[0,1,2,3]}))
print(sess.run(tf.shape(x1), feed_dict={x1: [[0, 1], [2, 3]]}))
set_shape只是设置placeholder的shape
程序员有偿接单和程序指导加QQ:734564390