mercury0114
mercury0114

Reputation: 1459

Why keras does not allow to add a convolutional layer in this way?

The following code

from tensorflow import keras
from keras.layers import Conv2D

model = keras.Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))

when executed throws an error:

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.convolutional.Conv2D object at 0x7fea0c002d10>

I also tried using the Convolutional2D but got the same error. Why?

Upvotes: 0

Views: 2037

Answers (2)

sdcbr
sdcbr

Reputation: 7129

Try this:

from tensorflow import keras
from tensorflow.keras.layers import Conv2D

model = keras.Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))

You are mixing a tf.keras Sequential model with a keras Conv2D layer (instead of a tf.keras Conv2D layer.)

Or, as remarked below, use actual Keras:

import keras
from keras.models import Sequential
from keras.layers import Conv2D

model = Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))

Upvotes: 5

Amin Golnari
Amin Golnari

Reputation: 171

You should import Sequential from keras models

from keras.models import Sequential
from keras.layers import Conv2D

model = Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))

Upvotes: 1

Related Questions