keras嵌入具有分类变量的权重查找

时间:2019-01-29 22:17:19

标签: python keras embedding collaborative-filtering

假设我有一个ratings数据框中的用户和项目列表。如果我创建嵌入层item_embeddinguser_embedding,然后获得嵌入层的权重,则嵌入向量如何映射到项/用户ID?他们遵循ID的顺序吗?

import pandas as pd

## minimal example
ratings = {'user': [1000, 10001, 1000], 'item': [115, 112, 115], 'rating': [5, 3, 4]}


## keras model----------------------------------------------------

n_latent_factors_user = 8
n_latent_factors_item = 8

n_users = len(train.user_id.unique())
n_items = len(train.book_id.unique())

## items
item_input= keras.layers.Input(shape=[1],name='Item') ## input 
item_embedding = keras.layers.Embedding(n_items + 1, n_latent_factors_item, name='item-Embedding')(item_input)
item_vec = keras.layers.Flatten(name='Flattenitems')(item_embedding)

## users
user_input = keras.layers.Input(shape=[1],name='User') ## input
user_embedding = keras.layers.Embedding(n_users + 1, n_latent_factors_user,name='User-Embedding')(user_input)
user_vec = keras.layers.Flatten(name='FlattenUsers')(user_embedding)

## concat items and users
concat = keras.layers.concatenate([item_vec, user_vec])

## fully connected
dense_1 = keras.layers.Dense(20,name='FullyConnected', activation='relu')(concat)

## output
result = keras.layers.Dense(1, activation='relu',name='Activation')(dense_1)

## model with input and output
model = keras.Model([user_input, item_input], result)

我的意思是,如果我们按以下方式获得项目嵌入层的权重,那么第一个向量将对应于项目112吗?

## items embedding weights
model.layers[2].get_weights()[0]  ## 2x8

1 个答案:

答案 0 :(得分:0)

您需要确保将用户枚举为0, ..., n_users,并将项目枚举为0, ..., n_items。要获取项目的嵌入内容,您可以

embeddings_items = model.get_layer('item-Embedding').get_weights()[0]

然后embeddings_items[0]为您嵌入项目编号0。