我正在尝试更改官方lstm-text-generation的tfjs-examples示例中使用的模型以输出其隐藏状态。为此,我首先将tf.sequential
的{{1}}中tf.model
的用法更改为createModel
。
文件model.js
的原始行37-47
model.js
同一文件// (...)
const model = tf.sequential();
for (let i = 0; i < lstmLayerSizes.length; ++i) {
const lstmLayerSize = lstmLayerSizes[i];
model.add(tf.layers.lstm({
units: lstmLayerSize,
returnSequences: i < lstmLayerSizes.length - 1,
inputShape: i === 0 ? [sampleLen, charSetSize] : undefined
}));
}
model.add(
tf.layers.dense({units: charSetSize, activation: 'softmax'}));
// (...)
的自定义行。
model.js
据我了解,我需要在lstm层选项中添加// (...)
// This replaces `inputShape` of the first LSTM layer
const inputs = tf.input({ shape: [sampleLen, charSetSize] });
let outputs = inputs;
for (let i = 0; i < lstmLayerSizes.length; ++i) {
const lstmLayerSize = lstmLayerSizes[i];
const layer = tf.layers.lstm({
units: lstmLayerSize,
returnSequences: i < lstmLayerSizes.length - 1,
});
outputs = layer.apply(outputs);
}
outputs = tf.layers
.dense({ units: charSetSize, activation: 'softmax' })
.apply(outputs);
const model = tf.model({ inputs, outputs });
// (...)
才能实现目标。
returnState: true
但是,这不适用于最后一个模型层,因为它仅期望一个输入张量,但是将// (...)
// Inside of the for loop
const layer = tf.layers.lstm({
units: lstmLayerSize,
returnState: true, // <-- Here
returnSequences: i < lstmLayerSizes.length - 1,
});
// (...)
设置为returnState
会将lstm层更改为输出3个张量true
。 / p>
我不确定如何实现为[output, hiddenState, cellState]
softmax
保留密集层并仍然打印隐藏状态的目标。
我不清楚的另一件事是,在训练模型时,我可能还需要调整expects target data as second argument y
的activation
方法的输出。
非常感谢您的帮助。