tensorflow.js 检查输入时出错:预期 dense_Dense1_input 有 3 个维度。但是得到了形状数组

tensorflow.js getting Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape

简单的问题,我确定答案很简单,但我真的很难将模型形状与张量拟合到模型中。

这个简单的代码

    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}
                )

结果出错:检查输入时出错:预期 dense_Dense1_input 有 3 个维度。但是得到了形状为 2,9

的数组

尝试了多种重塑、切片等方法,但都没有成功。谁能告诉我到底哪里出了问题?

model.fit 至少有两个参数 x, y,它们要么是张量,要么是张量数组。配置对象是第三个参数。

此外,作为参数传递给 model.fit 的特征 (tensorfeature) 张量应该比模型的 inputShape 高一维。由于 tensorfeature.shape 被用作 inputShape,如果我们想用 tensorfeature 训练模型,它的维度应该被扩展。可以使用 reshapeexpandDims.

来完成
model.fit(tensorfeature.expandDims(0))
// or possibly
model.fit(tensorfeature.reshape([1, ...tensorfeature.shape])

已经讨论了模型和训练数据之间的这种形状不匹配 and there