python - 对于使用 LSTM、keras 模型进行分类,我得到 loss = nan 和 accuracy = 0
问题描述
我正在尝试使用 lstm 进行运动分类。这是我的模型
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 10, 32
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(LSTM(32, input_shape=(n_timesteps,n_features)))
# model.add(Dropout(0.5))
# model.add(Dense(32, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
loss, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return loss, accuracy
for r in range(repeats):
loss, score = evaluate_model(trainx, trainy, testx, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
print('>#%d: %.3f' % (r+1, loss))
这是我的输出
>#1: 0.000
>#1: nan
>#2: 0.000
>#2: nan
>#3: 0.000
>#3: nan
>#4: 0.000
>#4: nan
>#5: 0.000
>#5: nan
>#6: 0.000
>#6: nan
>#7: 0.000
>#7: nan
>#8: 0.000
>#8: nan
>#9: 0.000
>#9: nan
>#10: 0.000
>#10: nan
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Accuracy: 0.000% (+/-0.000)
我哪里做错了?我已经看到一些回归模型得到 nan 损失,但我使用的是分类模型。是因为我的数据吗?
解决方案
检查您的数据。
您的模型在随机数据上运行良好:
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM, Dense
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 10, 32
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(LSTM(32, input_shape=(n_timesteps,n_features)))
model.add(Dense(n_outputs, activation='softmax'))
#model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
loss, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return loss, accuracy
for r in range(5):
trainx = tf.random.uniform([10, 10, 10])
trainy = tf.random.uniform([10, 10])
testx = tf.random.uniform([10, 10, 10])
testy = tf.random.uniform([10, 10])
loss, score = evaluate_model(trainx, trainy, testx, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
print('>#%d: %.3f' % (r+1, loss))
推荐阅读
- android - gradlew app:dependencies 命令不起作用
- android - RecyclerView 项目中的 Android Spinner 问题
- workflow - Oracle 工作流消息属性作为响应
- javascript - 使用 ReactJS 的动作处理程序中的 setState 不更新
- windows - Robocopy 不断复制“修改过的”文件
- android - 如何将我的应用的目标 API 级别从 23 更改为 26
- bootstrap-4 - 不是更新所有 Bootstrap 网格断点
- java - Java(Android)文件对象对于 AsyncTask 来说太慢了
- css - 如何删除嵌入在网站上的订阅按钮中的文本
- python - Python 错误:TypeError:'Timestamp' 类型的对象不是 JSON 可序列化的'