Skip to content

Commit

Permalink
add rmse metrics & fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
orbxball committed Jun 1, 2017
1 parent 3725e3e commit 583f4dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hw6/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def build_deep_model(n_users, n_movies, dim, dropout=0.1):
out = Dropout(dropout)(out)
out = Dense(dim, activation='relu')(out)
out = Dropout(dropout)(out)
out = Dense(1, activation='linear')(out)
out = Dense(1, activation='relu')(out)

model = Model(inputs=[u_input, m_input], outputs=out)
return model
Expand Down
2 changes: 1 addition & 1 deletion hw6/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def main(args):
test_data = pd.read_csv(TEST_CSV, usecols=['UserID', 'MovieID'])
print('{} testing data loaded.'.format(test_data.shape[0]))

trained_model = build_deep_model(max_userid, max_movieid, DIM)
trained_model = build_cf_model(max_userid, max_movieid, DIM)
print('Loading model weights...')
trained_model.load_weights(MODEL_WEIGHTS_FILE)
print('Loading model done!!!')
Expand Down
15 changes: 10 additions & 5 deletions hw6/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import numpy as np
import pandas as pd
from keras import backend as K
from keras.callbacks import Callback, EarlyStopping, ModelCheckpoint
from Model import build_cf_model, build_deep_model, rate

Expand All @@ -11,10 +12,14 @@ def parse_args():
parser = argparse.ArgumentParser(description='HW6: Matrix Factorization')
parser.add_argument('train', type=str)
parser.add_argument('test', type=str)
parser.add_argument('--dim', type=str, default=120)
parser.add_argument('--dim', type=int, default=120)
return parser.parse_args()


def rmse(y_true, y_pred):
y_pred = K.clip(y_pred, 1., 5.)
return K.sqrt(K.mean(K.square((y_true - y_pred))))

def main(args):
ratings = pd.read_csv(args.train,
usecols=['UserID', 'MovieID', 'Rating'])
Expand All @@ -39,12 +44,12 @@ def main(args):
Ratings = ratings['Rating'].values
print('Ratings: {}, shape = {}'.format(Ratings, Ratings.shape))

model = build_deep_model(max_userid, max_movieid, DIM)
model.compile(loss='mse', optimizer='adamax')
model = build_cf_model(max_userid, max_movieid, DIM)
model.compile(loss='mse', optimizer='adamax', metrics=[rmse])

callbacks = [EarlyStopping('val_loss', patience=2),
callbacks = [EarlyStopping('val_rmse', patience=2),
ModelCheckpoint(MODEL_WEIGHTS_FILE, save_best_only=True)]
history = model.fit([Users, Movies], Ratings, epochs=100, validation_split=.1, verbose=1, callbacks=callbacks)
history = model.fit([Users, Movies], Ratings, epochs=100, batch_size=256, validation_split=.1, verbose=1, callbacks=callbacks)


if __name__ == '__main__':
Expand Down

0 comments on commit 583f4dc

Please sign in to comment.