比较LSTM结构

时间:2018-08-18 08:07:44

标签: python keras deep-learning lstm recommendation-engine

我正在尝试根据该图片构建LSTM模型。 我是使用RNN结构进行深度学习的初学者,所以我需要您的建议来引导我

LSTM Structure

因此,为此,我要处理70k用户和12k动漫的数据框,我的数据框包含:

  • 用户ID

  • 用户评分

  • 动漫ID

  • genre:与动漫相关的标签列表,例如动作,喜剧,学校等。

  • users_tags:由于tfifd方法和一些与用户相关的文本数据,我为唯一用户创建了15个唯一标签的列表

我的数据框看起来像:

    anime_id    user_id user_rating name    tags    genre
0   1   234 9.0 Cowboy Bebop    drama , fi , mal action , military , sci fi , ...   Action, Adventure, Comedy, Drama, Sci-Fi, Space
1   1   382 10.0    Cowboy Bebop    life , shiki , tv , thriller , movie short , c...   Action, Adventure, Comedy, Drama, Sci-Fi, Space
2   1   160 9.0 Cowboy Bebop    fantasy , action , supernatural , tv , mystery...   Action, Adventure, Comedy, Drama, Sci-Fi, Space
3   1   341 8.0 Cowboy Bebop    action , school , romance , new , short , mal ...   Action, Adventure, Comedy, Drama, Sci-Fi, Space
4   1   490 9.0 Cowboy Bebop    mal adventure , movie short , school , strange...   Action, Adventure, Comedy, Drama, Sci-Fi, 

这里是我用于模型的参数:

#parameters
users = interactions_full_df.user_id.unique()
animes = interactions_full_df.anime_id.unique()
animes_tags = " ".join(interactions_full_df["genre"].unique()).split(",")
n_animes_tags = len(animes_tags)
n_users = len(users)
n_animes =  len(animes)
n_users_tags = 15

我为嵌入层的“潜在暗淡”添加了100。

在这里,我尝试构建此模型。你能说我是否做对了吗?

    """ The lstm cell is the concatenation of 3 things : 
--> 1.0  Anime Embedding Vector
--> 2.0 Average of : 
        --> 2.1 Tags embedding vectors associated with the current anime
        --> 2.2 Tags embedding vectors associated with the next anime in a sequence
"""
# 1.0
animes_input = Input(shape=[1],name='Anime')
animes_embedding = Embedding(n_animes + 1, 
                                 latent_dim, 
                                 name='Animes-Embedding')(animes_input)

""" I suppose we need Users embedding to find what's anime chosen by users ??"""
Users_input = Input(shape=[1],name='Users')
Users_embedding = Embedding(n_users + 1, 
                                 latent_dim, 
                                 name='Users-Embeddings')(Users_input)
#2.0
    # 2.1
""" Anime Tags """
animes_tags_input = Input(shape=[1],name='anime_tags')
tags_embedding = Embedding(n_animes_tags + 1, 
                                 latent_dim, 
                                 name='Animes-Tags-embedding')(animes_tags_input)

#2.2 : tags of future anime in a sequence ???
#my input will be a padded sequence of tags used as a string object <<<<<---- 
inp_shape = max_sequence_len - 1
input_len = Input(shape=[inp_shape], name = "future_tags")
sequence_tags_embeddings = Embedding(tags_total_words, latent_dim)(input_len)
sequence_lstm_cells = LSTM(30)(sequence_tags_embeddings)
future_tags_embedding = Dense(latent_dim, activation='softmax')(sequence_lstm_cells) #???????????? i'm not sure at all 
# then average them
averaged_tags = average([tags_embedding, future_tags_embedding])
#then we need to concatenate all of them
merged_cell =  merge([averaged_tags, animes_embedding, Users_embedding])
# My lstm cells is ready : the structure seems to be an Many to One (may be i'm wrong ?)
n_neurons = 100
lstm_cell = LSTM(30, input_shape=(10, 1))(merged_cell)
result = Dense(1, activation='softmax', name = "Recommendation")(lstm_cell)

LSTM_MODEL = Model([animes_input, animes_tags_input, Users_input, input_len], result)
LSTM_MODEL.compile(loss='categorical_crossentropy',
              optimizer='rmsprop')
LSTM_MODEL.summary()

对于“未来标签”部分,我使用诸如此类的填充标签序列:

def get_sequence_of_tokens(corpus):
    ## tokenization
    tokenizer.fit_on_texts(corpus)
    total_words = len(tokenizer.word_index) + 1

    ## convert data to sequence of tokens 
    input_sequences = []
    for line in corpus:
        token_list = tokenizer.texts_to_sequences([line])[0]
        for i in range(1, len(token_list)):
            n_gram_sequence = token_list[:i+1]
            input_sequences.append(n_gram_sequence)
    return input_sequences, total_words

def generate_padded_sequences(input_sequences, input_total_words):
    max_sequence_len = max([len(x) for x in tqdm(input_sequences)])
    input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))
    predictors, label = input_sequences[:,:-1],input_sequences[:,-1]
    label = ku.to_categorical(label, num_classes=input_total_words)
    return predictors, label, max_sequence_len

print("create list ..")
train_tags_anime_list = [get_tags_anime(anime_id) for anime_id in tqdm(train["anime_id"])]
test_tags_anime_list = [get_tags_anime(anime_id) for anime_id in tqdm(valid["anime_id"])]
print("cleaning ...")
train_tags_corpus = [clean_text(x) for x in tqdm(train_tags_anime_list)]
valid_tags_corpus = [clean_text(x) for x in tqdm(test_tags_anime_list)]
print("tokenization ..")
train_tags_inp_sequences, train_tags_total_words = get_sequence_of_tokens(train_tags_corpus)
valid_tags_inp_sequences, valid_tags_total_words = get_sequence_of_tokens(valid_tags_corpus)
print("padd sequence")
train_tags_predictors, train_tags_label, train_max_sequence_len = generate_padded_sequences(train_tags_inp_sequences, train_tags_total_words)
valid_tags_predictors, valid_tags_label, valid_max_sequence_len = generate_padded_sequences(valid_tags_inp_sequences, valid_tags_total_words)

1 个答案:

答案 0 :(得分:0)

您想构建一个具有多个功能(您通常称其为参数的功能)的堆叠LSTM 网络,这在https://machinelearningmastery.com/stacked-long-short-term-memory-networks/https://machinelearningmastery.com/use-features-lstm-networks-time-series-forecasting/和{{ 3}}

RNN和LSTM仅能处理顺序数据,但是可以通过具有多个维度的特征向量来扩展(如{{3中的答案所述,您的参数集合}})

2层中的6个LSTM单元的显示结构是一个堆叠的LSTM网络,具有2层feature_dim = data_dim=6 (or 7)(您的参数/特征数)和timesteps=3(2层,每层3个单元) ),参见 https://datascience.stackexchange.com/questions/17024/rnns-with-multiple-features 中的用于序列分类的堆叠LSTM 和用于keras代码的https://datascience.stackexchange.com/questions/17024/rnns-with-multiple-features部分。

设置准确的输入形状至关重要,请参考 https://keras.io/getting-started/sequential-model-guide/ ,您的网络是多对多情况。 传递给LSTM的输入的形状应为(num_samples,timesteps,data_dim)的形式,其中data_dim是特征向量或参数的向量

嵌入层适用于一键编码,请参见How to stack multiple lstm in keras?以获得keras代码,请参见Understanding Keras LSTMshttps://towardsdatascience.com/deep-learning-4-embedding-layers-f9a02d55ac12,也许您也可以使用简单的 label编码({ {3}},https://towardsdatascience.com/deep-learning-4-embedding-layers-f9a02d55ac12