Reputation: 25
I have coded this model with tf.keras ,
import tensorflow as tf
from tensorflow import einsum
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Flatten , Dot
from tensorflow.keras.layers import Embedding, Multiply, Dense, Input
from tensorflow.keras import Model
from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import load_model
num_items = 1250
num_users = 1453
emb_size = 32
input_userID = Input(shape=[1], name='user_ID')
input_itemID = Input(shape=[1], name='item_ID')
user_emb_GMF = Embedding(num_users, emb_size, name='user_emb_GMF')(input_userID)
item_emb_GMF = Embedding(num_items, emb_size, name='item_emb_GMF')(input_itemID)
flat_u_GMF = Flatten()(user_emb_GMF)
flat_i_GMF = Flatten()(item_emb_GMF)
interraction_map = einsum('i,j->ij',flat_u_GMF ,flat_i_GMF) # output[i,j] = u[i]*v[j]
layer = Dense(16, activation='relu', name='hidden_layer' )(interraction_map)
out = Dense(1,activation='sigmoid',name='output')(layer)
oncf_model = Model([input_userID, input_itemID], out)
tf.keras.utils.plot_model(oncf_model, show_shapes=True)
and basically i want to get the outer product of user_emb_GMF and item_emb_GMF(which is a matrix), and i get the error :
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1811 try:
-> 1812 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1813 except errors.InvalidArgumentError as e:
InvalidArgumentError: Shape must be rank 1 but is rank 2
for 0th input and equation: i,j->ij for '{{node Einsum_2}} = Einsum[N=2, T=DT_FLOAT, equation="i,j->ij"](flatten_10/Reshape, flatten_11/Reshape)' with input shapes: [?,32], [?,32].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1813 except errors.InvalidArgumentError as e:
1814 # Convert to ValueError for backwards compatibility.
-> 1815 raise ValueError(str(e))
1816
1817 return c_op
ValueError: Shape must be rank 1 but is rank 2
for 0th input and equation: i,j->ij for '{{node Einsum_2}} = Einsum[N=2, T=DT_FLOAT, equation="i,j->ij"](flatten_10/Reshape, flatten_11/Reshape)' with input shapes: [?,32], [?,32].
and i wonder how i can solve the problem
Upvotes: 1
Views: 1190
Reputation: 22031
if the desired output of interraction_map is (num_batch,emb_size,emb_size,1)
you can simply use the keras Dot layer and then adding a dimensionality
in this way, the flattening of the embedding is not needed
num_items = 1250
num_users = 1453
emb_size = 32
input_userID = Input(shape=[1], name='user_ID')
input_itemID = Input(shape=[1], name='item_ID')
user_emb_GMF = Embedding(num_users, emb_size, name='user_emb_GMF')(input_userID)
item_emb_GMF = Embedding(num_items, emb_size, name='item_emb_GMF')(input_itemID)
interraction_map = tf.expand_dims(Dot(axes=1)([user_emb_GMF,item_emb_GMF]), -1)
conv = Conv2D(32, 2, activation='relu', padding="SAME")(interraction_map)
pool = GlobalMaxPool2D()(conv)
layer = Dense(16, activation='relu', name='hidden_layer' )(pool)
out = Dense(1,activation='sigmoid',name='output')(layer)
oncf_model = Model([input_userID, input_itemID], out)
oncf_model.summary()
Upvotes: 1