如何使用tf.train.batch来支持填充变量长度常量?

问题描述 投票:1回答:1

我正在使用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行,在make_tensor_proto _GetDenseDimensions(values)中))ValueError:参数必须是密集张量:[[1,2,3],[1,2,0],[1,2], 4],[1,2],[1,3,4,2],[1,2,3]] - 形状[6],但想要[6,3]。

我想测试tf.train.batch如何填充可变长度的输入。那么我该如何解决这个错误呢?非常感谢!

tensorflow deep-learning
1个回答
0
投票

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

a = tf.constant([[1,2,3], [1,2,0], [1,2,4], [1,2], [1,3,4,2], [1,2,3]])
© www.soinside.com 2019 - 2024. All rights reserved.