【发布时间】:2018-05-21 19:50:10
【问题描述】:
我正在尝试使用 Python API 为 Tensorflow 神经网络的权重和值设置特定值。为此,我将所有权重和偏差放在一个公共集合中,并在每一层的张量上进行适当的重塑和使用 tf.concat。
在我的代码的某个阶段,我检索了所述集合。但是,当我尝试 tf.assign (使用相同形状的 tf.placeholder)到这些连接张量时,以便从单个值向量设置所有权重/偏差,例如坐在 feed_dict 中,然后我得到错误
AttributeError: 'Tensor' object has no attribute 'assign'
我已将我的问题归结为一个最小工作示例 (MWE),如下所示:
import tensorflow as tf
a=tf.Variable(tf.random_uniform([2], dtype=tf.float32))
b=tf.Variable(tf.random_uniform([2], dtype=tf.float32))
c=tf.concat([a,b], axis=0)
d_all=tf.placeholder(shape=[4], dtype=tf.float32)
d_single=tf.placeholder(shape=[2], dtype=tf.float32)
#e_all=tf.assign(c,d_all)
e_single=tf.assign(a,d_single)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
print(a)
print(d_single)
sess.run(e_single, feed_dict={
d_single: [1,2]
})
print(c)
print(d_all)
#sess.run(e_all, feed_dict={
# d_all: [1,2,3,4]
#})
注释掉的行不起作用并失败并出现相同的错误。似乎 tf.concat 产生的张量不再是可变的,因此不具有 assign 属性。我发现了一个相关的问题here,但我的问题并没有按照那里的建议通过 validate_shape 解决。
有什么想法吗?这是期望的行为吗?
【问题讨论】:
标签: python tensorflow concat assign