【发布时间】:2017-06-07 10:03:21
【问题描述】:
我正在尝试在 TensorFlow 中编写 this 论文的实现,但遇到了一些障碍。在我的池化层中,我必须将所有内容连接在一起。这是我使用的代码:
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
当我在 reshape 行中添加时,我得到了这个错误:
TypeError: Expected binary or unicode string, got 94
我知道的第二个错误是因为我传递了一个“?”对于新尺寸,我认为第一个错误是因为张量的尺寸不同。 如何正确填充这些张量,以便可以毫无问题地连接它们?
【问题讨论】:
标签: python python-3.x machine-learning tensorflow conv-neural-network