如何使用tf.train.tagt来支持填充可变长度恒定



我正在使用TensorFlow进行练习,而我的代码如下:

a = tf.constant([[1,2,3],
               [1,2,0],
               [1,2,4],
               [1,2],
               [1,3,4,2],
               [1,2,3]])
b = tf.reshape(tf.range(12), [6,2])
num_epochs = 3
batch_size = 2
num_batches = 3
# dequeue ops
a_batched, b_batched = tt.slice_input_producer([a, b], num_epochs = num_epochs, capacity=48, shuffle=False)
aa, bb = tt.batch([a_batched, b_batched], batch_size=batch_size, dynamic_pad=True)
aa3 = tf.reduce_mean(aa)
bb3 = tf.reduce_mean(bb)
loss = tf.squared_difference(aa3, bb3)
sess = tf.Session()
sess.run([tf.global_variables_initializer(),
    tf.local_variables_initializer()])
coord = tf.train.Coordinator()
threads = queue_runner_impl.start_queue_runners(sess=sess)
for i in range(num_batches*num_epochs):
    print sess.run(loss)
    print '='*30
coord.request_stop()
coord.join(threads)

由于变量a为具有可变长度的变量,因此代码运行到错误:

追溯(最近的最新通话(: 文件" small_input_with_no_padding.py",第16行 [1,2,3]]( 文件"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py",第99行,在常数 tensor_util.make_tensor_proto(value,dtype = dtype,shape = shape,verify_shape = verify_shape(( 文件"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py",第376行,在shake_tensor_proto中 _getDensedimensions(values(((( ValueError:参数必须是一个密度张量:[[1,2,3],[1,2,0],[1,2,4],[1,2],[1,3,4,2],[1,2,2],[1,2,3]] - 得到形状[6],但要[6,3]。

我想测试如何具有可变长度的输入tf.train.tain。那么如何解决此错误?非常感谢!

您无法通过可变长度列表创建常数张量,该列表无法转换为密度张量。

a = tf.constant([[1,2,3], [1,2,0], [1,2,4], [1,2], [1,3,4,2], [1,2,3]])

最新更新