将神经网络输出限制为正值

标签 r machine-learning neural-network

我正在使用 R 的神经网络函数训练 160 个独立的神经网络模型,每个模型都有两个输入变量,并将它们部署到测试数据上。

我需要将预测限制为正值。
我认为在 nueralnet 函数中指定 act.fct = "logistic"可以实现此目的。

但是,激活函数似乎并未被应用。检查权重后,函数的输出似乎只是输入值和权重加上偏置节点值的和积。

对于我的输出,在我尝试预测的 9 条记录中,有 2 条为负数 - 均为 -0.0885。为了简单起见,我指定了一个没有隐藏层的神经网络。创建模型后,我确定负值的罪魁祸首是偏差节点。虽然两个变量的权重为正,但偏差节点为负。

if (require(neuralnet==F)) install.packages("neuralnet"); require(neuralnet)

for (i in 1:160) {
    training.list[[i]] <- neuralnet(nn.training[,i] ~  nn.training[,i+160]+nn.training[,i+320], nn.training, 
                              act.fct = "logistic", hidden = 0, threshold = 0.01)                                  
 }

 plot(training.list[[1]])

# Load test data in proper format 
load("nn_test.Rdata")

 # predict on only the first dataset for illustration's sake
 a <- compute(training.list[[1]], cbind(test[,1], test[,1+160]))

a的输出是

$net.result
         [,1]
  [1,]  0.44661508
  [2,]  0.31966633
  [3,]  0.01916739
  [4,] -0.08849801
  [5,] -0.08849801
  [6,]  0.39840670
  [7,]  0.33252190
  [8,]  0.33573579
  [9,]  0.38555113

图中的权重为 0.71174,每个输入节点的权重为 0.39118,偏置节点的权重为 -0.0891。

为了将输出限制为正值,我需要指定什么?

最佳答案

默认情况下,act.fct应用于隐藏层。如果您希望它也影响输出一 - 您必须设置linear.output = FALSE

来自documentation

act.fct a differentiable function that is used for smoothing the result of the cross product of the covariate or neurons and the weights. Additionally the strings, ’logistic’ and ’tanh’ are possible for the logistic function and tangent hyperbolicus.

linear.output logical. If act.fct should not be applied to the output neurons set linear output to TRUE, otherwise to FALSE.

关于将神经网络输出限制为正值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33848124/

相关文章:

image-processing - 人脸认证

r - 如何在没有 alpha 值的情况下将两个 geom_bar 相互叠加

r - 注释掉 Rmd 文件的一些 block /部分

r - r中的粘贴功能

r - R 中 Tomek 链接的快速计算

python - 在 Tensorflow 中同时训练和测试

c++ - 无法加载共享对象 : undefined symbol

python - 朴素贝叶斯多项式NB scikit-learn/sklearn

python - 使用 PyTorch 生成新图像

algorithm - 元素拾取竞赛的AI算法