输入形状
tf.tensor3d([
[
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
],
[
[0.02, 0.03, 0.04],
[0.02, 0.03, 0.04],
[0.02, 0.03, 0.04],
[0.02, 0.03, 0.04],
[0.01, 0.02, 0.03],
],
[
[0.03, 0.05, 0.06],
[0.03, 0.05, 0.06],
[0.03, 0.05, 0.06],
[0.03, 0.05, 0.06],
[0.01, 0.02, 0.03],
],
]);
输出形状
const ys = tf.tensor3d([
[
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
[0.01, 0.02, 0.03],
],
[
[0.02, 0.03, 0.04],
[0.02, 0.03, 0.04],
[0.02, 0.03, 0.04],
],
[
[-0.03, 0.05, 0.06],
[0.03, -0.05, 0.06],
[0.03, 0.05, -0.06],
],
]);
我正在尝试使用lstm
层来创建预测模型。问题是我只知道如何更改 lstm
层的 units
变量。
我一直在寻找一种转换为 tensor3d
但具有不同行的方法。我只能找到一种方法将其变成一维或二维形状。
model.add(
tf.layers.lstm({
units: 30,
returnSequences: true,
inputShape: [5, 3],
batchInputShape: [3, 3, 3],
})
);
model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({ loss: "meanSquaredError", optimizer: "adam" });
我必须放入哪些层和变量才能将[3,5,3]
的输入转换为[3,3,3]
?
最佳答案
这是可以做什么的示例
const model = tf.sequential();
model.add(
tf.layers.lstm({
units: 30,
returnSequences: true,
inputShape: [5, 3],
batchInputShape: [3, 3, 3],
})
);
model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
model.add(tf.layers.flatten());
// flatten is used so as to be able to change the size of the second dimension using the dense layer
model.add(tf.layers.dense({ units: 15}));
// dense allow to remap the size of the previous layer to a different size
model.add(tf.layers.reshape({targetShape: [5, 3]}))
// reshape to the appropriate shape
model.summary() // will print the shape of all the layers; last layer will be [3, 5, 3]
关于tensorflow - lstm 不同的输入和输出形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71238448/