简体   繁体   中英

SyntaxError: positional argument follows keyword argument in CNN model

I am trying to make a CNN model and I get a the following error

keras.layers.MaxPooling2D(pool_size = (2,2), padding= "same"),
    ^
SyntaxError: positional argument follows keyword argument

the error applies when I want to add a dropout or max pooling, I will add my code below and comment the lines that gave me the mentioned syntax error.

I get different errors when I try to run dropout and comment out maxpooling and vice versa.

Note: I am using hp.Choice and hp.Int from kerastuner from the following documentary ( https://keras-team.github.io/keras-tuner/ ) It's working fine I am pretty sure the error isn't because of misuse of it.

  model = keras.Sequential([
    keras.layers.Conv2D(
        keras.layers.BatchNormalization(),
        input_shape = (img_rows, img_cols, 1),
        kernel_size = hp.Choice("conv1_kernel", values = [3, 6]),
        filters = hp.Int("conv1_filters", min_value = 32, max_value = 128, step = 16),
        #keras.layers.MaxPooling2D(pool_size = (2,2), padding= "same"),
        #keras.layers.Dropout(0.2),
        activation = "relu"
    ),

    keras.layers.Conv2D(
        keras.layers.BatchNormalization(),
        input_shape = (img_rows, img_cols, 1),
        kernel_size = hp.Choice("conv2_kernel", values = [3, 6]),
        filters = hp.Int("conv2_filters", min_value = 32, max_value = 64, step = 16),
        #keras.layers.MaxPooling2D(pool_size = (2,2), padding = "same"),
        #keras.layers.Dropout(0.5),
        activation = "relu"
    ), 
    

    keras.layers.Flatten(),
    keras.layers.Dense(
        units = hp.Int("dense1_units", min_value = 16, max_value = 256, step = 16),
        activation = "relu"

    ),
    
    keras.layers.Dense(units = 7, activation = "softmax")

  ])

  model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-1, 1e-2, 1e-3])),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
  
  return model```

keras.layers.MaxPooling2D(pool_size = (2,2), padding= "same"),
keras.layers.Dropout(0.2),

are the same entities as keras.layers.Conv2D : they are layers and should be added in the same way to model architecture:

keras.layers.Conv2D(
        input_shape = (img_rows, img_cols, 1),
        kernel_size = hp.Choice("conv1_kernel", values = [3, 6]),
        filters = hp.Int("conv1_filters", min_value = 32, max_value = 128, step = 16),
        activation = "relu"
    ),

keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D(pool_size = (2,2), padding= "same"),
keras.layers.Dropout(0.2),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM