简体   繁体   English

Keras始终输出恒定值

[英]Keras Always Output a Constant Value

I've been training a simple Artificial Neural Network using Keras 我一直在使用Keras训练一个简单的人工神经网络

model = Sequential([
    Dense(32, input_shape=(32,), activation = 'relu'),
    Dense(20, activation='relu'),
    Dense(65, input_shape=(65,), activation='softmax')
])

model.summary()
model.compile(Adam(lr=.001), loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_samples, train_labels, batch_size=1000, epochs=10000,shuffle = True, verbose=2)

After training, I test the model, and it always outputs a constant value. 训练后,我测试了模型,并且它始终输出恒定值。 What shall I do? 我该怎么办?

https://github.com/keras-team/keras/issues/1727 https://github.com/keras-team/keras/issues/1727

The link above tells that I have to center my data to have a zero mean. 上面的链接告诉我,我必须将数据居中以使其均值为零。 I don't have any idea to do that. 我不知道要这么做。

If you want to center your data to have zero mean, just subtract sample mean from each samples. 如果您想将数据的平均值设为零,只需从每个样本中减去样本均值即可。

eg if you have features data and want to center it to have zero mean, you can do that using numpy's mean function as follows. 例如,如果您具有features数据,并且希望将其居中以具有零均值,则可以使用numpy的均值函数按如下方式进行操作。

features = features - np.mean(features)

You may also need to normalize using standard deviation. 您可能还需要使用标准偏差进行归一化。 This can be achieved by using numpy as follows. 这可以通过如下使用numpy来实现。

normalized_features = (features - np.mean(features))/ np.std(features)

I hope this helps. 我希望这有帮助。

You're using 'relu' activations without caring for it going to all zeros. 您正在使用'relu'激活,而无需关心它是否为全零。

Relu has a "zero region" when its input is negative, and this region, naturally has no gradient, so there is no possibility of it changing over training. 当Relu的输入为负值时,它具有一个“零区域”,并且该区域自然没有梯度,因此不可能改变其训练范围。

If all neurons in a layer go to the zero region, your model is frozen forever. 如果层中的所有神经元都归零,则模型将永远冻结。

One thing you can do is to replace 'relu' by 'sigmoid' or 'tanh' . 您可以做的一件事是将'relu'替换为'sigmoid''tanh'
Another thing is to use a BatchNormalization layer before it. 另一件事是在其之前使用BatchNormalization层。

The BatchNormalization does the centering for you, besides adding some speed to training and a little regularization. 除了增加训练速度和一些正规化功能外, BatchNormalization以为您居中。

model = Sequential([

    #optional BatchNormalization(input_shape(32,))

    Dense(32, input_shape=(32,)),
    BatchNormalization(),
    Activation('relu')
    Dense(20),
    BatchNormalization(),
    Activation('relu'),
    Dense(65, input_shape=(65,), activation='softmax')
])

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

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