1
我有一個部分未知的形狀張量和一個面具 - 一張相同形狀的張量填充了1.0
或0.0
- 我想將它轉換爲SparseTensor,只考慮在面具中對應1.0
的項目。所以,我覺得我必須去的東西,如:將張量未知形狀轉換爲張量流中的SparseTensor
import tensorflow as tf
tf.reset_default_graph()
tf.set_random_seed(23)
BATCH = 3
LENGTH = None
dense = tf.placeholder(shape=[BATCH, LENGTH], dtype=tf.float32, name='dense')
mask = tf.placeholder(shape=[BATCH, LENGTH], dtype=tf.float32, name='mask')
indices = tf.where(tf.equal(mask, 0.0))
values = tf.gather_nd(dense, indices)
在這一點上,我不知道如何着手,因爲這兩個,我試圖在不同的錯誤結束了,如下的方式。第一:
sparse = tf.SparseTensor(indices, values, shape=tf.shape(dense))
ValueError: Tensor conversion requested dtype int64 for Tensor with dtype int32: 'Tensor("Shape:0", shape=(2,), dtype=int32)'
第二種:
sparse = tf.SparseTensor(indices, values, shape=dense.get_shape())
ValueError: Cannot convert a partially known TensorShape to a Tensor: (3, ?)
第三種:
sparse = tf.SparseTensor(indices, values, shape=[BATCH, LENGTH])
TypeError: Expected int64, got None of type '_Message' instead.
任何暗示?謝謝!
在你的情況下,不是SparseTensor的形狀只是[BATCH,LENGTH]? –
@ abhisheknaik96試過。添加了第三個錯誤,這是我使用明確的[BATCH,LENGTH]列表得到的。 – petrux
我明白了。我可以問一下LENGTH變量是如何設置的嗎? –