-- coding: UTF-8 --

import pandas as pd
from sklearn.model_selection import train_test_split

dataset = pd.read_csv('/www/wwwroot/tensorflow/ratings.csv')
train, test = train_test_split(dataset, test_size=0.2, random_state=42)
n_users = len(dataset.user_id.unique())
n_books = len(dataset.book_id.unique())

from keras.layers import Input, Embedding, Flatten, Dot, Dense
from keras.models import Model

book_input = Input(shape=[1], name="Book-Input")
book_embedding = Embedding(n_books+1, 5, name="Book-Embedding")(book_input)
book_vec = Flatten(name="Flatten-Books")(book_embedding)

user_input = Input(shape=[1], name="User-Input") #不明白定义这个INPUT是啥意思,明明没有输入
user_embedding = Embedding(n_users+1, 5, name="User-Embedding")(user_input)
user_vec = Flatten(name="Flatten-Users")(user_embedding)

prod = Dot(name="Dot-Product", axes=1)([book_vec, user_vec])
input_dim = train.shape[1]
model = Model([user_input, book_input], prod)
#如果要在此处加个全链接层,怎么加
model.add(layers.Dense(1, input_dim=input_dim, activation='relu'))
model.compile('adam', 'mean_squared_error')

history = model.fit([train.user_id, train.book_id], train.rating, epochs=2, verbose=1,validation_split=0.2,shuffle=True)
model.save('regression_model.h5')