简体   繁体   English

使用keras卷积1D层时的负尺寸误差

[英]Negative dimension error while using keras Convolutional1D Layer

I'm trying to create a char cnn using Keras. 我正在尝试使用Keras创建一个char cnn。 That type of cnn requires you to use Convolutional1D layer. 这种类型的cnn要求你使用Convolutional1D层。 But all the ways I try to add them to my model, it gives me errors at creation stage. 但是我试图将它们添加到我的模型的所有方法,它在创建阶段给我错误。 Here is my code: 这是我的代码:

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

And here is the error I receive when I try to call char_cnn function 这是我尝试调用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].

How to fix it? 怎么解决?

Your downsampling is too aggressive and the key argument here is max_len : when it's too small, the sequence becomes too short to perform either a convolution or a max-pooling. 你的下采样过于激进,这里的关键参数是max_len :当它太小时,序列变得太短而无法执行卷积或最大池化。 You set pool_size=3 , hence it shrinks the sequence by a factor of 3 after each pooling (see the example below). 你设置pool_size=3 ,因此它在每次汇集后将序列缩小了3倍(参见下面的例子)。 I suggest you try pool_size=2 . 我建议你尝试pool_size=2

The minimal max_len that this network can handle is max_len=123 . 最小max_len 网络可以处理被max_len=123 In this case x shape is transformed in the following way (according to conv_layers ): 在这种情况下, x形状按以下方式转换(根据conv_layers ):

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

Setting a smaller value, like max_len=120 causes x.shape=(?, 4, 256) before the last layer and this can't be performed. 设置较小的值,如max_len=120会导致最后一层之前的x.shape=(?, 4, 256) ,并且无法执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二维卷积层中Keras中merge()函数的类型错误 - Type error for merge() function in Keras for 2d convolutional layer keras Conv2d 值错误:负维度大小 - keras Conv2d value error: Negative dimension size 卷积层和输入数据上的Keras错误 - Keras Error on Convolutional Layer and Input Data 使用 keras 自定义层时构建错误 - Error in build while using keras custom layer Keras 尺寸错误 - (层“max_pooling2d”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=6。) - Keras Dimension error - (Input 0 of layer "max_pooling2d" is incompatible with the layer: expected ndim=4, found ndim=6.) 在使用tensorflow keras时,我收到错误消息,对于第一个conv2D层,添加的层必须是类的实例 - While using tensorflow keras i am getting error saying the added layer must be an instance of class, for the first conv2D layer 如何使用 Keras 中的功能 API 在快捷连接中添加卷积层? - How to add a convolutional layer in the shortcut connection using functional API in Keras? 第二个卷积层的输入维数? - Dimension of the input of second convolutional layer? 输入层的 TensorFlow Keras 维度错误 - TensorFlow Keras dimension error for input layer 如何在 keras 中使用重塑层添加维度 - How to add a dimension using reshape layer in keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM