使用 Tensorflow.js 学习 XOR
Learning XOR using Tensorflow.js
您好,我正在使用 Tensorflow.js 创建我的第一个神经网络。
我想使用点 (0,0)、(0,1)、(1,0)、(1,1) 和标签 0、1、1、0 作为我的神经网络的输入。我尝试了以下方式:
async function runModel() {
// Build and compile model.
const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [2]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1], [0]], [2,1]);
const ys = tf.tensor2d([[1]], [1, 1]);
// Train model with fit().
await model.fit(xs, ys, {epochs: 10});
// Run inference with predict().
model.predict(tf.tensor2d([[0], [1]], [2, 1])).print();
}
runModel()
我遇到了错误:
Uncaught (in promise) Error: Error when checking input: expected
dense_Dense1_input to have shape [,2], but got array with shape [2,1].
我尝试使用所有参数,但我不明白(即使有文档)如何成功。
如前所述 and ,当模型预期的形状与训练数据的形状不匹配时会抛出此错误。
expected dense_Dense1_input to have shape [,2], but got array with shape [2,1]
抛出的错误很有意义,足以帮助解决问题。第一层期望形状为 [,2]
的张量,因为 inputShape 为 [2]
。但是 xs
具有 [2, 1]
的形状,它应该具有 [1, 2]
.
的形状
在模型中,最后一层将 return 2 个值,而实际上它应该只有一个(异或运算仅输出一个值)。因此,而不是 units: 2
,它应该是 units: 1
。这意味着 ys
的形状应该是 [,1]
。 ys
的形状已经是模型应该具有的形状 - 所以那里没有变化。
用于预测的张量形状应与模型输入形状匹配,即[, 2]
经过上面的改动,变成了下面这样:
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [2]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1, 0]]);
const ys = tf.tensor2d([[1]], [1, 1]);
// Train model with fit().
await model.fit(xs, ys, {epochs: 10});
// Run inference with predict().
model.predict(tf.tensor([[0, 1]], [1, 2])).print()
您好,我正在使用 Tensorflow.js 创建我的第一个神经网络。
我想使用点 (0,0)、(0,1)、(1,0)、(1,1) 和标签 0、1、1、0 作为我的神经网络的输入。我尝试了以下方式:
async function runModel() {
// Build and compile model.
const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [2]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1], [0]], [2,1]);
const ys = tf.tensor2d([[1]], [1, 1]);
// Train model with fit().
await model.fit(xs, ys, {epochs: 10});
// Run inference with predict().
model.predict(tf.tensor2d([[0], [1]], [2, 1])).print();
}
runModel()
我遇到了错误:
Uncaught (in promise) Error: Error when checking input: expected dense_Dense1_input to have shape [,2], but got array with shape [2,1].
我尝试使用所有参数,但我不明白(即使有文档)如何成功。
如前所述
expected dense_Dense1_input to have shape [,2], but got array with shape [2,1]
抛出的错误很有意义,足以帮助解决问题。第一层期望形状为 [,2]
的张量,因为 inputShape 为 [2]
。但是 xs
具有 [2, 1]
的形状,它应该具有 [1, 2]
.
在模型中,最后一层将 return 2 个值,而实际上它应该只有一个(异或运算仅输出一个值)。因此,而不是 units: 2
,它应该是 units: 1
。这意味着 ys
的形状应该是 [,1]
。 ys
的形状已经是模型应该具有的形状 - 所以那里没有变化。
用于预测的张量形状应与模型输入形状匹配,即[, 2]
经过上面的改动,变成了下面这样:
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [2]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1, 0]]);
const ys = tf.tensor2d([[1]], [1, 1]);
// Train model with fit().
await model.fit(xs, ys, {epochs: 10});
// Run inference with predict().
model.predict(tf.tensor([[0, 1]], [1, 2])).print()