简体   繁体   English

Keras Multilayer Perceptron train data show loss = nan

[英]Keras Multilayer Perceptron train data show loss = nan

I have data in data_2.csv like this.我在 data_2.csv 中有这样的数据。

a   b   c   d        e         outcome
2   9   5   10175   3500        10000
1   3   4   23085   35000       34000
2   1   3   NaN     23283.33333 50000
....

I try to train with MLP.我尝试使用 MLP 进行训练。 Column outcome is target output.列结果是目标 output。 This is my code.这是我的代码。

df = pd.read_csv('C://data_2.csv')

sc = MinMaxScaler()
X = sc.fit_transform(df.drop('income', axis=1).astype(float))

test= df[['outcome']]

y = sc.fit_transform(test.astype(float))

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=20, test_size=0.1)

model = Sequential()
model.add(Dense(32,input_shape=(5,), activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(X_train, y_train, epochs=200, batch_size=32, verbose=1)

y_pred = model.predict(X_test)

print("##########################################")
print(y_pred)

while I train data it show loss: nan like this当我训练数据时,它显示损失:nan like this

Epoch 1/200
45000/45000 [==============================] - 2s 48us/step - loss: nan
Epoch 2/200
45000/45000 [==============================] - 2s 38us/step - loss: nan

when finish training it show output like this.完成训练后,它会像这样显示 output。

##########################################
[[nan]
 [nan]
 [nan]
 ...
 [nan]
 [nan]
 [nan]]

X_train.shape is (45000, 5) y_train.shape is (45000, 1) All output are NaN. X_train.shape 是 (45000, 5) y_train.shape 是 (45000, 1) 所有 output 都是 NaN。 How to fix it?如何解决?

The prominent problem in your code is that you aren't cleaning your data.您的代码中的突出问题是您没有清理数据。 Neural Networks behave, in simple terms, by multiplying each node on each layer (that's a Dense layer).简单来说,神经网络的行为是将每一层(即密集层)上的每个节点相乘。 Then, imagine this: you have 32 nodes on the first layer, the largest positive number you have is about 35,000.然后,想象一下:第一层有 32 个节点,最大的正数约为 35,000。 If you multiply this 35,000 (more or less depending on weight and bias) by itself for 32 times, your number will be over the limit and will end up with NaN in just a few epochs.如果你将这 35,000(或多或少取决于权重和偏差)乘以 32 次,你的数字将超过限制,并在几个时期内以 NaN 结束。

Thus, your problem is with your activator, relu .因此,您的问题在于您的激活relu This activator only filters the positive number (zero or greater) and turns any negative numbers to zero.此激活器仅过滤正数(零或更大)并将任何负数变为零。 With this activator, your initial nodes will have astronomical numbers!使用此激活器,您的初始节点将拥有天文数字!

I recommend changing your activator into a sigmoid function.我建议将您的激活器更改为sigmoid function。 This function scales a number between 1 and -1 (mostly).这个 function 在 1 和 -1 之间缩放一个数字(大部分)。 With this, your large inputs will be turned to numbers with absolute values of less than 1.有了这个,你的大输入将变成绝对值小于 1 的数字。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM