我正在尝试手动设置具有1个输入节点(加上偏置节点)和隐藏层中2个节点的网络的权重。我怎么能这样做?
入门代码:
library(keras)
model <- keras_model_sequential()
wts = list(matrix(c(1, 1), ncol=1), matrix(c(1, 1), ncol=1))
model <- layer_dense(
object = model, input_shape = 1L, use_bias = TRUE, units = 2L, activation = 'sigmoid',
weights = wts
)
这给出了“ValueError:图层权重形状(1,2)与提供的重量形状(2,1)不兼容”
答案 0 :(得分:0)
使这项工作的诀窍似乎是使用数组来指定偏差权重。
model <- keras_model_sequential()
wts = list(matrix(c(1, 1), nrow=1), array(c(1, 1)))
model <- layer_dense(
object = model, input_shape = 1L, use_bias = TRUE, units = 2L, activation = 'sigmoid', weights = wts
)
get_weights(model)
[[1]]
[,1] [,2]
[1,] 1 1
[[2]]
[1] 1 1