expected_shape 变量在tensorflow中没有任何作用

expected_shape variable does not have any effect in tensorflow

在下面的代码段中,即使初始值和 expected_shape 不匹配,代码也会运行并给出输出 1.0。这是代码。

import tensorflow as tf
import numpy as np

X = tf.placeholder(dtype=tf.float32)
y = tf.placeholder(dtype=tf.float32)
W = tf.Variable(0.0, expected_shape=(3,1))
b = tf.Variable(1.0)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(tf.add(W,b)))

不是应该报错吗?

截至目前,建议使用tf.get_variable并尽可能避免使用tf.Variable。

现在问你为什么 expected_shape 没有任何效果,如果你查看 source code,它会被提及,因为它已被弃用并被忽略。如果进一步查看 _init_from_args 的函数,expected_shape 的参数将被完全忽略,并且该值不会用于进一步处理。