简体   繁体   English

Keras - ValueError:Sequential模型中的第一层必须得到`input_shape`或`batch_input_shape`参数

[英]Keras - ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument

I'm getting the following error when trying to run a model: 尝试运行模型时出现以下错误:

Using TensorFlow backend.
train.py:99: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(pool_size=(2, 2), data_format="channels_last")`
  model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))
Traceback (most recent call last):
  File "train.py", line 361, in <module>
    save_bottleneck_features()
  File "train.py", line 99, in save_bottleneck_features
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))
  File "C:\Python35\lib\site-packages\keras\models.py", line 420, in add
    raise ValueError('The first layer in a '
ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument.

Those are the relevant lines of code ( train.py ): 这些是相关的代码行( train.py ):

model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))

In model.py : model.py

def add(self, layer):
        """Adds a layer instance on top of the layer stack.

        # Arguments
            layer: layer instance.

        # Raises
            TypeError: If `layer` is not a layer instance.
            ValueError: In case the `layer` argument does not
                know its input shape.
            ValueError: In case the `layer` argument has
                multiple output tensors, or is already connected
                somewhere else (forbidden in `Sequential` models).
        """
        if not isinstance(layer, Layer):
            raise TypeError('The added layer must be '
                            'an instance of class Layer. '
                            'Found: ' + str(layer))
        if not self.outputs:
            # first layer in model: check that it is an input layer
            if not layer.inbound_nodes:
                # create an input layer
                if not hasattr(layer, 'batch_input_shape'):
                    raise ValueError('The first layer in a '
                                     'Sequential model must '
                                     'get an `input_shape` or '
                                     '`batch_input_shape` argument.')
                # Instantiate the input layer.
                x = Input(batch_shape=layer.batch_input_shape,
                          dtype=layer.dtype, name=layer.name + '_input')
                # This will build the current layer
                # and create the node connecting the current layer
                # to the input layer we just created.
                layer(x)

            if len(layer.inbound_nodes) != 1:
                raise ValueError('A layer added to a Sequential model must '
                                 'not already be connected somewhere else. '
                                 'Model received layer ' + layer.name +
                                 ' which has ' +
                                 str(len(layer.inbound_nodes)) +
                                 ' pre-existing inbound connections.')

            if len(layer.inbound_nodes[0].output_tensors) != 1:
                raise ValueError('All layers in a Sequential model '
                                 'should have a single output tensor. '
                                 'For multi-output layers, '
                                 'use the functional API.')

            self.outputs = [layer.inbound_nodes[0].output_tensors[0]]
            self.inputs = topology.get_source_inputs(self.outputs[0])

            # We create an input node, which we will keep updated
            # as we add more layers
            topology.Node(outbound_layer=self,
                          inbound_layers=[],
                          node_indices=[],
                          tensor_indices=[],
                          input_tensors=self.inputs,
                          output_tensors=self.outputs,
                          # no model-level masking for now
                          input_masks=[None for _ in self.inputs],
                          output_masks=[None],
                          input_shapes=[x._keras_shape for x in self.inputs],
                          output_shapes=[self.outputs[0]._keras_shape])
        else:
            output_tensor = layer(self.outputs[0])
            if isinstance(output_tensor, list):
                raise TypeError('All layers in a Sequential model '
                                'should have a single output tensor. '
                                'For multi-output layers, '
                                'use the functional API.')
            self.outputs = [output_tensor]
            # update self.inbound_nodes
            self.inbound_nodes[0].output_tensors = self.outputs
            self.inbound_nodes[0].output_shapes = [self.outputs[0]._keras_shape]

        self.layers.append(layer)
        self.built = False

How can I solve this issue? 我该如何解决这个问题?

From error message 来自错误消息

ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument.

if MaxPooling is the first layer of your model you should pass input_shape (or batch_input_shape ) argument like 如果MaxPooling是模型的第一层,你应该传递input_shape (或batch_input_shape )参数

model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf", input_shape=(16, 16)))

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

相关问题 用于Keras LSTM模型的batch_input_shape - batch_input_shape for Keras LSTM model Keras LSTM上的batch_input_shape元组 - batch_input_shape tuple on Keras LSTM 如何为Keras顺序模型指定input_shape - How to specify input_shape for Keras Sequential model Keras 忽略提供给第一层的 input_shape - Keras ignores the input_shape provided to the first layer Keras 自定义层:“input_shape”不可接受 - Keras custom Layer: "input_shape" is not suscriptable Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer &quot;sequential&quot; is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: 加载集合 keras model 给出 ValueError: Invalid input_shape argument (None, 224, 224, 3): Z20F35E630DAF44DBFA4C3F608F5399D8C 有输入 - Loading ensemble keras model gives ValueError: Invalid input_shape argument (None, 224, 224, 3): model has 0 tensor inputs Keras 顺序模型输入形状 - Keras Sequential model input shape Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) keras.Sequential()的“ input_shape”格式是什么? - What is the format of “input_shape” is keras.Sequential()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM