我是Tensoflow和python的初学者。现在,我有两个函数 get_lines()和 generate_rays()打算生成张量。我用numpy编写了这些函数,并且效果很好,但是当我将其转移到Tensorflow时,即将horizon_lines更改为Tensor并将np.vstack()更改为tf.stack()会引发错误。
代码:
def get_lines(lines, angle):
res = tf.round(lines / tf.tan(angle))
return lines, res
def generate_rays(horizon_lines,
angles={0: np.array([1, 2, 3, 4])}):
temp = [[get_lines(horizon_lines, a)] for a in angles[0]]
rays_left = tf.stack(temp, axis = 0)
其中 horizon_lines 是张量。
但是它显示:
File "/home/zchen/PycharmProjects/test/tools/../lib/layer_utils/generate_rays.py", line 64, in generate_rays
rays_left = tf.stack(pp, axis = 0)
File "/home/zchen/.conda/envs/py3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "/home/zchen/.conda/envs/py3/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 998, in stack
value_shape = ops.convert_to_tensor(values[0], name=name)._shape_tuple() # pylint: disable=protected-access
TypeError: 'generator' object is not subscriptable
有人知道如何解决吗?