<jupyter_start><jupyter_text>Lesson 4 WM  To Do- set up notebook- set up data- build model- train model  Set up notebook<jupyter_code>from theano.sandbox import cuda
%matplotlib inline
import utils; reload(utils)
from utils import *
from __future__ import division, print_function
os.getcwd()
#path = 'data/ml-small/'
path = 'data/ml-20m/'
model_path = path + 'models/'
if not os.path.exists(path): os.mkdir(path)
if not os.path.exists(model_path): os.mkdir(model_path)
batch_size = 128<jupyter_output><empty_output><jupyter_text>Set up data<jupyter_code>ratings = pd.read_csv(path + 'ratings.csv')
ratings.head()
len(ratings)
movie_names = pd.read_csv(path + 'movies.csv')
movie_names.head()
len(movie_names)
n_users = ratings.userId.nunique()
u_users = ratings.userId.unique()
userid2idx = {u:idx for idx,u in enumerate(u_users)}

n_movies = ratings.movieId.nunique()
u_movies = ratings.movieId.unique()
movieid2idx = {m:idx for idx,m in enumerate(u_movies)}
n_users, n_movies
ratings.userId = ratings.userId.apply(lambda x: userid2idx[x])
ratings.movieId = ratings.movieId.apply(lambda x: movieid2idx[x])
user_min, user_max, movie_min, movie_max = (ratings.userId.min(), ratings.userId.max(),
                                           ratings.movieId.min(), ratings.movieId.max())
user_min, user_max, movie_min, movie_max
np.random.seed = 42
mask = np.random.rand(len(ratings)) < 0.8
train = ratings[mask]
valid = ratings[~mask]
len(train), len(valid)<jupyter_output><empty_output><jupyter_text>Build model<jupyter_code>user_inp = Input(shape=(1,))
movie_inp = Input(shape=(1,))
n_factors = 100

u_emb = Embedding(n_users, n_factors,input_length=1,W_regularizer=l2(1e-2))(user_inp)
m_emb = Embedding(n_movies, n_factors,input_length=1,W_regularizer=l2(1e-2))(movie_inp)
# where is the bias accounted for?
x = merge([u_emb, m_emb], mode='concat')
x = Flatten()(x)
x = Dropout(0.2)(x)
x = Dense(150, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(100, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(50, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(10, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(1, activation='relu')(x)
model = Model(input=[user_inp, movie_inp],output=x)
model.summary()<jupyter_output>____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_11 (InputLayer)            (None, 1)             0                                            
____________________________________________________________________________________________________
input_12 (InputLayer)            (None, 1)             0                                            
____________________________________________________________________________________________________
embedding_11 (Embedding)         (None, 1, 100)        13849300    input_11[0][0]                   
____________________________________________________________________________________________________
embedding_12 (Embedding)         (None, 1, 100)        2674400     input_12[0][0]          [...]<jupyter_text>Train model<jupyter_code>model.compile(optimizer=Adam(lr=1e-4),loss='mse')
model.fit([train.userId, train.movieId], train.rating, batch_size=batch_size, nb_epoch=1,
         validation_data=([valid.userId, valid.movieId], valid.rating))
model.optimizer.lr.set_value(1e-3)
model.fit([train.userId, train.movieId], train.rating, batch_size=batch_size, nb_epoch=1,
         validation_data=([valid.userId, valid.movieId], valid.rating))
model.optimizer.lr.set_value(1e-4)
model.fit([train.userId, train.movieId], train.rating, batch_size=batch_size, nb_epoch=1,
         validation_data=([valid.userId, valid.movieId], valid.rating))
model.optimizer.lr.set_value(1e-5)
model.fit([train.userId, train.movieId], train.rating, batch_size=batch_size, nb_epoch=1,
         validation_data=([valid.userId, valid.movieId], valid.rating))
model.save_weights(model_path + 'wm_nn.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15225246928450975, 'fraction_numerical': 0.025054203806311733, 'mean_word_length': 4.679890560875513, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '639296', 'license': 'Apache-2.0', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1355, 'n_words': 239, 'path': 'deeplearning1/nbs/lesson4_wm.ipynb', 'repo_name': 'wminshew/courses', 'size': '4151'}