【问题标题】:tensorflow.js 2 dimensional input?tensorflow.js 二维输入?
【发布时间】:2021-01-05 21:35:11
【问题描述】:
const trainingData = tf.tensor3d(fixedData.map(item => 
       
        [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[...],[...],[...]]
))

 model.add(tf.layers.dense({
    inputShape: [4,16],
    activation: "relu",
    units: 4,
  }))
model.compile({
    loss: "meanSquaredError",
    optimizer: tf.train.adam(0.05),
    metrics: ['accuracy']
  })

  model.fit(trainingData, outputData, {epochs: 10})
  .then((history) => {
    // console.log(history)
    model.predict(testingData).print()

      })

错误: (节点:5118)UnhandledPromiseRejectionWarning:错误:检查输入时出错:预期 dense_Dense1_input 具有 2 个维度。但得到了形状为 935,4,16 的数组。

inputShape 可以是二维的吗?

【问题讨论】:

  • trainingData 是一个 3D 张量,所以请改用 tf.tensor2d()
  • 我的代码现在仅在输出具有 [4,4] 形状时才有效。输出的形状是否取决于输入的形状?我的输出应该是 2d 而输入是 3d。

标签: tensorflow multidimensional-array neural-network layer tensorflow.js


【解决方案1】:

您没有提供完整的代码。查看您的标签(输出)数据很重要。我伪造了输出数据以匹配您的单个密集层的输出。 此外,正如 @yudhiesh 在 cmets 中提到的,您的张量只有 2 个维度。我还修复了这个问题,以防你想为每个输入坚持使用 [4,16]。

这是正在运行的代码

const trainingData = tf.tensor3d( 
         [[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
         [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
         [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
         [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]]
)

const output = tf.tensor3d( 
        [[ [1,2,3,4],
         [1,2,3,4],
         [1,2,3,4],
         [1,2,3,4]]]
          )

const model = tf.sequential()

model.add(tf.layers.dense({
    inputShape: [4, 16],
    activation: "relu",
    units: 4
  }))



model.compile({
    loss: "meanSquaredError",
    optimizer: tf.train.adam(0.05),
    metrics: ['accuracy']
  })


model.fit(trainingData,output, {epochs: 2})
  .then((history) => {
    model.predict(trainingData).print()
}).catch((e) => {
  console.log(e.message);
});

【讨论】:

  • 这是输出: const outputData = tf.tensor2d(fixedData.map(item => [ // shape:[ 935, 4 ] // item.position === 1 ? 1 : 0 , // item.position !== 1 ? 1 : 0, item[1][0] === 1 ? 1 : 0, item[1][1] === 1 ? 1 : 0, item[1 ][2] === 1 ? 1 : 0, 项目[1][3] === 1 ? 1 : 0, ]))
  • 我的代码现在仅在输出具有 [4,4] 形状时才有效。输出的形状是否取决于输入的形状?我的输出应该是 2d 而输入是 3d。
  • 您的输入和输出可以是任何东西。我不知道你的输入是不是 3d。我以为您正在定义要训练的批次。所以基本上,如果你有一个形状为 (10,4,16) 的 trainingData,你就有了一批 10 个 4x16 的张量。
猜你喜欢
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2021-10-26
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多