是否可以在公式中指定条件,例如
out1 + out2 ~ in1 + in2 + in3 <with all (out1 + out2 = 1)>
一个可能的例子:
尝试预测由红色
、绿色
和蓝色
组合而成的颜色。当没有关于模型的假设时,这可以通过神经网络来完成:
library("neuralnet")
red <- runif(n=50)
green <- (1 - red) * runif(n=50)
blue <- 1 - red - green
input1 <- green^2
input2 <- sin(red)
trainingdata <- data.frame(red, green, blue, input1, input2)
color.net <- neuralnet(red + green + blue ~ input1 + input2, trainingdata)
test.red <- runif(10)
test.green <- (1 - test.red) * runif(n=10)
test.input1 <- test.green^2
test.input2 <- sin(test.red)
testdata <- data.frame(test.input1, test.input2)
testoutcolor <- as.data.frame(compute(color.net, testdata)[2])
colnames(testoutcolor) <- c("red", "green", "blue")
testoutcolor$sum <- testoutcolor$red + testoutcolor$green + testoutcolor$blue
testoutcolor
即使trainingdata
具有red + green + blue = 1
,神经网络很可能不会自行“学习”此条件,而只会输出近似此条件的值.
是否可以强制神经网络满足这个条件?
[编辑]
正如 @Spacedman 所说,在此示例中,blue
不会向模型添加信息 - 它可以仅使用 1 - red - green
进行计算。我仍然需要一种方法来“告诉”模型存在一个条件:
red + green <= 1
最佳答案
只需向数据集添加一个功能,如果条件为 true,则返回 1;如果为 false,则返回 0(或 -1,具体取决于您的激活函数),反之亦然。如果您想强制 ML 算法考虑某些内容,只需将其添加为功能即可。如果大多数算法无法预测它,它将被忽略。
关于r - 机器学习公式中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18672245/