Tensorflow 如何使用tf.train.batch支持填充可变长度常量?
我正在使用TensorFlow进行练习,我的代码如下:Tensorflow 如何使用tf.train.batch支持填充可变长度常量?,tensorflow,deep-learning,Tensorflow,Deep Learning,我正在使用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_bat
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的长度可变,因此代码会出现以下错误:
回溯(最近一次呼叫最后一次):
第16行中的文件“无填充的小输入”
[1,2,3]])
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/framework/constant_op.py”,第99行,常量
tensor_util.make_tensor_proto(值,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(值)))
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如何使用可变长度填充输入。那么我如何修复这个错误呢?多谢各位 不能通过不能转换为稠密张量的可变长度列表创建常量张量
a = tf.constant([[1,2,3], [1,2,0], [1,2,4], [1,2], [1,3,4,2], [1,2,3]])
不能通过不能转换为稠密张量的可变长度列表创建常量张量
a = tf.constant([[1,2,3], [1,2,0], [1,2,4], [1,2], [1,3,4,2], [1,2,3]])