Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 为什么'tf.constant_initializer'不接受常数张量?_Python 2.7_Tensorflow - Fatal编程技术网

Python 2.7 为什么'tf.constant_initializer'不接受常数张量?

Python 2.7 为什么'tf.constant_initializer'不接受常数张量?,python-2.7,tensorflow,Python 2.7,Tensorflow,对我来说,这似乎很愚蠢。能够用常量张量初始化变量是很有意义的: tf.get_variable('some_var', shape = [4,3], initializer=tf.constant_initializer(tf.constant([[0,0,0], [0,0,1],[0,1,0],[1,0,0]]))) 使用占位符和feed_dict是将张量变量初始化为自定义值的唯一方法吗?这就迫使人们在不同的位置进行声明和数据初始化,这是一个麻烦的问题。函数可能不接受a作为参数,但接受tf.

对我来说,这似乎很愚蠢。能够用常量张量初始化变量是很有意义的:

tf.get_variable('some_var', shape = [4,3], initializer=tf.constant_initializer(tf.constant([[0,0,0], [0,0,1],[0,1,0],[1,0,0]])))
使用占位符和
feed_dict
是将张量变量初始化为自定义值的唯一方法吗?这就迫使人们在不同的位置进行声明和数据初始化,这是一个麻烦的问题。函数可能不接受a作为参数,但接受tf.Tensor作为其
初始值设定项
参数。这意味着您可以编写:

v = tf.get_variable('some_var', initializer=tf.constant([[0, 0, 0],
                                                         [0, 0, 1],
                                                         [0, 1, 0],
                                                         [1, 0, 0]]))
…这需要更少的字符

之所以
tf.constant\u initializer()
不采用任意张量,是因为它被设计为使用每个元素的相同常量值初始化许多不同形状的变量。例如,以下语句:

v = tf.get_variable('some_var', shape=[15, 37], initializer=tf.constant_initializer(
    tf.constant([[0, 0, 0],
                 [0, 0, 1],
                 [0, 1, 0],
                 [1, 0, 0]])))
…没什么意义。可以说,我们可以让
tf.constant\u initializer()
接受标量
tf.Tensor
,然后它的语义与类似,但我们还没有任何要求。尽管如此,如果有用的话,请随时提出建议

删除“tf.constant”,如下所示,在tf1.13中工作

tf.get_variable('some_var', shape = [4,3], initializer=tf.constant_initializer([[0,0,0], [0,0,1],[0,1,0],[1,0,0]]))

常量_初始值设定项接受Python列表和numpy数组,它们刚刚更新以反映这一点