我正在尝试使用CNN-LSTM进行3类分类,我已经在MATLAB上准备了数据,并使用以下代码进行了训练。它适用于我使用二进制交叉熵的二进制分类,但不适用于我使用sparse_categorical_crossentropy的多类情况。我的损失为nan,准确性为0.0000e + 00。怎么了?
import os
os.environ["CUDA_DEVİCE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import scipy.io
import numpy as np
mat = scipy.io.loadmat('frames3class_train80_test20.mat')
XTrain = mat['tensortrain']
y = mat['YTrain']
##
trainlength=2400
X = np.zeros(shape=(trainlength,8,18,400,1))
X[:,:,:,:,0]=XTrain
import keras
from keras.layers import Dense, LSTM, Flatten, TimeDistributed, Conv2D, Dropout,MaxPooling2D
from keras.models import Sequential
width = 18
height = 400
kernelsize =2
numberOfHiddenUnits =8
model = Sequential()
model.add(TimeDistributed(Conv2D(2, (kernelsize, kernelsize), activation='relu'),
input_shape=(None, width, height, 1)))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(numberOfHiddenUnits))
model.add(Dense(1, activation='sigmoid'))
opt=keras.optimizers.Adam(learning_rate=0.000001)
model.compile(loss=keras.losses.sparse_categorical_crossentropy, optimizer=opt, metrics=['acc'])
print(model.summary())
model.fit(X, y,batch_size=16,epochs=30)
Python控制台上的结果为
16/2400 [..............................] - ETA: 2s - loss: nan - acc: 0.0000e+00
80/2400 [>.............................] - ETA: 2s - loss: nan - acc: 0.0000e+00
144/2400 [>.............................] - ETA: 2s - loss: nan - acc: 0.0000e+00
208/2400 [=>............................] - ETA: 2s - loss: nan - acc: 0.0000e+00
272/2400 [==>...........................] - ETA: 2s - loss: nan - acc: 0.0000e+00
336/2400 [===>..........................] - ETA: 2s - loss: nan - acc: 0.0000e+00
384/2400 [===>..........................] - ETA: 2s - loss: nan - acc: 0.0000e+00
感谢您的帮助,
答案 0 :(得分:0)
好吧,我用一种热编码更改了标签,因此损失函数得以解决。