我正在尝试仅使用正值初始化tensorflow.js中的权重,但似乎我从未给它“正确”的形状。这是我的代码:
let data_size = 500;
let input = [];
let output;
const model = tf.sequential();
for (var i = 0; i < data_size; i++){
input[i] = i;
}
input = tf.tensor2d(input, [data_size, 1]);
output = tf.add(tf.scalar(1), input);
model.add(tf.layers.dense({units: 6, activation: "relu", inputShape: [1], weights: tf.randomUniform([6, 1], 0, 1)}));
model.add(tf.layers.dense({units: 1, activation: "linear"}));
model.compile({loss: "meanSquaredError", optimizer: "adam"});
所以在我的代码中,我要添加的第一层中,我放置了“ weights”参数来选择权重https://js.tensorflow.org/api/0.13.0/#layers.add
的初始化但是即使权重的形状为[6,1],也不会接受。我也尝试过tf.randomUniform([1],0,1),因为它可能是传递给所有权重的单个表达式,但它也不起作用。您如何选择表达式来使用tensorflow.js初始化权重?
答案 0 :(得分:1)
weights
是根据doc的张量数组。通过 A X + B 初始化层。因此,需要提供 A 和 B 张量(其中X是该层的输入)。
let data_size = 500;
let input = [];
let output;
const model = tf.sequential();
for (var i = 0; i < data_size; i++){
input[i] = i;
}
input = tf.tensor2d(input, [data_size, 1]);
output = tf.add(tf.scalar(1), input);
model.add(tf.layers.dense({units: 6, activation: "relu", inputShape: [1], weights: [ tf.randomUniform([1, 6], 0, 1), tf.randomUniform([6], 0, 1)]}));
model.add(tf.layers.dense({units: 1, activation: "linear"}));
model.compile({loss: "meanSquaredError", optimizer: "adam"});
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"> </script>
</head>
<body>
</body>
</html>