示例#1
0
	def recommend(self, u, q):
		self.average_weights = self._load_matrix(WEIGHTS_FILE)
		self.average_bv 	 = self._load_matrix(VISIBLE_BIAS_FILE)
		self.average_bh		 = self._load_matrix(HIDDEN_BIAS_FILE)

		#x is the input (the user we are predicting for), w is the weights of just the items rated by user x and bv is 
		# just the visible biases of the corresponding to the items rated by the user
		x, w, bv 		 = self._ratings_to_softmax_units(self.dataset.training_X[:,u], q)
		#bh is the same size for every user because they correspond to the hidden units
		bh = self.average_bh

		#Returns the new index for the query q. The index changes because we reomved all unrated items.
		#The units for the query are changed to 5 -1s as a placeholder 
		x, new_q  = self._new_index(x)
		results = []

		#For each rating 1-5 we sample the RBM with the corresponding input.
		for r in range(RATING_MAX):
			x = self._set_rating(x, new_q, (r+1))
			rbm = RBM(hidden_layer_n=HIDDEN_LAYER_N,iterations=ITERATIONS,dataset=x, training=False)
			sample = rbm.run_prediction(x, w, bh, bv)
			results.append(sample)

		#Get the expected output from each of the probablitlies of each input 
		probs = self._get_probabilities(results, new_q)
		prediction = self._expected_value(self.softmax(probs))

		print("Prediction: user " + str(u) + " will give movie: " + str(q) + " rating: " + str(prediction))