machine-learning - ReLU 没有学习处理负输入 Keras/Tensorflow

标签 machine-learning tensorflow neural-network artificial-intelligence keras

我希望我的神经网络将负值转换为正值。理论上,这可以使用 ReLU 函数和 1 个节点来完成,该节点将输入权重学习为 -1(因此负输入乘以 -1 = 正输入。

它只是继续输出 0。代码如下。我使用 -1 作为输入值来看看它是否可以至少在单个输入上进行学习。

我尝试添加更多层,但没有帮助查看编辑,如果我添加更多层,它确实有帮助

train_input = np.asarray([[-1]]*10000) # Input arr of -1s
train_output = np.asarray(map(lambda x: [abs(x[0])] , train_input))

# Define the model
model = Sequential()
model.add(Dense(1, input_dim=1, kernel_initializer='normal', activation='linear'))
model.add(LeakyReLU(alpha=.001))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

# Train and evaluate
model.fit(train_input, train_output, epochs=10, batch_size=10, verbose=0)
test_model_output = model.predict(test_input)
print str(test_input[0][0]) + " " + str(test_output[0][0]) + " " +  str(test_model_output[0][0])

我得到的输出如下(第一个值是输入,第二个值是预期输出,第三个是模型输出)

-1 1 0.0

编辑 我尝试使用随机统一初始化程序,这样它就会初始化负权重并且它有效。我明白为什么这会让网络学习变得更容易。但我不明白为什么有必要。

from keras.initializers import RandomUniform
model.add(Dense(1, input_dim=1, kernel_initializer=RandomUniform(minval=-0.05, maxval=0.05, seed=None), activation='linear'))

编辑2 有人提到我没有足够的时间来训练数据。起初,我认为将数据量增加 10 倍,并将批处理缩小 10 倍(更多迭代)是可行的。它没有 但是如果我添加 10 倍以上的 epoch(总共 100 个),它确实有效。因此将正初始化权重转换为负初始化权重需要很长时间

最佳答案

我将使用以下方法将第一个权重初始化为负数 keras.initializers.Constant(值=-1) https://keras.io/initializers/#constant

可能有助于让第一个神经元放电。

关于machine-learning - ReLU 没有学习处理负输入 Keras/Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43371117/

相关文章:

machine-learning - TensorFlow:训练好的模型存储在哪里以及如何访问?

python - 将输入的 JPG 图像转换为 scikit 学习 SVM 分类器

python - 如何使用 Tensorflow make_csv_dataset 为 Keras 模型配置数据集管道

apache-spark - Apache SystemML 标量矩阵(元素方面)乘法不起作用

python - Tensorflow - autodiff 是否让我们从 back-prop 实现中重生?

tensorflow - tf.decode_raw 和 tf.reshape 使用不同的图像大小

machine-learning - 为什么rnn的平均权重不断攀升?

optimization - 神经网络、局部最小规避技术

tensorflow - 卷积神经网络输出所有标签的相同概率

machine-learning - scikit 管道 FeatureUnion 的尺寸不匹配错误