【问题标题】:How to get a binary bipolar activation function for output as +1 and -1 in keras?如何在keras中获得输出为+1和-1的二进制双极激活函数?
【发布时间】:2019-01-03 07:19:59
【问题描述】:

我只想将 y_pred 输出设为 +1 或 -1。它不应该有中间实数值,甚至不应该是零。

classifier = Sequential()

#adding layers
# Adding the input layer and the first hidden l`enter code here`ayer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation ='relu', input_shape = (22,)))
# Adding the second hidden layer classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'tanh'))

# Compiling Neural Network
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Fitting our model 
classifier.fit(x_train, y_train, batch_size = 10, epochs = 100)

# Predicting the Test set results
y_pred = classifier.predict(x_test)

y_pred 的输出值在 [-1,1] 范围内,但我希望值只能是 1 或 -1。

【问题讨论】:

  • 设置一个阈值,比如 0,高于 0 为 1,低于 0 为 -1
  • @Oswald 在修改中我可以在损失或激活参数中进行,而不是将 y_pred 修改为 y_pred[y_pred > 0] = 1 y_pred[y_pred

标签: python-3.x machine-learning keras deep-learning anaconda


【解决方案1】:

为了正常运行,神经网络需要一个可以获取非整数值的激活函数。如果您需要严格离散的输出,则需要自己转换输出值。

【讨论】:

    【解决方案2】:

    当您在代码中实现binary_crossentropy 损失时,Keras 会自动获取输出并将阈值 0.5 应用于该值。这使得任何高于 0.5 的值都为 1,低于 0 的值。不幸的是,在 keras 中没有简单的方法来更改阈值。您将不得不编写自己的损失函数。

    Here 是一个 Stackoverflow 链接,可指导您执行此操作。

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 2020-02-13
      • 1970-01-01
      • 2022-06-22
      • 2018-02-01
      • 2021-05-17
      • 2019-01-13
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多