我编写了一个程序来将行从一个张量更新到另一个张量。以下是我想要实现的目标的一个非常基本的想法
with tf.Session() as sess:
A = tf.Variable(
[[[0.2, 0.8, 0.1], [0.0, 1.0, 3.0], [0.0, 1.0, 3.0]], [[0.0, 1.0, 1.0], [0.0, 1.0, 0.5], [0.0, 1.0, 3.0]],
[[0.0, 1.0, 0.6], [0.0, 1.0, 0.4], [0.0, 1.0, 3.0]]])
B = tf.Variable(
[[[1.2, 1.8, 1.1], [1.1, 1.1, 3.1], [1.1, 1.1, 3.1]], [[1.1, 1.1, 1.1], [1.1 ,1.1, 1.5], [1.1, 1.1, 3.1]],
[[1.0, 1.0, 1.6], [1.1, 1.1, 1.4], [1.1, 1.1, 3.1]]])
sess.run(tf.global_variables_initializer())
parent1 = 0
parent2 = 1
print("\n A")
print(sess.run(A[parent2][0]))
# A=[0. 1. 1.]
print("\n B")
print(sess.run(B[parent1][0]))
# B=[0.2 0.8 0.1]
print("\n Result")
B = B[0,0].assign(A[1,0])
print(sess.run(B[0]))
# Result
# [[0. 1. 1. ]
# [1.1 1.1 3.1]
# [1.1 1.1 3.1]]
这有效。当我添加第二个运算符时,我的问题出现了:
print("\n Result")
B = B[0,0].assign(A[1,0])
B = B[1,0].assign(A[0,0])
然后我遇到了(底部的完整堆栈跟踪):
ValueError: None values not supported.
似乎由于某种原因,它尝试对张量进行内部转换,并且由于某种原因,传递了 None 类型。我尝试在尝试继续之前评估变量,但这也不起作用这个想法是达到可以使用循环的程度,如下所示:
for i in indexes:
B = B[parent1][i].assign(A[parent2][i])
B = B[parent2][i].assign(A[parent1][i])
完整的堆栈跟踪:
Traceback (most recent call last):
File "tensorflow\python\framework\op_def_library.py", line 527, in _apply_op_helper
preferred_dtype=default_dtype)
File "tensorflow\python\framework\ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "tensorflow\python\framework\constant_op.py", line 305, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "tensorflow\python\framework\constant_op.py", line 246, in constant
allow_broadcast=True)
File "tensorflow\python\framework\constant_op.py", line 284, in _constant_impl
allow_broadcast=allow_broadcast))
File "tensorflow\python\framework\tensor_util.py", line 454, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.
最佳答案
尽管我还没有弄清楚这个错误,但当前解决这个问题的一种方法是使用tf.assign
。
op1 = tf.assign(B[0,0], A[1,0])
op2 = tf.assign(B[1,0], A[0,0])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run([op1, op2])
print(B.eval())
#[[[0. 1. 1. ]
# [1.1 1.1 3.1]
# [1.1 1.1 3.1]]
# [[0.2 0.8 0.1]
# [1.1 1.1 1.5]
# [1.1 1.1 3.1]]
# [[1. 1. 1.6]
# [1.1 1.1 1.4]
# [1.1 1.1 3.1]]]
关于python - 在张量中分配行会抛出 "None values not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161472/