LSTM自动编码器维度降低常数输出



我正在尝试使用LSTM自动编码器将整数的可变大小序列的数据集嵌入固定长度向量中,但是即使序列不同,该模型即使序列不同。

数据集的每个样本如下:

[1,3,4,2,1]

每个序列都是使用一个hot编码编码的:

[[0,1,0,0,0],[0,0,0,1,0],[0,0,0,0,1],[0,0,0,1,0,0],[0,1,0,0,0]]

如果较短的序列零盖应用于单热编码的向量。

[[0,1,0,0,0],[0,0,0,1,0],[0,0,0,0,1],[0,0,0,1,0,0],[0,1,0,0,0],...,[0,0,0,0,0]

最后,我有一个大小的输入矩阵

n_samples x n_integers(n_timesteps(x One_hot_encoding_size(n_features(

我期望的是该模型的输出是大小的矩阵

的矩阵

n_samples x fixed_size(latent_dim(


from keras.utils import Sequence
def to_categorical(sequences, n_categories, max_len):
    categorical_sequences = []
    for s in sequences:
        #ohe = np.full((max_len, n_categories), fill_value=-1 )
        ohe = np.zeros((max_len, n_categories))
        for i, item in enumerate(s):
                ohe[i][item] = 1
        categorical_sequences.append(ohe)
    return np.array(categorical_sequences)
class batch_generator(Sequence):
    def __init__(self, X, batch_size, num_classes, max_len, y=None, prediction_only=False, shuffle=True):
        self.X = X
        self.batch_size = batch_size
        self.num_classes = num_classes
        self.max_len = max_len
        self.y = y
        self.prediction_only = prediction_only
        self.shuffle = shuffle
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return int(np.floor(len(self.X) / self.batch_size))
    def __getitem__(self, index):
        'Generate one batch of data'
        #print("Generating batch with index {}".format(index))
        batch_indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
        return self.__data_generation(batch_indexes)    
    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.indexes = np.arange(len(self.X))
        if(self.shuffle == True):
            np.random.shuffle(self.indexes, )
    def __data_generation(self, batch_indexes):
        'Generates data containing batch_size samples'
        result = None
        batch_X = to_categorical(self.X[batch_indexes], self.num_classes, self.max_len)
        if(self.prediction_only):
            result = batch_X
        else:
            if(self.y is None):
                result = batch_X, batch_X
            else:
                batch_y = self.y[batch_indexes]
                result = batch_X, batch_y
        return result

from keras.layers import Input, RepeatVector, CuDNNGRU
from keras.models import Model
n_timesteps = np.max([x.shape[0] for x in X])
n_features = int(np.max([np.max(x) for x in X]) + 1)
latent_dim = 128
print("N timesteps {}".format(n_timesteps))
print("N features {}".format(n_features))
print("Latent dimension {}".format(latent_dim))
inputs = Input(shape=(n_timesteps, n_features))
encoded = CuDNNGRU(units=latent_dim)(inputs)
decoded = RepeatVector(n=n_timesteps)(encoded)
decoded = CuDNNGRU(units=n_features, return_sequences=True)(decoded) 
autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)
autoencoder.compile(loss='mse', optimizer="adam")
autoencoder.summary()
batch_size = 128
train_generator = batch_generator(X_train, batch_size=batch_size, num_classes=n_features, max_len=n_timesteps)
val_generator = batch_generator(X_val, batch_size=batch_size, num_classes=n_features, max_len=n_timesteps)
history = autoencoder.fit_generator(generator = train_generator,
                                    steps_per_epoch = X_train.shape[0]//batch_size,
                                    epochs = 2,
                                    #callbacks = [early_stopping, model_checkpoint],
                                    validation_data = val_generator,
                                    validation_steps = X_val.shape[0]//batch_size,
                                    #use_multiprocessing = True,
                                    #workers = n_cpu
                                   ) 
X_generator = batch_generator(X, batch_size=batch_size, num_classes=n_features,  max_len=n_timesteps, prediction_only=True ) 
compact_representation64 = encoder.predict_generator(generator=X_generator, steps=X.shape[0]//batch_size, verbose=1)

问题是每个样本都编码为相同的固定长度向量:

样本#1

阵列([ - 0.00898637,0.02220072,-0.0095799,0.00655961,0.00733364,0.00733364, 0.00351852,0.00088661,-0.00060489,-0.00819919,-0.01798768,-0.00819919 -0.02408937,-0.01549,0.00395884,-0.0124888,-0.00321282,-0.0032482, -0.01447861,........................................................................................

样本#100

阵列([ - 0.00898637,0.02220072,-0.0095799,0.00655961,0.00733364,0.00733364, 0.00351852,0.00088661,-0.00060489,-0.00819919,-0.01798768,-0.00819919 -0.02408937,-0.01549,0.00395884,-0.0124888,-0.00321282,-0.0032482, -0.01447861,........................................................................................

它在我看来是您想要的:

inputs = Input(shape=(n_timesteps, n_features))
encoded = CuDNNGRU(units=latent_dim, return_sequences=True)(inputs)
decoded = CuDNNGRU(units=n_features)(decoded)

通过添加RepeatVector层,您可以创建一个恒定序列,该序列被馈送到第二个CuDNNGRU层。因此,恒定输出。

相关内容

  • 没有找到相关文章

最新更新