gpt4 book ai didi

python - 如何连接 "Jagged"张量

转载 作者:行者123 更新时间:2023-11-30 09:52:46 27 4
gpt4 key购买 nike

我正在尝试编写 this 的实现我在 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

当我添加 reshape 线时,出现此错误:

TypeError: Expected binary or unicode string, got 94

我知道的第二个错误是因为我传递了一个“?”对于新的大小,我认为第一个错误是因为张量大小不同。 我怎样才能正确地填充这些张量,以便我可以毫无问题地连接它们?

最佳答案

您可以将 -1 作为形状的组成部分之一传递给 tf.reshape 方法;它将根据张量的形状自动推断,因此总大小将相同。

所以,尝试将问题行更改为

pooled_outputs = [tf.reshape(out, [-1, 94, 1, self.max_length]) for out in pooled_outputs]

请参阅documentation了解详情

关于python - 如何连接 "Jagged"张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com