首页 > 解决方案 > 对于使用 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 损失,但我使用的是分类模型。是因为我的数据吗?

这些是测试和训练的大小

标签: pythontensorflowkeraslstmmulticlass-classification

解决方案


检查您的数据。

您的模型在随机数据上运行良好:

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))
    

推荐阅读