简体   繁体   中英

How to solve "model has not yet been built" error in Keras?

I got this error while working with CNN:

This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.

My code is below :

 from tensorflow.keras.models import Sequential
 from tensorflow.keras.layers import Dense,Dropout
 from tensorflow.keras.layers import Conv2D
 from tensorflow.keras.layers import Flatten
 from tensorflow.keras.layers import MaxPooling2D
 from tensorflow.keras.callbacks import TensorBoard

 model=Sequential()
 model.add(Conv2D(32,5,input_shape=(1,28,28),activation='relu',data_format='channels_last'))
 model.add(MaxPooling2D(pool_size=(2,2),strides=2))
 model.add(Flatten())
 model.add(Conv2D(64,5,input_shape=(1,28,28),activation='relu',data_format='channels_last'))
 model.add(MaxPooling2D(pool_size=2,strides=2))
 model.add(Flatten())
 model.add(Dense(units=32,activation='relu'))
 model.add(Dropout(0.4))
 model.add(Dense(units=1,activation='softmax'))

 model.summary()

According to the error, run:

input_shape=(1,28,28)
model.build(input_shape)                
model.summary()

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