LSTM输入形状和损耗NaN

时间:2020-05-23 07:11:35

标签: python tensorflow machine-learning lstm

我正在进行LSTM Multi分类。我有一个具有6个属性的数据集:5个数据(所有数字)和1个标签(4个值:0、1、2、3)。我有331713个条目。 在完成矢量后,我将其如下所示:

classi = 4
X_Train
[[6 646 26 1 400.0]
 [12 1265 28 0 400.0]
 [18 1827 36 1 400.0]
 ...
 [1 192 50 1 400.0]
 [1 171 29 1 400.0]
 [19 1921 22 0 400.0]]

y_train
[1 3 1 ... 3 1 0]

然后我使用to_categorical函数将标签转换为

train_labels = to_categorical(y_train,num_classes=classi)
y_train = train_labels

我的y_train变成了

array([[0., 1., 0., 0.],
       [0., 0., 0., 1.],
       [0., 1., 0., 0.],
       ...,
       [0., 0., 0., 1.],
       [0., 1., 0., 0.],
       [1., 0., 0., 0.]], dtype=float32)

现在,我将X_train重塑为

X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[1],1))

变量的形状是

X_train (331713, 1, 5)
y_train (331713, 4)

我的网络是:

regressor = Sequential()
# First LSTM layer with Dropout regularisation
regressor.add(LSTM(units=200,activation='relu', return_sequences=True, input_shape=(X_train.shape[1],1)))
regressor.add(Dropout(0.2))
# Second LSTM layer
regressor.add(LSTM(units=200,activation='relu', return_sequences=True))
regressor.add(Dropout(0.2))
# Third LSTM layer
regressor.add(LSTM(units=200,activation='relu', return_sequences=True))
regressor.add(Dropout(0.2))
# Fourth LSTM layer
regressor.add(LSTM(units=200,activation='relu', return_sequences=False))
regressor.add(Dropout(0.2))

# The output layer
regressor.add(Dense(units=classi,activation="softmax"))

# Compiling 
optimizer = keras.optimizers.Adam(learning_rate=0.0001,beta_1=0.9,clipvalue=0.5, beta_2=0.999, epsilon=1e-08, decay=0.0)
regressor.compile(optimizer=optimizer,loss='categorical_crossentropy', metrics=['accuracy'])
# Fitting to the training set
regressor.fit(X_train,y_train,epochs=100,batch_size=128)

我得到了NaN损失和高精度。 我怎么解决这个问题?我不了解我的X_train和Y_train形状是否适合我的情况。谢谢大家

0 个答案:

没有答案