0
$\begingroup$

I have a data of the following format:

user_id time item_type item score
 1  1    a         1   1
 1  2    a         5   0
 1  3    b         2   1
 2  1    b        30   1
 2  2    a        52   0
 ...

I want to train a MLP network with embeddings for users and items in one model. But for the item embedding, I want to have item embedding for type "a" and another type embedding for type "b". one item can be of different types for different users.

But do not know how to subset the input in the model. I have a user_id data, item_id data, score _data with the following code:

user = Input(shape = [1])
item = Input(shape = [1])

# separate the item type
item_a = tf.gather(item, train.loc[train['item_type'] == "a"].index.values)
item_b = tf.gather(item, train.loc[train['item_type'] != "a"].index.values)  

user_embedd = Embedding(input_dim = n_users,  
                          output_dim = 20)(user)

item_embed_a = Embedding(input_dim = n_items,                        
               output_dim = 20)(item_a)

item_embed_b = Embedding(input_dim = n_items,                      
               output_dim = 20)(item_b)


user_vec = Flatten()(user_embed)
item_vec_a = Flatten()(item_embed_a)
item_vec_b = Flatten()(item_embed_b)

vector = Concatenate()([user_vec, item_vec_a, item_vec_b])

x_MLP=Dense(100,activation='relu')(vector)

x = Dense(1, activation = 'sigmoid')(x_MLP)
model = Model(inputs = [user, item_rec, item_rand], outputs = x)

But when I compile this model, I got the following error:

A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 20), (30000, 20), (50000, 20)]

I tried to also have three inputs in the model (user, item_a, item_b), but I will get another error:

  All input arrays (x) should have the same number of samples. Got array shapes: [(80000, 1), (30000, 1), (50000, 1)]

So, I am wondering how I can have separate processing for different inputs of the data and combine them back in the same keras model.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.