【发布时间】:2018-08-18 08:07:43
【问题描述】:
我正在尝试根据该图片构建 LSTM 模型。 我是深度学习的初学者,特别是 RNN 结构,所以我需要你的建议来指导我
所以,我正在处理一个包含 70k 用户和 12k 动画的数据框,我的数据框包含:
用户标识
用户评分
动漫ID
流派:与动漫相关的标签列表,例如:动作、喜剧、学校...等。
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)
【问题讨论】:
标签: python keras deep-learning lstm recommendation-engine