基础知识reshape、set_shape区别

  1. dynamic shape和static shape区别:
    TensorFlow在构建图的时候,tensor的shape被称为static(inferred);而在实际运行中,常常出现图中tensor的具体维数不确定二用placeholder代替的情况,因此static shape未必是已知的,tensor在训练过程中的实际维数被称为dynamic shape,而dynamic shape是一定的
import tensorflow as tf
x1 = tf.placeholder(tf.int32)
print(x1.get_shape())
 
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]]}))

unknown 
[4] 
[2,2]

2、get_shape()方法和tf.shape的区别,get_shape()是tensor的方法,返回一个tuple,而tf.shape()则是返回一个tensor,
3、set_shape()和reshape()的区别,前者用于更新某个tensor的shape,而后者往往用于动态创建一个新的tensor

import tensorflow as tf
x1 = tf.placeholder(tf.int32)
x1.set_shape([2,2])
print(x1.get_shape())
 
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]]}))

(2,2) 
[2,2]

这表示图中最开始没有shape的x1在使用set_shape后,它的图中的信息已经改变

import tensorflow as tf
x1 = tf.placeholder(tf.int32)
x2 = tf.reshape(x1, [2,2])
print(x1.get_shape())
 
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]]}))

你可能感兴趣的:(深度学习第一步)