defcofi_cost_func(X,W,b,Y,R,lambda_):""" Returns the cost for the content-based filtering Args: X (ndarray (num_movies,num_features)): matrix of item features W (ndarray (num_users,num_features)) : matrix of user parameters b (ndarray (1, num_users) : vector of user parameters Y (ndarray (num_movies,num_users) : matrix of user ratings of movies R (ndarray (num_movies,num_users) : matrix, where R(i, j) = 1 if the i-th movies was rated by the j-th user lambda_ (float): regularization parameter Returns: J (float) : Cost """nm,nu=Y.shapeJ=0### START CODE HERE ### forjinrange(nu):w=W[j,:]b_j=b[0,j]foriinrange(nm):x=X[i,:]y=Y[i,j]r=R[i,j]J+=0.5*np.square(r*(np.dot(w,x)+b_j-y))# RegularizationJ+=(lambda_/2)*(np.sum(np.square(W))+np.sum(np.square(X)))### END CODE HERE ### returnJ
# Initialize an optimizeroptimizer=keras.optimizers.Adam(learning_rate=1e-1)# Set the number of iterationsiterations=200# Define the cost functiondefcofiCostFuncV(X,W,b,Ynorm,R,num_users,num_movies,lambda):# The implementation of the cost function should be herepass# Gradient descent loopforiterinrange(iterations):# Use TensorFlow's GradientTape to record the operations used to compute the costwithtf.GradientTape()astape:# Compute the costcost_value=cofiCostFuncV(X,W,b,Ynorm,R,num_users,num_movies,lambda)# Use the gradient tape to automatically retrieve the gradients of the trainable variables with respect to the lossgrads=tape.gradient(cost_value,[X,W,b])# Run one step of gradient descent by updating the value of the variables to minimize the lossoptimizer.apply_gradients(zip(grads,[X,W,b]))
# Create the user neural networkuser_NN=tf.keras.models.Sequential([tf.keras.layers.Dense(256,activation='relu'),tf.keras.layers.Dense(128,activation='relu'),tf.keras.layers.Dense(32)])# Create the item neural networkitem_NN=tf.keras.models.Sequential([tf.keras.layers.Dense(256,activation='relu'),tf.keras.layers.Dense(128,activation='relu'),tf.keras.layers.Dense(32)])
# Create the user input and point to the base networkinput_user=tf.keras.layers.Input(shape=(num_user_features,))vu=user_NN(input_user)vu=tf.linalg.l2_normalize(vu,axis=1)# Create the item input and point to the base networkinput_item=tf.keras.layers.Input(shape=(num_item_features,))vm=item_NN(input_item)vm=tf.linalg.l2_normalize(vm,axis=1)
# Measure the similarity of the two vector outputsoutput=tf.keras.layers.Dot(axes=1)([vu,vm])# Specify the inputs and output of the modelmodel=tf.keras.Model(inputs=[input_user,input_item],outputs=output)# Specify the cost functioncost_fn=tf.keras.losses.MeanSquaredError()
# DQN
Initialize neural network randomly as guess of Q(s, a).
Repeat {
Take actions in the lunar lander. Get (s, a, R(s), s').
Store 10,000 most recent (s, a, R(s), s') tuples.
Train neural network:
Create training set of 10,000 examples using
x = (s, a) and y = R(s) + γ * max_a' Q(s', a')
Train Q_new such that Q_new(s, a) ≈ y.
Set Q = Q_new.
}