我正试图用TensorFlow编写一个TensorFlow文档的实现,但遇到了一些麻烦。在我的池层中,我必须把所有的东西连接在一起。这是我使用的代码:
pooled_outputs = []
for i, filter_size in enumerate(filter_sizes):
with tf.name_scope("conv-maxpool-%s" % filter_size):
# Conv layer
filter_shape = [filter_size, embedding_size, 1, num_filters]
# W is the filter matrix
W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
conv = tf.nn.conv2d(
self.embedded_chars_expanded,
W,
strides=[1, 1, 1, 1],
padding="VALID",
name="conv"
)
# Apply nonlinearity
h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
# Max-pooling layer over the outputs
pooled = tf.nn.max_pool(
h,
ksize=[1, sequence_lengths[i] - filter_size + 1, 1, 1],
strides=[1, 1, 1, 1],
padding="VALID",
name="pool"
)
pooled_outputs.append(pooled)
# Combine all of the pooled features
num_filters_total = num_filters * len(filter_sizes)
print(pooled_outputs)
pooled_outputs = [tf.reshape(out, ["?", 94, 1, self.max_length]) for out in pooled_outputs] # The problem line
self.h_pool = tf.concat(3, pooled_outputs)当我运行这段代码时,它会为pooled_outputs打印这段代码
[<tf.Tensor 'conv-maxpool-3/pool:0' shape=(?, 94, 1, 128) dtype=float32>, <tf.Tensor 'conv-maxpool-4/pool:0' shape=(?, 51, 1, 128) dtype=float32>, <tf.Tensor 'conv-maxpool-5/pool:0' shape=(?, 237, 1, 128) dtype=float32>]最初,我在没有pooled_outputs = [tf.reshape(out, ["?", 94, 1, self.max_length]) for out in pooled_outputs]行的情况下尝试了这段代码,并得到了以下错误:
ValueError: Dimension 1 in both shapes must be equal, but are 51 and 237当我添加整形行时,我得到了以下错误:
TypeError: Expected binary or unicode string, got 94我知道的第二个错误是因为我传递了一个"?“对于新的尺寸,我认为第一个错误是因为张量不是相同的大小。,我怎样才能正确地填充这些张量,这样我就可以毫无问题地将它们连接起来?
发布于 2017-01-23 03:18:09
您可以将-1作为形状的一个组件传递给tf.reshape方法;它将从张量的形状自动推断,因此总大小将是相同的。
因此,尝试将问题行更改为
pooled_outputs = [tf.reshape(out, [-1, 94, 1, self.max_length]) for out in pooled_outputs]详细信息,请参阅文档
https://stackoverflow.com/questions/41797136
复制相似问题