- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 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>
尝试拟合模型后出现此错误:
检查输入时出错:预期 dense_Dense11_input 有 4 个维度。但是得到了形状为 5,2,5 的数组
具有完整代码的代码沙箱:https://codesandbox.io/s/kr37m63w7
最佳答案
这个模型有两个问题:
首先传递给方法 fit
的输入 x 的维度。 xs
应该比第一个 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>
关于javascript - 检查输入 : expected dense_Dense5_input to have 4 dimension(s). 时出错,但得到形状为 5、2、5 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52796751/
我使用 Angular 2 CLI 创建了一个 Angular 2 项目并使用了 ng build --prod 命令并且没有出现任何错误。 当我将 dist 文件夹上传到我的网站时,应用程序加载时出
这是我正在尝试的代码:http://jsfiddle.net/sbrsK/10/ 在jsfiddle中运行正常,没有报错 尝试通过本地网络服务器在我的计算机上运行相同的程序是行不通的。正在加载以下文件
我是一名优秀的程序员,十分优秀!