简单的问题和确定的答案很简单,但是我很难将模型形状与张量拟合到模型中。
这个简单的代码
let tf = require('@tensorflow/tfjs-node');
let features = {
x: [1,2,3,4,5,6,7,8,9],
y: [1,2,3,4,5,6,7,8,9]
}
let tensorfeature = tf.tensor2d(Object.values(features))
console.log(tensorfeature.shape)
const model = tf.sequential();
model.add(tf.layers.dense(
{
inputShape: tensorfeature.shape,
units: 1
}
))
const optimizer = tf.train.sgd(0.005);
model.compile({optimizer: optimizer, loss: 'meanAbsoluteError'});
model.fit(tensorfeature,
{epochs: 5}
)
结果为错误:检查输入时出错:预期density_Dense1_input具有3个维度。但是得到了形状为2,9的数组
尝试了多种方法,但都没有重塑,切片等效果。有人可以指出我到底有什么问题吗?
model.fit
取至少两个参数x,y,它们是张量或张量的数组。config对象是第三个参数。
此外,tensorfeature
作为参数传递给model.fit的feature()张量应比inputShape
模型高一维。由于tensorfeature.shape
被用作inputShape
,如果我们要训练tensorfeature
其尺寸应扩展的模型。可以使用reshape
或来完成expandDims
。
model.fit(tensorfeature.expandDims(0))
// or possibly
model.fit(tensorfeature.reshape([1, ...tensorfeature.shape])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句