简体   繁体   English

如何解决 Keras 中的“模型尚未构建”错误?

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

I got this error while working with CNN:我在使用 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()

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

相关问题 RNN 模型错误:“ValueError:此模型尚未构建。” - RNN Model Error: "ValueError: This model has not yet been built." This model has not been built 错误在 model.summary() - This model has not yet been built error on model.summary() 双向 LSTM model 尚未构建错误 - Bidirectional LSTM model not yet been built error model.RNN 的摘要错误。 此 model 尚未构建 - model.summary error for RNN. This model has not yet been built 这个model还没有建好。 首先通过调用 build() 或对一批数据调用 model 来构建 model - This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data 如何使用 CNN 模型 Keras 解决错误? - How can solve Error with CNN Model Keras? 使用keras加载模型时如何解决错误 - How to solve error while loading model with keras Keras 如何在 model 拟合后评估单个图像 - Keras how to evaluate a single image once model has been fit 在使用 Keras 运行 CNN model 时如何解决此错误? - How can I solve this error during running CNN model with Keras? 如何解决 Keras layer.get_output_shape_at() 抛出的异常“该层从未被调用过,因此没有定义的输出形状”? - How to solve the exception " The layer has never been called and thus has no defined output shape" thrown by Keras layer.get_output_shape_at()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM