【发布时间】:2018-04-24 11:20:40
【问题描述】:
我有以下示例代码。我可以通过打印功能在控制台中看到正确的结果。
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.add(tf.layers.dense({units: 4, inputShape: [1]}));
model.add(tf.layers.dense({units: 10, inputShape: [1]}));
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
answer = model.predict(tf.tensor2d([3], [1, 1]));
answer.print()
});
我想做的是将答案放入一个数字变量中,以便我可以在其他地方使用它。我得到的答案是:
Tensor [[4.9999123],]
但我想将 4.9999 放入一个变量中,以便将其四舍五入为 5 并在屏幕上打印(以 html 格式)。
【问题讨论】:
-
任何帮助将不胜感激。