gpt4 book ai didi

python - Keras 值错误 : This model has never been called

转载 作者:行者123 更新时间:2023-11-28 20:01:13 29 4
gpt4 key购买 nike

这应该是一个非常简单的 Keras 程序。它一直工作到最后一行代码。但是我已经调用了预测方法。当然,我使用了与训练数据相同的输入数据,但这应该无关紧要。

from keras.models import Sequential
import pandas as pd

url = 'https://raw.githubusercontent.com/werowe/logisticRegressionBestModel/master/KidCreative.csv'

data = pd.read_csv(url, delimiter=',')

labels=data['Buy']
features = data.iloc[:,2:16]

model = Sequential()
model.compile(optimizer='rmsprop' ,loss='binary_crossentropy',metrics=['accuracy'])
model.fit(data, labels, epochs=10, batch_size=32)
model.evaluate(labels, features, batch_size=128)
model.predict(labels)
model.summary()

但是我得到这个错误:

model.summary()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/walker/tf3/lib/python3.4/site-packages/keras/engine/network.py", line 1263, in summary
'This model has never been called, this its weights '
ValueError: This model has never been called, this its weights have not yet been created, so no summary can be displayed. Build the model first (e.g. by calling it on some test data).

最佳答案

第一步应该是实际构建一个模型,但您不需要这样做;即在 model = Sequential() 之后,应该有一些 model.add 语句,以便构建模型,然后再编译、拟合并用于评估或获取其摘要。

与其从一些质量不明确的 repo 中获得指导,不如从官方示例和教程开始 - 例如在 Keras MNIST CNN example 中查找示例;在这里只复制与模型相关的部分,我们得到:

model = Sequential()
# no model built, as in your case
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

这应该已经给出了一个错误:

TypeError: Sequential model cannot be built: model is empty. Add some layers first.

我很惊讶你没有在你的案例中报告。

这是我们应该做的(同样,请参阅链接以获取完整详细信息):

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

model.summary()
# works OK

关于python - Keras 值错误 : This model has never been called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497370/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com