Giter Site home page Giter Site logo

Comments (2)

mindis avatar mindis commented on September 14, 2024 4

based on https://stackoverflow.com/questions/43160181/keras-merge-layer-warning

"""
Triplet loss network example for recommenders
"""

from future import print_function

import numpy as np

from keras import backend as K
from keras.models import Model
from keras.layers import Embedding, Flatten, Input, Lambda
from keras.optimizers import Adam
import data
import metrics

def identity_loss(y_true, y_pred):

return K.mean(y_pred - 0 * y_true)

def bpr_triplet_loss(X):

positive_item_latent, negative_item_latent, user_latent = X

# BPR loss
loss = 1.0 - K.sigmoid(
    K.sum(user_latent * positive_item_latent, axis=-1, keepdims=True) -
    K.sum(user_latent * negative_item_latent, axis=-1, keepdims=True))

return loss

def build_model(num_users, num_items, latent_dim):

positive_item_input = Input((1, ), name='positive_item_input')
negative_item_input = Input((1, ), name='negative_item_input')

# Shared embedding layer for positive and negative items
item_embedding_layer = Embedding(
    num_items, latent_dim, name='item_embedding', input_length=1)

user_input = Input((1, ), name='user_input')

positive_item_embedding = Flatten()(item_embedding_layer(
    positive_item_input))
negative_item_embedding = Flatten()(item_embedding_layer(
    negative_item_input))
user_embedding = Flatten()(Embedding(
    num_users, latent_dim, name='user_embedding', input_length=1)(
        user_input))

def out_shape(shapes):
    return shapes[0]

loss = Lambda(bpr_triplet_loss, output_shape=out_shape)([positive_item_embedding, negative_item_embedding,user_embedding])

model = Model(
    input=[positive_item_input, negative_item_input, user_input],
    output=loss)
model.compile(loss=identity_loss, optimizer=Adam())

return model

from triplet_recommendations_keras.

myeonghak avatar myeonghak commented on September 14, 2024 1

for those who are encountering this issue,

> TypeError: ('Keyword argument not understood:', 'input')

the solution is to remove keywords in Model function.

so the fixed code will be look like this.

model = Model(
[positive_item_input, negative_item_input, user_input],
loss)

source: https://stackoverflow.com/questions/60690327/typeerror-keyword-argument-not-understood-inputs

from triplet_recommendations_keras.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.