我的代码:
def f(x):
try:
import tensorflow as tf
# x is (None, 10, 2)
idx = K.cast(x*15.5+15.5, "int32")
z = tf.sparse_to_dense(idx, 32, 1.0, 0.0, name='sparse_tensor')
print('z.shape={0}'.format(z.shape))
except Exception as e:
print(e)
return x[:, :, 0:2]
drop_out = Lambda(lambda x: f(x),
output_shape=drop_output_shape, name='projection')(reshape_out)
x是 (None, 10, 2)
的张量,其中有10个索引/坐标 . 试图生成一个 (None, 32, 32)
张量 z
. 我收到以下错误:
Shape must be rank 1 but is rank 0 for 'projection_14/sparse_tensor' (op: 'SparseToDense') with input shapes: [?,10,2], [], [], [].
怎么解决?谢谢
1 回答
您看到的具体错误是试图说你的
output_shape
应该是一维张量,就像(32,)
,而不是你在那里的0-D Tensor,32
. 但我担心这个简单的改变不会解决你的问题 .有一点我不明白,当你说你只有10个指数时,为什么你的
x
是一个三维张量 . 从技术上讲,sparse_indices can be a 2-D tensor at most . 我对tf.sparse_to_dense
的理解是它与制作稀疏张量非常相似 . 所以(10, 2)
中的数字2
已经确定输出张量将是2-D .None
,与变体样本大小一样,应该以不同方式处理 .遵循这个逻辑,你可能会发现另一个问题是
output_shape
应该是(32, 32)
而不是(32,)
作为上面提到的简单修复 . 元组的长度应与sparse_indices
的形状(具体的最后一个轴)匹配 .考虑到所有这些,我认为只有MVCE模仿你的例子的张量流可能是:
只是要指出:The tf.sparse_to_dense "FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Create a tf.sparse.SparseTensor and use tf.sparse.to_dense instead."