[英]TensorFlow.js: Are those two Tensors equal?
我正在阅读TensorflowJS文档。 在他们的示例代码中,他们说
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
我很困惑,因为他们在这里使用二维数组。 有谁知道为什么?
为了完整起见,这里是完整的代码片段。
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
model.predict(tf.tensor2d([5], [1, 1])).print();
});
这里使用1维数组会不会更简单,因为数组不会使用第二维吗?
const xs = tf.tensor1d([1, 2, 3, 4]);
特征xs
张量是二维张量。 更一般地,特征张量比第一层的inputShape大一维。 添加的维度是批次维度,它指示inputShape的形状元素数量,模型是经过训练的还是预测的。
在示例中,inputShape的大小为[1]
,特征形状需要为[b,1]
形状,因此为二维张量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.