gpt4 book ai didi

tensorflow - 使用 keras Convolutional1D Layer 时出现负维度错误

转载 作者:行者123 更新时间:2023-11-30 08:36:09 26 4
gpt4 key购买 nike

我正在尝试使用 Keras 创建一个 char cnn。这种类型的 cnn 要求您使用 Convolutional1D 层。但我尝试将它们添加到模型中的所有方法,都会在创建阶段给我带来错误。这是我的代码:

def char_cnn(n_vocab, max_len, n_classes):
conv_layers = [[256, 7, 3],
[256, 7, 3],
[256, 3, None],
[256, 3, None],
[256, 3, None],
[256, 3, 3]]
fully_layers = [1024, 1024]
th = 1e-6

embedding_size = 128

inputs = Input(shape=(max_len,), name='sent_input', dtype='int64')

# Embedding layer

x = Embedding(n_vocab, embedding_size, input_length=max_len)(inputs)

# Convolution layers
for cl in conv_layers:
x = Convolution1D(cl[0], cl[1])(x)
x = ThresholdedReLU(th)(x)
if not cl[2] is None:
x = MaxPooling1D(cl[2])(x)


x = Flatten()(x)


#Fully connected layers

for fl in fully_layers:
x = Dense(fl)(x)
x = ThresholdedReLU(th)(x)
x = Dropout(0.5)(x)


predictions = Dense(n_classes, activation='softmax')(x)

model = Model(input=inputs, output=predictions)

model.compile(optimizer='adam', loss='categorical_crossentropy')

return model

这是我尝试调用 char_cnn 函数时收到的错误

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
685 graph_def_version, node_def_str, input_shapes, input_tensors,
--> 686 input_tensors_as_shapes, status)
687 except errors.InvalidArgumentError as err:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
515 compat.as_text(c_api.TF_Message(self.status.status)),
--> 516 c_api.TF_GetCode(self.status.status))
517 # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Negative dimension size caused by subtracting 3 from 1 for 'conv1d_26/convolution/Conv2D' (op: 'Conv2D') with input shapes: [?,1,1,256], [1,3,256,256].

如何解决?

最佳答案

您的下采样过于激进,这里的关键参数是 max_len:当它太小时,序列会变得太短而无法执行卷积或最大池化。您设置了 pool_size=3,因此它会在每次池化后将序列缩小 3 倍(请参见下面的示例)。我建议您尝试 pool_size=2

此网络可以处理的最小 max_lenmax_len=123。在本例中,x 形状按以下方式转换(根据 conv_layers):

(?, 123, 128)
(?, 39, 256)
(?, 11, 256)
(?, 9, 256)
(?, 7, 256)
(?, 5, 256)

设置较小的值,例如 max_len=120 会导致 x.shape=(?, 4, 256) 在最后一层之前,并且无法执行此操作。

关于tensorflow - 使用 keras Convolutional1D Layer 时出现负维度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48793510/

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