Python 如何连接;“锯齿状”;张量
我正试图用TensorFlow编写一个论文的实现,我遇到了一点障碍。在我的池层中,我必须将所有内容连接在一起。这是我使用的代码:Python 如何连接;“锯齿状”;张量,python,python-3.x,machine-learning,tensorflow,conv-neural-network,Python,Python 3.x,Machine Learning,Tensorflow,Conv Neural Network,我正试图用TensorFlow编写一个论文的实现,我遇到了一点障碍。在我的池层中,我必须将所有内容连接在一起。这是我使用的代码: pooled_outputs = [] for i, filter_size in enumerate(filter_sizes): with tf.name_scope("conv-maxpool-%s" % filter_size): # Conv layer filter_shape
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)
当我运行此代码时,它会为池输出打印此代码:
[<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>]
当我在重塑行中添加时,出现以下错误:
ValueError: Dimension 1 in both shapes must be equal, but are 51 and 237
TypeError: Expected binary or unicode string, got 94
我知道的第二个错误是因为我为新的大小传递了一个“?”,我认为第一个错误是因为张量的大小不同如何正确地填充这些张量,以便可以毫无问题地连接它们?您可以将-1
作为形状的一个组件传递给tf。重塑
方法;它将根据张量的形状自动推断,因此总大小将相同
因此,尝试将问题行更改为
pooled_outputs = [tf.reshape(out, [-1, 94, 1, self.max_length]) for out in pooled_outputs]
有关详细信息,请参阅