检查输入时出错:期望dense_Dense5_input具有4个维度,但得到的数组形状为5,2,5

我正在学习TensorFlow.js,并且尝试创建一个模型来预测两支“队伍”之间随机比赛/游戏的赢家,基于他们的“玩家”。

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/[email protected]"> </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


回答:

这个模型有两个问题:

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

其次,由于输入和输出的维度不匹配,你需要使用tf.flatten来更改数据的维度。输入数据的形状是[2, 5] (大小 = 2),而输出数据的形状是[2] (大小 = 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/[email protected]"> </script>  </head>  <body>  </body></html>

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注