我在tensorflow中的权重矩阵的维数方面遇到了问题。
#outputs = tf.reshape(outputs, [batch_size, seq_length, num_classes])
outputs = tf.reshape(outputs, [-1, seq_length, num_classes])
output_dim = outputs.get_shape().as_list()
weights = tf.ones([output_dim[0], seq_length], tf.int32) #TODO: change the dimension
sequence_loss = tf.contrib.seq2seq.sequence_loss(logits=outputs, targets=Y, weights=weights)
所以,我有一个在最后一个纪元发生变化的batch_size,当它到达最后一个纪元时,权重的维度会带来麻烦。
weights = tf.ones([output_dim[0], seq_length], tf.int32)
导致以下错误:
"Cannot convert a partially known TensorShape to a Tensor: %s" % s)
ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 25)
你将如何解决这个问题?我尝试使用tf.ones_like(outputs)
,但似乎不起作用,而且tf.ones
似乎需要一个固定值作为其维度。
发布于 2018-02-01 20:04:34
使用支持动态形状的tf.fill
:
a = tf.placeholder(tf.float32, shape=[None, 25, 10])
b = tf.fill(tf.shape(a)[:-1], 1) # shape=[None, 25]
with tf.Session() as sess:
print(sess.run(b, feed_dict={a: np.zeros([10, 25, 10])}))
# Prints:
# [[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]
https://stackoverflow.com/questions/48562282
复制相似问题