lstm 不同的输入和输出形状
lstm different input and output shape
输入形状
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]
输入形状
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]