训练损失是 Nan - 但训练数据都在范围内,没有 null
training loss is Nan - but trainning data is all in range without null
当我执行 model.fit(x_train_lstm, y_train_lstm, epochs=3, shuffle=False, verbose=2)
作为nan,我总是吃亏:
Epoch 1/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step
Epoch 2/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 74ms/step
Epoch 3/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step
我的x_training字形为(2475, 48),y_train字形为(2475,)
- 我在(2315, 160, 48)中导出我的输入训练集,所以2315组训练数据,160个作为我的回送时间窗口,48个特征
- 相应地,y_train在(2315, 1)
的形状中是0或1
全部在 (-1,1) 范围内:
我的模型是这样的:
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_6 (LSTM) (None, 160, 128) 90624
dropout_4 (Dropout) (None, 160, 128) 0
lstm_7 (LSTM) (None, 160, 64) 49408
dropout_5 (Dropout) (None, 160, 64) 0
lstm_8 (LSTM) (None, 32) 12416
dense_2 (Dense) (None, 1) 33
=================================================================
Total params: 152,481
Trainable params: 152,481
Non-trainable params: 0
- 我尝试了不同的 LSTM 单元:48、60、128、160,其中 none 个有效
- 我查看我的训练数据,都在(-1,1)
范围内
- 我的数据集中没有'null',
x_train.isnull().values.any()
输出False
现在不知道哪里可以尝试更多~
我的型号代码是:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.layers import Dropout
def create_model(win = 100, features = 9):
model = Sequential()
model.add(LSTM(units=128, activation='relu', input_shape=(win, features),
return_sequences=True))
model.add(Dropout(0.1))
model.add(LSTM(units=64, activation='relu', return_sequences=True))
model.add(Dropout(0.2))
# no need return sequences from 'the last layer'
model.add(LSTM(units=32))
# adding the output layer
model.add(Dense(units=1, activation='sigmoid'))
# may also try mean_squared_error
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
这里我绘制了一些 train_y 样本:
两件事:尝试标准化您的时间序列数据并使用 relu
作为 lstm
层的激活函数不是 'conventional'。检查此 post 以获得更多见解。一个例子:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.layers import Dropout
import tensorflow as tf
layer = tf.keras.layers.Normalization(axis=-1)
x = tf.random.normal((500, 100, 9))
y = tf.random.uniform((500, ), dtype=tf.int32, maxval=2)
layer.adapt(x)
def create_model(win = 100, features = 9):
model = Sequential()
model.add(layer)
model.add(LSTM(units=128, activation='tanh', input_shape=(win, features),
return_sequences=True))
model.add(Dropout(0.1))
model.add(LSTM(units=64, activation='tanh', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=32))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
model = create_model()
model.fit(x, y, epochs=20)
2022.Mar.17更新
经过进一步的调试,我最终发现问题其实是因为我新添加的特征包含np.inf
,删除那些行后,我的问题解决了,我现在可以看到损失值[=16] =]
6/6 [==============================] - 2s 50ms/step - loss: 0.6936 - accuracy: 0.5176
注意,np.inf 有符号,所以确保 np.inf
和 -np.inf
都被删除:
all_dataset = all_dataset[all_dataset.feature_issue != np.inf]
all_dataset = all_dataset[all_dataset.feature_issue != -np.inf]
2022.Mar.16
经过一些调试,我解决了 2 个新添加的功能实际上导致了问题。所以,问题来自数据,但与其他人不同的是,我的数据不包含nan,没有超出范围(本来我认为所有数据都需要归一化)
但我还不能说出原因,因为它们看起来不错
明天我会继续研究它,欢迎任何建议!
当我执行 model.fit(x_train_lstm, y_train_lstm, epochs=3, shuffle=False, verbose=2)
作为nan,我总是吃亏:
Epoch 1/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step
Epoch 2/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 74ms/step
Epoch 3/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step
我的x_training字形为(2475, 48),y_train字形为(2475,)
- 我在(2315, 160, 48)中导出我的输入训练集,所以2315组训练数据,160个作为我的回送时间窗口,48个特征
- 相应地,y_train在(2315, 1) 的形状中是0或1
全部在 (-1,1) 范围内:
我的模型是这样的:
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_6 (LSTM) (None, 160, 128) 90624
dropout_4 (Dropout) (None, 160, 128) 0
lstm_7 (LSTM) (None, 160, 64) 49408
dropout_5 (Dropout) (None, 160, 64) 0
lstm_8 (LSTM) (None, 32) 12416
dense_2 (Dense) (None, 1) 33
=================================================================
Total params: 152,481
Trainable params: 152,481
Non-trainable params: 0
- 我尝试了不同的 LSTM 单元:48、60、128、160,其中 none 个有效
- 我查看我的训练数据,都在(-1,1) 范围内
- 我的数据集中没有'null',
x_train.isnull().values.any()
输出False
现在不知道哪里可以尝试更多~
我的型号代码是:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.layers import Dropout
def create_model(win = 100, features = 9):
model = Sequential()
model.add(LSTM(units=128, activation='relu', input_shape=(win, features),
return_sequences=True))
model.add(Dropout(0.1))
model.add(LSTM(units=64, activation='relu', return_sequences=True))
model.add(Dropout(0.2))
# no need return sequences from 'the last layer'
model.add(LSTM(units=32))
# adding the output layer
model.add(Dense(units=1, activation='sigmoid'))
# may also try mean_squared_error
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
这里我绘制了一些 train_y 样本:
两件事:尝试标准化您的时间序列数据并使用 relu
作为 lstm
层的激活函数不是 'conventional'。检查此 post 以获得更多见解。一个例子:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.layers import Dropout
import tensorflow as tf
layer = tf.keras.layers.Normalization(axis=-1)
x = tf.random.normal((500, 100, 9))
y = tf.random.uniform((500, ), dtype=tf.int32, maxval=2)
layer.adapt(x)
def create_model(win = 100, features = 9):
model = Sequential()
model.add(layer)
model.add(LSTM(units=128, activation='tanh', input_shape=(win, features),
return_sequences=True))
model.add(Dropout(0.1))
model.add(LSTM(units=64, activation='tanh', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=32))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
model = create_model()
model.fit(x, y, epochs=20)
2022.Mar.17更新
经过进一步的调试,我最终发现问题其实是因为我新添加的特征包含np.inf
,删除那些行后,我的问题解决了,我现在可以看到损失值[=16] =]
6/6 [==============================] - 2s 50ms/step - loss: 0.6936 - accuracy: 0.5176
注意,np.inf 有符号,所以确保 np.inf
和 -np.inf
都被删除:
all_dataset = all_dataset[all_dataset.feature_issue != np.inf]
all_dataset = all_dataset[all_dataset.feature_issue != -np.inf]
2022.Mar.16
经过一些调试,我解决了 2 个新添加的功能实际上导致了问题。所以,问题来自数据,但与其他人不同的是,我的数据不包含nan,没有超出范围(本来我认为所有数据都需要归一化)
但我还不能说出原因,因为它们看起来不错
明天我会继续研究它,欢迎任何建议!