【发布时间】:2019-07-12 18:48:03
【问题描述】:
这是我的简单可重现代码:
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.models import load_model
import keras
import numpy as np
SEQUENCE_LEN = 45
LATENT_SIZE = 20
VOCAB_SIZE = 100
inputs = keras.layers.Input(shape=(SEQUENCE_LEN, VOCAB_SIZE), name="input")
encoded = keras.layers.Bidirectional(keras.layers.LSTM(LATENT_SIZE), merge_mode="sum", name="encoder_lstm")(inputs)
decoded = keras.layers.RepeatVector(SEQUENCE_LEN, name="repeater")(encoded)
decoded = keras.layers.Bidirectional(keras.layers.LSTM(VOCAB_SIZE, return_sequences=True), merge_mode="sum", name="decoder_lstm")(decoded)
autoencoder = keras.models.Model(inputs, decoded)
autoencoder.compile(optimizer="sgd", loss='mse')
autoencoder.summary()
x = np.random.randint(0, 90, size=(10, SEQUENCE_LEN,VOCAB_SIZE))
y = np.random.normal(size=(10, SEQUENCE_LEN, VOCAB_SIZE))
NUM_EPOCHS = 1
checkpoint = ModelCheckpoint(filepath='checkpoint/{epoch}.hdf5')
history = autoencoder.fit(x, y, epochs=NUM_EPOCHS,callbacks=[checkpoint])
这是我查看编码器层权重的代码:
for epoch in range(1, NUM_EPOCHS + 1):
file_name = "checkpoint/" + str(epoch) + ".hdf5"
lstm_autoencoder = load_model(file_name)
encoder = Model(lstm_autoencoder.input, lstm_autoencoder.get_layer('encoder_lstm').output)
print(encoder.output_shape[1])
weights = encoder.get_weights()[0]
print(weights.shape)
for idx in range(encoder.output_shape[1]):
token_idx = np.argsort(weights[:, idx])[::-1]
这里print(encoder.output_shape) 是(None,20) 而print(weights.shape) 是(100, 80)。
我知道get_weight会在图层之后打印权重过渡。
我没有得到基于这个架构的部分是80。这是什么?
而且,这里的weights 是连接编码器层和解码器的权重吗?我的意思是编码器和解码器之间的连接。
我看过这个问题here。因为它只是简单的密集层,我无法将这个概念与 seq2seq 模型联系起来。
更新1
有什么区别:
encoder.get_weights()[0] 和 encoder.get_weights()[1]?
第一个是(100,80),第二个是(20,80),就像概念上的一样?
感谢任何帮助:)
【问题讨论】:
标签: python tensorflow keras lstm seq2seq