简体   繁体   English

Keras Logistic回归在第一个时期返回nan

[英]Keras Logistic Regression returns nan on first epoch

I have a really simple data set. 我有一个非常简单的数据集。 I cleaned the data (one hot encoding, normalizing the data and check for missing values or NaNs) and my learning rate is pretty small. 我清理了数据(一个热编码,规范化数据并检查缺失值或NaN),我的学习率非常小。 But when tried to run a simple logistic regression using Keras and Theano as backend 但是当试图使用Keras和Theano作为后端进行简单的逻辑回归时

model = Sequential() 
model.add(Dense(input_dim=84, activation='softmax',
            bias_initializer='normal', units=6)) 
rms = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(optimizer=rms, loss='categorical_crossentropy')


batch_size = 100
nb_epoch = 1
n = X.shape[0] # number of training examples
history = model.fit(X, Y_oh, batch_size=batch_size, epochs=nb_epoch)

Error: 错误:

Epoch 1/1
5459/5459 [==============================] - 0s - loss: nan

I checked here and tried to downgrade Theano to the version mentioned but it still gives the same error 在这里检查并试图将Theano降级到提到的版本,但它仍然给出了相同的错误

Here is what X looks like 这是X的样子

[[ 0.35755179  0.13747887  0.3        ...,  0.          0.          0.        ]
 [ 0.36401758  0.14963742  0.55       ...,  0.          0.          0.        ]
 [ 0.37889517  0.13775149  0.275      ...,  0.          0.          0.        ]
 ..., 
 [ 0.34387947  0.18706723  0.05       ...,  0.          0.          0.        ]
 [ 0.35708726  0.12905512  0.75       ...,  0.          0.          0.        ]
 [ 0.37915882  0.08061174  0.05       ...,  0.          1.          0.        ]]

and Y_oh (generated using the following code): 和Y_oh(使用以下代码生成):

Y_oh = np_utils.to_categorical(Y.T[0],6)

[[ 0.  0.  0.  0.  0.  1.]
 [ 0.  0.  0.  0.  0.  1.]
 [ 0.  0.  0.  0.  0.  1.]
 ..., 
 [ 1.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.]]

The problem is your activation here 问题是你在这里激活

model.add(Dense(input_dim=84, activation='softmax',
            bias_initializer='normal', units=6)) 

Swap 'softmax' for 'sigmoid' (or 'tanh') and you should be good. 交换'ssmoid'(或'tanh')的'softmax',你应该很好。

Softmax has the property that the sum of its outputs is 1 , so that the network's output has a probability interpretation. Softmax具有其输出和为1的特性 ,因此网络的输出具有概率解释。 The problem is, since you have only 1 output, it would either will never train (since the output will always be the same) or get unreasonable gradients trying to do so. 问题是,由于你只有1个输出,它将永远不会训练(因为输出总是相同的)或者得到不合理的渐变试图这样做。

eg Fix 例如修复

model.add(Dense(input_dim=84, activation='sigmoid',
            bias_initializer='normal', units=6)) 

Try to apply Log(0) and see what is the result. 尝试应用Log(0)并查看结果是什么。 it is undefined "NAN" I answered this question in details here NAN in neural network 它是未定义的“NAN”我在这里详细回答了这个问题NAN在神经网络中

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

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