作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这应该是一个非常简单的 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/
我是一名优秀的程序员,十分优秀!