请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

对于标量tensor的shape表示

在风格转化里定义的loss值,初始化采用了
style_loss = tf.zeros(shape=1, tf.float32)
的方式,这里用1表示了style_loss这一个元素。
在以前vgg net课程里,表示is_training这个布尔输入值,采用了
is_training = tf.placeholder(tf.bool, shape=[])
这样用一个空list[]表示了is_training这一个元素。
我发现把对placeholder的声明换成
is_training = tf.placeholder(tf.bool, shape=1)
在引用时,会出现这样的错误:
ValueError: Shape must be rank 0 but is rank 1 for 'conv1_1/batch_normalization/cond/Switch' (op: 'Switch') with input shapes: [1], [1].
是说这里输入的shape=1被自动换为了shape=[1]吗?这表示的是标量还是一个元素的数组?
请问,这两种对一个元素的shape的赋值方式可以互换吗?为什么?

正在回答

插入代码

1回答

同学你好,shape=1会是长度为1的数组。shape=[]会是标量。

它们不能互换,因为维度不同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> import tensorflow as tf
>>> a = tf.placeholder(tf.bool, shape=[])
>>> b = tf.placeholder(tf.bool, shape=1)
>>> c = tf.zeros(shape=(), dtype=tf.float32)
>>> d = tf.zeros(shape=1, dtype=tf.float32)
>>> a
<tf.Tensor 'Placeholder:0' shape=() dtype=bool>
>>> b
<tf.Tensor 'Placeholder_1:0' shape=(1,) dtype=bool>
>>> c
<tf.Tensor 'zeros:0' shape=() dtype=float32>
>>> d
<tf.Tensor 'zeros_1:0' shape=(1,) dtype=float32>
>>>


0 回复 有任何疑惑可以回复我~
  • 提问者 慕虎9426780 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2019-10-20 09:50:42
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号