问题描述
我一直在尝试使用 tfjs 设置一个简单的强化学习示例。但是,在尝试训练模型时,我遇到了以下错误:
Uncaught (in promise) Error: Error when checking target: expected dense_Dense5 to have shape [,1],but got array with shape [3,4]
我建立的模型如下:
const NUM_OUTPUTS = 4;
const model = tf.sequential();
//First hidden Layer,which also defines the input shape of the model
model.add(
tf.layers.dense({
units: LAYER_1_UNITS,batchInputShape: [null,NUM_INPUTS],activation: "relu",})
);
// Second hidden Layer
model.add(tf.layers.dense({ units: LAYER_2_UNITS,activation: "relu" }));
// Third hidden Layer
model.add(tf.layers.dense({ units: LAYER_3_UNITS,activation: "relu" }));
// Fourth hidden Layer
model.add(tf.layers.dense({ units: LAYER_4_UNITS,activation: "relu" }));
// Defining the output Layer of the model
model.add(tf.layers.dense({ units: NUM_OUTPUTS,activation: "relu" }));
model.compile({
optimizer: tf.train.adam(),loss: "sparseCategoricalCrossentropy",metrics: "accuracy",});
训练是通过计算一些示例的 Q 值的函数完成的:
batch.forEach((sample) => {
const { state,nextState,action,reward } = sample;
// We let the model predict the rewards of the current state.
const current_Q: tf.Tensor = <tf.Tensor>model.predict(state);
// We also let the model predict the rewards for the next state,if there was a next state in the
//game.
let future_reward = tf.zeros([NUM_ACTIONS]);
if (nextState) {
future_reward = <Tensor>model.predict(nextState);
}
let totalValue =
reward + discountFactor * future_reward.max().dataSync()[0];
current_Q.bufferSync().set(totalValue,action);
// We can Now push the state to the input collector
x = x.concat(Array.from(state.dataSync()));
// For the labels/outputs,we push the updated Q values
y = y.concat(Array.from(current_Q.dataSync()));
});
await model.fit(
tf.tensor2d(x,[batch.length,NUM_INPUTS]),tf.tensor2d(y,NUM_OUTPUTS]),{
batchSize: batch.length,epochs: 3,}
);
这似乎是为拟合函数提供示例的正确方法,因为在记录模型时,最后一个密集层的形状是正确的:
Log of the shape of dense_Dense5
但是它会导致上面显示的错误,而不是预期的形状 [3,4],它检查形状 [,1]。我真的不明白这个形状突然来自哪里,非常感谢您的帮助!
为了更好地概览,您可以简单地从其 Github 存储库中查看/查看整个项目:
编辑:
提供模型的摘要以及为 y
中的 model.fit(x,y)
提供的张量形状的一些信息:
解决方法
已解决:由于使用错误的损失函数而出现问题。从 categoricalCrossEntropy
移动到 meanSquaredError
修复了输出层形状与批次形状不匹配的问题。