tensorflow.js在检查输入时获取错误:预期density_Dense1_input具有3个维度。但是有形状的阵列

斯蒂芬·科蒂克(Stepan Kotyk)

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

这个简单的代码

    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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档