如何使用return_sequence和return_state参数实现Keras的Tensorflow LSTM等价物



在Keras中,通常会写:

model = Sequential()
model.add(LSTM(n, input_shape=(ntimesteps, nfeatures), 
return_sequences=return_sequences, return_state=return_state))

如何在纯Tensorflow 2中模拟return_sequencesreturn_state功能?

Keras是TensorFlow 2的高级API,所以你也可以这样做,唯一的区别是不再导入Keras,而是导入

import tensorflow as tf
print(tf.__version__) # 2.x
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(n, input_shape=(ntimesteps, nfeatures), 
return_sequences=return_sequences, return_state=return_state))

最新更新