【问题标题】:Error when checking input: expected dense_Dense5_input to have 4 dimension(s). but got array with shape 5,2,5检查输入时出错:预期 dense_Dense5_input 有 4 个维度。但得到了形状为 5,2,5 的数组
【发布时间】:2018-10-13 19:52:00
【问题描述】:

我正在学习 tensorflow.js,我正在尝试创建一个模型,该模型可以根据他们的“玩家”预测 2 个“团队”之间随机比赛/游戏的获胜者。

const rawMatches = [
  {
    t1: [2, 99, 3, 5, 7],
    t2: [4, 75, 48, 23, 6],
    winner: 0
  },
  {
    t1: [2, 99, 48, 5, 7],
    t2: [4, 75, 3, 23, 6],
    winner: 1
  },
  {
    t1: [2, 83, 3, 4, 23],
    t2: [4, 75, 58, 25, 78],
    winner: 0
  },
  {
    t1: [26, 77, 11, 5, 7],
    t2: [3, 43, 48, 23, 9],
    winner: 1
  },
  {
    t1: [2, 99, 3, 5, 7],
    t2: [6, 65, 28, 23, 6],
    winner: 0
  }
];

const train = async () => {
  //   [
  //     [[2, 99, 3, 5, 7], [4, 75, 48, 23, 6]],
  //     [[2, 99, 48, 5, 7], [4, 75, 3, 23, 6]],
  //     [[2, 99, 3, 5, 7], [4, 75, 48, 23, 6]]
  //   ];
  const xs = tf.tensor3d(
    rawMatches.map((match, index) => [match.t1, match.t2])
  );

  //   [[1, 0], [0, 1], [1, 0]];
  const labelsTensor = tf.tensor1d(
    rawMatches.map(match => (match.winner === 1 ? 1 : 0)),
    "int32"
  );

  const ys = tf.oneHot(labelsTensor, 2);

  xs.print();
  ys.print();

  let model = tf.sequential();
  const hiddenLayer = tf.layers.dense({
    units: 15,
    activation: "sigmoid",
    inputShape: [5, 2, 5]
  });
  const outputLayer = tf.layers.dense({
    units: 2,
    activation: "softmax"
  });
  model.add(hiddenLayer);
  model.add(outputLayer);

  const optimizer = tf.train.sgd(0.2);

  model.compile({
    optimizer,
    loss: "categoricalCrossentropy"
  });

  model.fit(xs, ys, { epochs: 1 });
};

train();
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.0"> </script>
  </head>

  <body>
  </body>
</html>

尝试拟合模型后会出现此错误:

Error when checking input: expected dense_Dense11_input to have 4 dimension(s). but got array with shape 5,2,5

包含完整代码的代码沙箱:https://codesandbox.io/s/kr37m63w7

【问题讨论】:

    标签: javascript tensorflow machine-learning tensorflow.js


    【解决方案1】:

    这个模型有两个问题:

    首先将输入 x 的维度传递给方法fitxs 应该比第一个 inputShape 高一个维度。因为xs 是一个包含inputShape 形状数据的数组,所以inputShape 应该是[2, 5]

    第二,由于输入和输出的维度不匹配,需要使用tf.flatten改变数据的维度。两个维度不匹配,因为输入数据形状为[2, 5] (size = 2),而输出数据形状为[2] (size = 1)

    const rawMatches = [
      {
        t1: [2, 99, 3, 5, 7],
        t2: [4, 75, 48, 23, 6],
        winner: 0
      },
      {
        t1: [2, 99, 48, 5, 7],
        t2: [4, 75, 3, 23, 6],
        winner: 1
      },
      {
        t1: [2, 83, 3, 4, 23],
        t2: [4, 75, 58, 25, 78],
        winner: 0
      },
      {
        t1: [26, 77, 11, 5, 7],
        t2: [3, 43, 48, 23, 9],
        winner: 1
      },
      {
        t1: [2, 99, 3, 5, 7],
        t2: [6, 65, 28, 23, 6],
        winner: 0
      }
    ];
    
    const train = () => {
      const xs = tf.tensor3d(
        rawMatches.map((match, index) => [match.t1, match.t2])
      );
      const labelsTensor = tf.tensor1d(
        rawMatches.map(match => (match.winner === 1 ? 1 : 0)),
        "int32"
      );
    
      const ys = tf.oneHot(labelsTensor, 2);
    
      xs.print();
      ys.print();
    
      let model = tf.sequential();
      const hiddenLayer = tf.layers.dense({
        units: 15,
        activation: "sigmoid",
        inputShape: [2, 5]
      });
      const outputLayer = tf.layers.dense({
        units: 2,
        activation: "softmax"
      });
      model.add(hiddenLayer);
      model.add(tf.layers.flatten())
      model.add(outputLayer);
    
      const optimizer = tf.train.sgd(0.2);
    
      model.compile({
        optimizer,
        loss: "categoricalCrossentropy"
      });
    
      model.fit(xs, ys, { epochs: 1 });
    };
    
    train();
    <html>
      <head>
        <!-- Load TensorFlow.js -->
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.0"> </script>
      </head>
    
      <body>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2023-03-04
      • 2019-08-25
      • 2019-09-03
      • 2018-01-09
      • 2020-06-06
      • 2021-11-24
      • 2018-11-09
      • 2020-03-03
      相关资源
      最近更新 更多