def fit(self, train_set, val_set=None): """Fit the model to observations. Parameters ---------- train_set: :obj:`cornac.data.Dataset`, required User-Item preference data as well as additional modalities. val_set: :obj:`cornac.data.Dataset`, optional, default: None User-Item preference data for model selection purposes (e.g., early stopping). Returns ------- self : object """ Recommender.fit(self, train_set, val_set) X = sp.csc_matrix(self.train_set.matrix) # recover the striplet sparse format from csc sparse matrix X (needed to feed c++) (rid, cid, val) = sp.find(X) val = np.array(val, dtype='float32') rid = np.array(rid, dtype='int32') cid = np.array(cid, dtype='int32') tX = np.concatenate((np.concatenate( ([rid], [cid]), axis=0).T, val.reshape((len(val), 1))), axis=1) del rid, cid, val if self.trainable: map_iid = train_set.iid_list (rid, cid, val) = train_set.item_graph.get_train_triplet(map_iid, map_iid) context_info = np.hstack( (rid.reshape(-1, 1), cid.reshape(-1, 1), val.reshape(-1, 1))) if self.variant == 'c2pf': res = c2pf.c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) elif self.variant == 'tc2pf': res = c2pf.t_c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) elif self.variant == 'rc2pf': res = c2pf.r_c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) else: res = c2pf.c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) self.Theta = sp.csc_matrix(res['Z']).todense() self.Beta = sp.csc_matrix(res['W']).todense() self.Xi = sp.csc_matrix(res['Q']).todense() elif self.verbose: print('%s is trained already (trainable = False)' % (self.name)) return self
def fit(self, train_set): """Fit the model to observations. Parameters ---------- train_set: object of type TrainSet, required An object contraining the user-item preference in csr scipy sparse format,\ as well as some useful attributes such as mappings to the original user/item ids.\ Please refer to the class TrainSet in the "data" module for details. """ Recommender.fit(self, train_set) X = sp.csc_matrix(self.train_set.matrix) # recover the striplet sparse format from csc sparse matrix X (needed to feed c++) (rid, cid, val) = sp.find(X) val = np.array(val, dtype='float32') rid = np.array(rid, dtype='int32') cid = np.array(cid, dtype='int32') tX = np.concatenate((np.concatenate( ([rid], [cid]), axis=0).T, val.reshape((len(val), 1))), axis=1) del rid, cid, val if self.trainable: map_iid = train_set.iid_list (rid, cid, val) = train_set.item_graph.get_train_triplet(map_iid, map_iid) context_info = np.hstack( (rid.reshape(-1, 1), cid.reshape(-1, 1), val.reshape(-1, 1))) if self.variant == 'c2pf': res = c2pf.c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) elif self.variant == 'tc2pf': res = c2pf.t_c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) elif self.variant == 'rc2pf': res = c2pf.r_c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) else: res = c2pf.c2pf(tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, self.init_params) self.Theta = sp.csc_matrix(res['Z']).todense() self.Beta = sp.csc_matrix(res['W']).todense() self.Xi = sp.csc_matrix(res['Q']).todense() elif self.verbose: print('%s is trained already (trainable = False)' % (self.name))
def fit(self, train_set, val_set=None): """Fit the model to observations. Parameters ---------- train_set: :obj:`cornac.data.Dataset`, required User-Item preference data as well as additional modalities. val_set: :obj:`cornac.data.Dataset`, optional, default: None User-Item preference data for model selection purposes (e.g., early stopping). Returns ------- self : object """ Recommender.fit(self, train_set, val_set) X = sp.csc_matrix(self.train_set.matrix) # recover the striplet sparse format from csc sparse matrix X (needed to feed c++) (rid, cid, val) = sp.find(X) val = np.array(val, dtype="float32") rid = np.array(rid, dtype="int32") cid = np.array(cid, dtype="int32") tX = np.concatenate( (np.concatenate( ([rid], [cid]), axis=0).T, val.reshape((len(val), 1))), axis=1, ) del rid, cid, val if self.trainable: # use pre-trained params if exists, otherwise from constructor init_params = { "G_s": self.Gs, "G_r": self.Gr, "L_s": self.Ls, "L_r": self.Lr, "L2_s": self.L2s, "L2_r": self.L2r, "L3_s": self.L3s, "L3_r": self.L3r, } map_iid = train_set.item_indices (rid, cid, val) = train_set.item_graph.get_train_triplet(map_iid, map_iid) context_info = np.hstack( (rid.reshape(-1, 1), cid.reshape(-1, 1), val.reshape(-1, 1))) if self.variant == "c2pf": res = c2pf.c2pf( tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, init_params, ) elif self.variant == "tc2pf": res = c2pf.t_c2pf( tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, init_params, ) elif self.variant == "rc2pf": res = c2pf.r_c2pf( tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, init_params, ) else: res = c2pf.c2pf( tX, X.shape[0], X.shape[1], context_info, X.shape[1], X.shape[1], self.k, self.max_iter, init_params, ) self.Theta = sp.csc_matrix(res["Z"]).todense() self.Beta = sp.csc_matrix(res["W"]).todense() self.Xi = sp.csc_matrix(res["Q"]).todense() # overwrite init_params for future fine-tuning self.Gs = np.asarray(res["G_s"]) self.Gr = np.asarray(res["G_r"]) self.Ls = np.asarray(res["L_s"]) self.Lr = np.asarray(res["L_r"]) self.L2s = np.asarray(res["L2_s"]) self.L2r = np.asarray(res["L2_r"]) self.L3s = np.asarray(res["L3_s"]) self.L3r = np.asarray(res["L3_r"]) elif self.verbose: print("%s is trained already (trainable = False)" % (self.name)) return self