Ejemplo n.º 1
0
    def fit(self, ratings, n_iters=50):
        # ratings有三列,[user_id, item_id, rating]
        # 取第三列rating的全局评分的均值,用于去均值化
        self.mean_rating_ = np.mean(ratings.take(2, axis=1))
        # user-item矩阵,行为user_id,列为item_id,值为评分,每行对应一个ID,从0开始,所以ID-1
        self.ratings_csr_ = build_user_item_matrix(self.n_user, self.n_item,
                                                   ratings)
        self.ratings_csc_ = self.ratings_csr_.tocsc()
        # 初始化最近一次的RMSE
        last_rmse = None
        for iteration in range(n_iters):
            logger.debug('iteration {:d}'.format(iteration))

            self._update_user_feature()
            self._update_item_feature()
            # 计算RMSE
            train_preds = self.predict(ratings.take([0, 1], axis=1))
            train_rmse = RMSE(train_preds, ratings.take(2, axis=1))
            logger.info('iter: {:d}, train RMSE: {:.6f}'.format(
                iteration, train_rmse))

            # 当两次rmse的差小于阈值,即收敛则停止
            if last_rmse and abs(train_rmse - last_rmse) < self.converge:
                logger.info(
                    'converges at iteration {:d}, stop.'.format(iteration))
                break
            else:
                last_rmse = train_rmse
        return self.user_features_, self.item_features_
Ejemplo n.º 2
0
def error_of_vot(best_model, dic_z, input_z, params):

    pred_vots = []
    with torch.no_grad():
        for key in input_z:
            pred_vots.append(
                (-best_model.vot_module(input_z[key]) * 60).flatten())

    true_vots = []
    for key in dic_z:
        true_vots.append((-valueOfTime(params, dic_z[key]) * 60).flatten())

    rmse = RMSE(pred_vots, true_vots).item()
    mabse = ABSE(pred_vots, true_vots).item()
    re = RE(pred_vots, true_vots).item()

    return pred_vots, true_vots, rmse, mabse, re
Ejemplo n.º 3
0
def optimize_model():
    print("Start training model with data poisoning attacks!")
    last_rmse = None
    for iteration in xrange(n_iters):
        t1 = time.time()
        ALS(n_user, n_item, n_feature, mal_user, train, mean_rating_, mal_mean_rating_, mal_ratings, lamda_u, lamda_v, \
        user_features_, mal_user_features_, item_features_)
        train_preds = predict(train.take([0, 1], axis=1), user_features_,
                              item_features_, mean_rating_)
        train_rmse = RMSE(train_preds, train.take(2, axis=1))
        t2 = time.time()
        print("The %d th iteration \t time: %ds \t RMSE: %f " %
              (iteration + 1, t2 - t1, train_rmse))
        # stop when converge
        if last_rmse and abs(train_rmse - last_rmse) < converge:
            break
        else:
            last_rmse = train_rmse
    return last_rmse
Ejemplo n.º 4
0
    def fit(self, ratings, n_iters=50):
        """training models"""

        check_ratings(ratings, self.n_user, self.n_item, self.max_rating,
                      self.min_rating)

        self.mean_rating_ = np.mean(ratings[:, 2])

        # csr user-item matrix for fast row access (user update)
        self.ratings_csr_ = build_user_item_matrix(self.n_user, self.n_item,
                                                   ratings)
        # keep a csc matrix for fast col access (item update)
        self.ratings_csc_ = self.ratings_csr_.tocsc()

        last_rmse = None
        for iteration in xrange(n_iters):
            logger.debug("iteration %d...", iteration)

            # update item & user parameter
            self._update_item_params()
            self._update_user_params()

            # update item & user features
            self._udpate_item_features()
            self._update_user_features()

            # compute RMSE
            train_preds = self.predict(ratings[:, :2])
            train_rmse = RMSE(train_preds, ratings[:, 2])
            # train_preds = self.predict(ratings[:, :2])
            # train_rmse = RMSE(train_preds, ratings[:, 2])
            logger.info("iter: %d, train RMSE: %.6f", iteration, train_rmse)

            # stop when converge
            if last_rmse and abs(train_rmse - last_rmse) < self.converge:
                logger.info('converges at iteration %d. stop.', iteration)
                break
            else:
                last_rmse = train_rmse
        print(train_rmse)
        return self
Ejemplo n.º 5
0
def optimize_model_origin(converge, n_user, n_item, n_feature, train,
                          mean_rating_, lamda_u, lamda_v,
                          user_features_origin_, item_features_origin_):

    print("Start training model without data poisoning attacks!")
    last_rmse = None
    n_iters = 100
    for iteration in range(n_iters):
        t1 = time.time()
        user_features_origin_, item_features_origin_ = ALS_origin(
            n_user, n_item, n_feature, train, mean_rating_, lamda_u, lamda_v,
            user_features_origin_, item_features_origin_)
        train_preds = predict(train.take([0, 1], axis=1),
                              user_features_origin_, item_features_origin_)
        train_rmse = RMSE(train_preds, train.take(2, axis=1) - 3)
        t2 = time.time()
        print("The %d th iteration \t time: %ds \t RMSE: %f " %
              (iteration + 1, t2 - t1, train_rmse))
        # stop when converge
        if last_rmse and abs(train_rmse - last_rmse) < converge:
            break
        else:
            last_rmse = train_rmse
    return last_rmse
Ejemplo n.º 6
0
f.write(str(args))
f.close()
#
## save model string
f = open(args.result_path + "/" + "summary_modelstr.txt", "wb")
f.write(str(best_model))
f.close()

#======Toy data VOT=====================
allds = {"train": ds_train, "dev": ds_dev, "test": ds_test}
pred_vots = [
    predictVOT(best_model, allds[name].z) for name in ["train", "dev", "test"]
]
true_vots = [allds[name].vots for name in ["train", "dev", "test"]]

rmse = RMSE(pred_vots, true_vots).item()
mabse = ABSE(pred_vots, true_vots).item()
re = RE(pred_vots, true_vots).item()

s = ""
s += "rmse:" + str(rmse) + "\n"
s += "mean absolute error:" + str(mabse) + "\n"
s += "percentage error:" + str(re) + "\n"

f = open(args.result_path + "/" + "vot_error.txt", "wb")
f.write(str(s))
f.close()

pickle.dump(pred_vots, open(args.result_path + "/" + "pred_vots.pkl", "wb"))

#======= VOT on simulated z ==========
Ejemplo n.º 7
0
def BPNNIndustry(StartYear,EndYear,PreStartYear,PreEndYear,timestep,pretype,city="云南省", hidden=[24,12], learningrate=0.005,epoch=1000):
    """

    Parameters
    ----------
    StartYear : TYPE
        DESCRIPTION.
    EndYear : TYPE
        DESCRIPTION.
    PreStartYear : TYPE
        DESCRIPTION.
    PreEndYear : TYPE
        DESCRIPTION.
    timestep : TYPE
        DESCRIPTION.
    pretype : TYPE, optional
        DESCRIPTION. The default is "consumption".
    city : TYPE, optional
        DESCRIPTION. The default is "云南省".
    hidden : TYPE, optional
        神经网络的隐藏层, list, 几个元素代表几层,每层神经元个数为list元素值. The default is [24,12].
    learningrate : TYPE, optional
        神经网络学习率. The default is 0.005.
    epoch : TYPE, optional
        训练学习次数. The default is 1000.

    Returns
    -------
    None.

    """
    
    def bpnn(timestep,outputlen,x_train,y_train,x_test,y_test,x_pre,hiddenneron,lr,epoch):
        x=tf.placeholder(tf.float32,shape=[None,timestep],name="Input")
        y=tf.placeholder(tf.float32,shape=[None,outputlen],name="Onput")
        hlen=len(hiddenneron)
        f=locals()
        for i in range(hlen+1):
            if i==0:
                f["f%s"%(i+1)]=tf.contrib.layers.fully_connected (x,hiddenneron[i])
            else:
                if i== hlen:
                    pre=tf.contrib.layers.fully_connected (f["f%s"%(i)],outputlen)
                else:
                    f["f%s"%(i+1)]=tf.contrib.layers.fully_connected (f["f%s"%(i)],hiddenneron[i])
        
        loss=tf.losses.mean_squared_error(y, pre)
    
        train_op = tf.train.AdamOptimizer(lr).minimize(loss)
        
        saver = tf.train.Saver()
        
        with tf.Session() as sess:
            init=tf.initialize_all_variables()
            sess.run(init)
            for i in range(epoch):
                sess.run(train_op,feed_dict={x:x_train,y:y_train})
                lossz=sess.run(loss,feed_dict={x:x_train,y:y_train})
                if i%50==0:
                    print(lossz)
            
            y_train_pre=sess.run(pre,feed_dict={x:x_train}) 
            
            y_test_pre=sess.run(pre,feed_dict={x:x_test}) 
            
            y_pre=sess.run(pre,feed_dict={x:x_pre}) 
            
            training=np.array(y_train_pre).squeeze()
            
            predictions=np.array(y_test_pre).squeeze()
            labels=np.array(y_test).squeeze()
            # saver.save(sess, "D:/lab/Yunnan_Pre/result/yunnan_shortterm_钢铁_BPNN/")
        return predictions,labels,y_pre,training


    if timestep > (int(EndYear)-int(StartYear)+1)*0.5:
        return {"trainfromyear":None,"traintoyear":None,"trainresult":None,"prefromyear":None,"pretoyear":None,"preresult":"timestep设定过大,请重新设定。","MAPE":None,"RMSE":None}
    else:
        #读取数据,确定参数
        name=[pretype]
        finaldata=[]
        outputlen=int(PreEndYear)-int(PreStartYear)+1
        
        
        
        datajson=getData("云南省_year_电力电量类-行业", pretype, StartYear, EndYear)
        data=json.loads(datajson)
        finaldata.append(data)
        final=pd.DataFrame(finaldata,index=name)
        final=final.T
    
        
        test_size=0#测试数据集应当取0才可以
        X,y=generate_data(final,timestep,outputlen,test_size=test_size,if_norm="no")
        testdata=final[pretype].values
        testinput=[]
        testoutput=[]
        
        num=len(X["train"])
        selet=int(np.floor(num/2))
        testinput=X["train"][selet:,:]
        testoutput=y["train"][selet:,:]
        
        x_pre=np.array(np.flipud(testdata[-1:-(timestep+1):-1])).reshape(1,-1)
        
        test_pre,test_label,pre,training=bpnn(timestep,outputlen,X["train"][:-1,:],y["train"][:-1,:],testinput,testoutput,x_pre,hidden,learningrate,epoch)
        
        mape=MAPE(test_pre,test_label)
        rmse=RMSE(test_pre,test_label)
        
        
        #保存训练结果,年份上可能有问题
        #trainingtrue=y["train"][:-1,:].flatten()
        trainingtrue=y["train"][-1,:]
        
        trainyear=[]
        for t in trainingtrue:
            count=-1
            for d in final[pretype]:
                count+=1
                
                if t>d-5 and t<d+5:
                    # print("yes")
                    trainyear.append(final.index[count])
                    break
        
        ytrain=training[-1]
        ypre=pre.flatten()
        
        #trainsave.to_csv("D:/lab/Yunnan_Pre/result/yunnan_shortterm_consumption_BPNN_training.csv")
        result={"trainfromyear":trainyear[0],"traintoyear":trainyear[-1],"trainresult":ytrain.tolist(),"prefromyear":PreStartYear,"pretoyear":PreEndYear,"preresult":ypre.tolist(),"MAPE":mape,"RMSE":rmse}
        #保存
        return result
Ejemplo n.º 8
0
            print(gt.shape)
            down_gt = np.load(path + dset + '/data20.npy')
            down_gt = np.moveaxis(down_gt, 1, 3)
            #down_gt = misc.imresize(gt, 1.0/args.scale,  interp='bicubic')
            up_gt = np.zeros((down_gt.shape[0], down_gt.shape[1] * 2,
                              down_gt.shape[2] * 2, down_gt.shape[3]))
            for i in range(0, down_gt.shape[0]):
                up_gt[i] = cv2.resize(
                    down_gt[i], (down_gt.shape[1] * 2, down_gt.shape[2] * 2),
                    interpolation=inter)
            up_gt = np.moveaxis(up_gt, 3, 1)
            up_gt = recompose_images(up_gt, border=4, size=(5490, 5490, 6))

        ssim.append(compare_ssim(gt, up_gt, multichannel=True))
        uiqi.append(uqi(gt, up_gt))
        RMSE_err.append(RMSE(up_gt, gt))
        SRE_err.append(SRE(up_gt, gt))
        ERGAS_err.append(ERGAS(up_gt, gt, 2))
        SAM_err.append(SAM(up_gt, gt))
        psnr.append(compare_psnr(gt, up_gt, data_range=np.max(gt)))
        bpsnr.append(bPSNR(up_gt, gt))

        if args.save or args.test_one != 'none':
            print('Writing to file...')
            np.save(path + dset + '/' + args.predication_file, up_gt)

    print('psnr: {:.4f}'.format(np.mean(psnr)))
    print('bpsnr: {:.4f}'.format(np.mean(bpsnr)))
    print('RMSE Average: {:.4f}'.format(np.mean(RMSE_err)))
    print('UIQI Average: {:.4f}'.format(np.mean(uiqi)))
    print('ssim: {:.4f}'.format(np.mean(ssim)))
##########################################################################
# Compute RMSE, FINAL ERROR AND MAXIMUM ERROR
##########################################################################
from evaluation import RMSE, MAE, MAPE

#ORB_SLAM2 Keyframes Position
kf_orb_slam2 = pandas.read_csv(os.path.expanduser(path_keyframes_position_file), sep=" ", parse_dates=True,
    date_parser=dateparse , index_col='time',
    names=['time', 'x', 'y', 'z', 'cov_xx', 'cov_xy', 'cov_xz', 'cov_yx', 'cov_yy', 'cov_yz',
        'cov_zx', 'cov_zy', 'cov_zz'], header=None)

estimation = orb_slam2.resample('1s').pad()
ground_truth = reference.resample('1s').pad()
##########################################################################

rmse = RMSE()
eval_rmse = rmse.evaluate(estimation, ground_truth)
la.norm(eval_rmse) #0.145m with adaptation # 0.163m original #1.827m 0.5fps w/relocalization # 0.204 m w/o relocalization
print("RMSE: " + str(la.norm(eval_rmse[0:3])))
##########################################################################
final_estimation = np.array([estimation.x[estimation.shape[0]-1], estimation.y[estimation.shape[0]-1], estimation.z[estimation.shape[0]-1]])
final_groundtruth = np.array([ground_truth.x[ground_truth.shape[0]-1], ground_truth.y[ground_truth.shape[0]-1], ground_truth.z[ground_truth.shape[0]-1]])

final_error = final_estimation - final_groundtruth

la.norm(final_error) #0.264m #0.264m original #0.52m 0.5fps
print("Final error: " + str(la.norm(final_error)))

##########################################################################
max_error = np.max(estimation - ground_truth)
la.norm(max_error) #0.468m adaptation 0.4553m original #4.620m 0.5fps w/relocalization #0.729m w/o relocalization
Ejemplo n.º 10
0
        if self.max_rating:
            preds[preds > self.max_rating] = self.max_rating

        # 限制预测值的下限
        if self.min_rating:
            preds[preds < self.min_rating] = self.min_rating

        return preds


if __name__ == '__main__':
    # 载入文件
    with gzip.open('raw_data/ml_100k_ratings.pkl.gz') as f:
        ratings = cPickle.load(f, encoding='latin1')

    # ID从0开始
    ratings[:, 0] = ratings[:, 0] - 1
    ratings[:, 1] = ratings[:, 1] - 1

    als = ALS(n_user=943,
              n_item=1682,
              n_feature=10,
              reg=1e-2,
              max_rating=5.0,
              min_rating=1.0,
              seed=0)
    # 建模拟合数据
    user_features, item_features = als.fit(ratings, n_iters=5)
    # 预测率
    rmse = RMSE(als.predict(ratings[:, :2]), ratings[:, 2])
Ejemplo n.º 11
0
        # 		if count > K:
        # 			break
        # print(total_number)

        # pickle.dump(total_number,file1_object)
        # exit()

        #mean_rating_ = np.mean(validation[:, 2])
        # def predict(user_features_,item_features_, data,max_rating=5.0,min_rating=1.0):
        # 	if not mean_rating_:
        # 		raise NotFittedError()
        # 	u_features = user_features_.take(data.take(0, axis=1), axis=0)
        # 	i_features = item_features_.take(data.take(1, axis=1), axis=0)
        # 	preds = np.sum(u_features * i_features, 1) + mean_rating_
        # 	if max_rating:
        # 		preds[preds > max_rating] = max_rating
        # 	if min_rating:
        # 		preds[preds < min_rating] = min_rating
        # 	return preds
        # train_preds = bpmf.predict(ratings[:, :2])
        # train_rmse = RMSE(train_preds, ratings[:, 2])
        #bpmf.fit(validation, n_iters=eval_iters)
        val_preds = bpmf.predict(validation[:, :2])
        val_rmse += RMSE(val_preds, validation[:, 2])
        print("after %d iteration, validation RMSE: %.6f" %
              (eval_iters, val_rmse))
    # else:
    # 	print(i)
    # val_rmse=val_rmse/5.0
    # print("after %d iteration, validation RMSE: %.6f" %
    # 	      (eval_iters,val_rmse))
            #psnr.append(compare_psnr(gt,images, data_range=np.max(gt)))
            #ssim.append(compare_ssim(gt,images, multichannel=True))           
        print('RMSE Average: {:.4f}'.format(np.mean(RMSE_err)))
        print('SRE Average: {:.4f}'.format(np.mean(SRE_err)))
        #print('ERGAS Average: {:.4f}'.format(np.mean(ERGAS_err)))
        #print('SAM Average:{:.4f}'.format(np.mean(SAM_err)))
        #print('psnr: {:.4f}'.format(np.mean(psnr)))
        #print('ssim: {:.4f}'.format(np.mean(ssim)))
    '''

    for dset in filelist:
        images = np.load(path + dset + '/%s.npy' % model_name)
        images = downPixelAggr(images, 2)
        gt = np.load(path + dset + '/no_tiling/data20_gt.npy')
        print(dset)

        RMSE_err.append(RMSE(images, gt))
        SRE_err.append(SRE(images, gt))
        ERGAS_err.append(ERGAS(images, gt, 2))
        SAM_err.append(SAM(images, gt))
        psnr.append(compare_psnr(gt, images, data_range=np.max(gt)))
        ssim.append(compare_ssim(gt, images, multichannel=True))
        bpsnr.append(bPSNR(gt, images))

    print('RMSE Average: {:.4f}'.format(np.mean(RMSE_err)))
    print('SRE Average: {:.4f}'.format(np.mean(SRE_err)))
    print('ERGAS Average: {:.4f}'.format(np.mean(ERGAS_err)))
    print('SAM Average:{:.4f}'.format(np.mean(SAM_err)))
    print('psnr: {:.4f}'.format(np.mean(psnr)))
    print('ssim: {:.4f}'.format(np.mean(ssim)))
    print('bpsnr: {:.4f}'.format(np.mean(bpsnr)))
Ejemplo n.º 13
0
    def fit(self, ratings, n_iters=50):
        # 全局电影评分的均值
        self.mean_rating_ = np.mean(ratings[:, 2])
        last_rmse = None
        # 切分数据集,进行分批训练,假设batch_size=10000,ratings=100000,则切成10份
        batch_num = int(np.ceil(float(ratings.shape[0] / self.batch_size)))
        logger.debug('batch count = {}'.format(batch_num + 1))

        # 初始化动量矩阵
        u_feature_mom = np.zeros((self.n_user, self.n_feature))
        i_feature_mom = np.zeros((self.n_item, self.n_feature))
        # 初始化梯度矩阵
        u_feature_grads = np.zeros((self.n_user, self.n_feature))
        i_feature_grads = np.zeros((self.n_item, self.n_feature))

        for iteration in range(n_iters):
            logger.debug('iteration {:d}'.format(iteration))
            # 打乱数据集
            self.random_state.shuffle(ratings)
            # 分批训练
            for batch in range(batch_num):
                start_idx = int(batch * self.batch_size)
                end_idx = int((batch + 1) * self.batch_size)
                data = ratings[start_idx:end_idx]

                # 计算梯度
                # data.take(0, axis=1),取data数据集第0列的值,即用户ID
                # user_features_1矩阵是根据用户ID排序的,根据之前的分批用户ID找到对应的数据
                u_features = self.user_features_.take(data.take(0, axis=1),
                                                      axis=0)
                # 第一列为项目ID,使用项目ID作为item_features_的索引
                i_features = self.item_features_.take(data.take(1, axis=1),
                                                      axis=0)

                # 计算预测值,用户矩阵和项目矩阵做内积
                preds = np.sum(u_features * i_features, 1)
                # 计算误差,预测值 - (实际值 - 实际值的全局均值)
                errs = preds - (data.take(2, axis=1) - self.mean_rating_)
                # 误差矩阵,大小为errs * n_feature
                # http://blog.csdn.net/ksearch/article/details/21388985
                # 假设errs.shape=(10000,),使用np.tile则在行上复制n_feature次
                # 即(10, 10000),在转置为(10000, 10),等于将errs每个值在横轴上复制10次
                err_mat = np.tile(errs, (self.n_feature, 1)).T
                # (u_features * i_features - trues)^2 + λ(u_features + i_features)求导
                # 第二项为正则化项,使用L1,分别对该公式求u_features和i_features的梯度
                u_grads = 2 * i_features * err_mat + self.reg * u_features
                i_grads = 2 * u_features * err_mat + self.reg * i_features

                # 初始化梯度矩阵,所有值为0
                u_feature_grads.fill(0.0)
                i_feature_grads.fill(0.0)

                # 更新梯度矩阵
                for i in range(data.shape[0]):
                    row = data.take(i, axis=0)
                    # row[0]为用户ID,u_feature_grads.shape=(943, 10)
                    u_feature_grads[row[0], :] += u_grads.take(i, axis=0)
                    # row[1]为项目ID,i_feature_grads.shape=(1682, 10)
                    i_feature_grads[row[1], :] += i_grads.take(i, axis=0)

                # 更新动量,以前梯度方向 + 当前梯度方向 = 现在走的梯度方向
                # momentum决定以前梯度有多大影响,动量用于当前梯度为0时,陷入高原
                # 或局部最小点时,可靠以前梯度的惯性继续往前走
                u_feature_mom = (self.momentum * u_feature_mom) + \
                                ((self.epsilon / data.shape[0]) * u_feature_grads)
                i_feature_mom = (self.momentum * i_feature_mom) + \
                                ((self.epsilon / data.shape[0]) * i_feature_grads)

                # 更新隐变量latent variables
                self.user_features_ -= u_feature_mom
                self.item_features_ -= i_feature_mom

            # 计算RMSE
            train_preds = self.predict(ratings[:, :2])
            train_rmse = RMSE(train_preds, ratings[:, 2])
            logger.info('iter: {:d}, train RMSE: {:.6f}'.format(
                iteration, train_rmse))

            # 当两次rmse的差小于阈值,即收敛则停止
            if last_rmse and abs(train_rmse - last_rmse) < self.converge:
                logger.info(
                    'converges at iteration {:d}, stop.'.format(iteration))
                break
            else:
                last_rmse = train_rmse

        return self.user_features_, self.item_features_
Ejemplo n.º 14
0
    with gzip.open('raw_data/ml_100k_ratings.pkl.gz') as f:
        ratings = cPickle.load(f, encoding='latin1')

    # ID从0开始
    ratings[:, 0] = ratings[:, 0] - 1
    ratings[:, 1] = ratings[:, 1] - 1

    pmf = PMF(n_user=943,
              n_item=1682,
              n_feature=10,
              batch_size=1e4,
              epsilon=20.0,
              reg=1e-4,
              max_rating=5.0,
              min_rating=1.0,
              seed=0)
    # 建模拟合数据
    user_features, item_features = pmf.fit(ratings, n_iters=15)
    # 预测率
    rmse = RMSE(pmf.predict(ratings[:, :2]), ratings[:, 2])

    ratings_df = pd.DataFrame(ratings,
                              columns=['user_id', 'item_id', 'true_rating'])
    ratings_df['pred_rating'] = pmf.predict(ratings[:, :2])

    #true_rating_df = ratings_df.pivot(index='user_id', columns='item_id', values='true_rating').fillna(0)
    #pred_rating_df = ratings_df.pivot(index='user_id', columns='item_id', values='pred_rating').fillna(0)
    # user * item的所有评分
    user_item_rating = np.dot(user_features, item_features.T) + np.mean(
        ratings[:, 2])
Ejemplo n.º 15
0
def main():
    argparser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    # data set parameters
    argparser.add_argument('--X_context',
                           default=168,
                           help='observing time length',
                           type=int)
    argparser.add_argument('--y_horizon',
                           default=24,
                           help='predicting time length',
                           type=int)
    argparser.add_argument('--window_skip',
                           default=12,
                           help='skipping step for data generation',
                           type=int)
    argparser.add_argument('--train_prop',
                           default=0.97,
                           help='percent of data used for trainning',
                           type=float)

    # network structure
    argparser.add_argument('--h_dim',
                           default=200,
                           help='dimension for ts/event encoder and decoder',
                           type=int)
    argparser.add_argument('--z_dim',
                           default=100,
                           help='dimension for latent variable encoder',
                           type=int)
    argparser.add_argument('--use_GRU',
                           default=True,
                           help='RNN cell type(True:GRU, False:LSTM)',
                           type=bool)

    # trainning setting
    argparser.add_argument('--lr',
                           default=0.001,
                           help='learning_rate',
                           type=float)
    argparser.add_argument('--dec_bound',
                           default=0.05,
                           help='dec_bound for std',
                           type=float)
    argparser.add_argument('--batch_size',
                           default=400,
                           help='batch size',
                           type=int)
    argparser.add_argument('--epochs',
                           default=100,
                           help='trainning epochs',
                           type=int)
    argparser.add_argument('--device',
                           default='cuda:0',
                           help='select device (cuda:0, cpu)',
                           type=str)
    argparser.add_argument('--mc_times',
                           default=1000,
                           help='num of monte carlo simulations',
                           type=int)
    args = argparser.parse_args()
    data_dict, num_event_type = get_air_quality_data()
    print('Dataset downloaded and preprocessed successfully!')
    dataset = TsEventDataset(data_dict,
                             num_event_type,
                             X_context=args.X_context,
                             y_horizon=args.y_horizon,
                             window_skip=args.window_skip,
                             train_prop=args.train_prop)
    device = torch.device(args.device)
    model = VSMHN(device,
                  dataset.x_dim,
                  num_event_type + 2,
                  dataset.t_dim,
                  args.h_dim,
                  args.z_dim,
                  args.y_horizon,
                  dec_bound=args.dec_bound,
                  use_GRU=args.use_GRU).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)

    print('Training:')
    for epoch in tqdm(range(args.epochs)):
        dataset.train_shuffle()
        while True:
            (X_ts_batch, X_tf_batch, X_event_batch,
             X_event_arrays), (y_ts_batch, y_tf_batch,
                               y_target), end = dataset.next_batch(
                                   args.batch_size, train=True)
            optimizer.zero_grad()
            loss_like, loss_kl = model(X_ts_batch.to(device),
                                       X_event_batch.to(device),
                                       X_tf_batch.to(device),
                                       y_ts_batch.to(device),
                                       y_tf_batch.to(device))
            loss = -loss_like + loss_kl
            loss.backward()
            optimizer.step()
            if end:
                break
    dataset.test_shuffle()
    (X_ts_batch, X_tf_batch, X_event_batch,
     X_event_arrays), (y_ts_batch, y_tf_batch,
                       y_target), end = dataset.next_batch(100000, train=False)
    print('Forecasting and Plotting:')
    indexs = range(-60, 0, 2)  # plot last 720 time stamps
    plot_size = len(indexs)
    (X_ts_batch, X_tf_batch, X_event_batch,
     X_event_arrays), (y_ts_batch, y_tf_batch,
                       y_target) = dataset._get_batch(indexs)
    ts_past, _, _, _, _ = X_ts_batch.to(device), X_event_batch.to(
        device), X_tf_batch.to(device), y_ts_batch.to(device), y_tf_batch.to(
            device)
    preds = predict(model,
                    X_ts_batch.to(device),
                    X_event_batch.to(device),
                    X_tf_batch.to(device),
                    y_tf_batch.to(device),
                    mc_times=args.mc_times)
    (y_ts_batch, y_tf_batch,
     y_target) = [x.numpy() for x in (y_ts_batch, y_tf_batch, y_target)]
    ts_mean = preds.mean(axis=0)
    ts_std = preds.std(axis=0)
    num_plots = min(plot_size, X_ts_batch.shape[0])
    fig, axes = plt.subplots(num_plots, 1, figsize=(4, 2 * num_plots))
    for idx in range(num_plots):
        ax = axes[idx]
        X_idx = X_tf_batch[idx, -30:, 0]
        y_idx = y_tf_batch[idx, :, 0]
        for i in range(4):
            ts_past = X_ts_batch[idx, -30:, i]
            ts_future = y_ts_batch[idx, :, i]
            ts_pred = ts_mean[idx, :, i]
            std_pred = ts_std[idx, :, i]
            ax.plot(X_idx, ts_past, color='k', alpha=0.5)
            ax.plot(y_idx, ts_future, color='k', alpha=0.5)
            ax.plot(y_idx, ts_pred, color='r', alpha=0.5)
            ax.fill_between(y_idx,
                            ts_pred - std_pred,
                            ts_pred + std_pred,
                            color='r',
                            alpha=0.1)
    fig.savefig('forecast_plots.png')
    preds_ori = dataset.dataset['time_series_scaler'].inverse_transform(
        preds.reshape(-1, 720, 4).reshape(-1, 4)).reshape(-1, 720, 4)
    ts_ori = dataset.dataset['time_series_scaler'].inverse_transform(
        y_ts_batch.reshape(-1, 4))
    rmse = RMSE(ts_ori, preds_ori.mean(axis=0))
    crps = calc_crps(ts_ori, preds_ori.mean(axis=0),
                     preds_ori.std(axis=0)).mean(axis=0)
    print(f"RMSE scores: {rmse}")
    print(f"CRPS scores: {crps}")
Ejemplo n.º 16
0
    def fit(self, ratings, n_iters=50):
        #print(1)
        check_ratings(ratings, self.n_user, self.n_item,
                      self.max_rating, self.min_rating)
        #print(2)
        self.mean_rating_ = np.mean(ratings[:, 2])
        #print(self.mean_rating_)
        last_rmse = None
        #print(3)
        batch_num = int(np.ceil(float(ratings.shape[0]/ self.batch_size)))
        #print(batch_num)
        logger.debug("batch count = %d", batch_num + 1)
        #print(4)

        # momentum
        u_feature_mom = np.zeros((self.n_user, self.n_feature))
        #print(u_feature_mom)
        i_feature_mom = np.zeros((self.n_item, self.n_feature))
        #print(i_feature_mom)
        # gradient
        u_feature_grads = np.zeros((self.n_user, self.n_feature))
        #print(u_feature_grads)
        i_feature_grads = np.zeros((self.n_item, self.n_feature))
        #print(i_feature_grads)
        k = 0
        m = 0
        for iteration in xrange(n_iters):
            logger.debug("iteration %d...", iteration)
            #print(5)
            self.random_state.shuffle(ratings)
            #print(6)
            for batch in xrange(batch_num):
                start_idx = int(batch * self.batch_size)
                #print(start_idx)
                end_idx = int((batch + 1) * self.batch_size)
                #print(end_idx)
                data = ratings[start_idx:end_idx]
                #print(data)
                #print(data.take(0, axis=1))
                # compute gradient
                u_features = self.user_features_.take(
                    data.take(0, axis=1), axis=0)  #data.take(0, axis=1)表示取用户值,user_features_.take表示取这些用户对应的特征值
                #print(k)
                #print(u_features)
                i_features = self.item_features_.take(
                    data.take(1, axis=1), axis=0)
                #print(data.take(1, axis=1))
                #print(m)
                #print(i_features)
                k = k+1
                m = m+1
                preds = np.sum(u_features * i_features, 1)    #即可得到用户i对item i 的ratings
                #print(preds)
                errs = preds - (data.take(2, axis=1) - self.mean_rating_)   #为什么要减去平均值呢
                #print(errs)
                # print(errs)
                # print("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
                err_mat = np.tile(2 * errs, (self.n_feature, 1)).T
                #print(err_mat)
                u_grads = i_features * err_mat + self.reg * u_features
                i_grads = u_features * err_mat + self.reg * i_features
                #print(u_grads)
                #print(i_grads)
                u_feature_grads.fill(0.0)
                #print(u_feature_grads)
                i_feature_grads.fill(0.0)
                #print(i_feature_grads)
                for i in xrange(data.shape[0]):
                    row = data.take(i, axis=0)
                    #print(row)
                    u_feature_grads[row[0], :] += u_grads.take(i, axis=0)
                    i_feature_grads[row[1], :] += i_grads.take(i, axis=0)
                    #print(u_feature_grads)
                    #print(i_feature_grads)
                # update momentum
                u_feature_mom = (self.momentum * u_feature_mom) + \
                    ((self.epsilon / data.shape[0]) * u_feature_grads)
                #print(u_feature_mom)
                i_feature_mom = (self.momentum * i_feature_mom) + \
                    ((self.epsilon / data.shape[0]) * i_feature_grads)
                #print(i_feature_mom)
                # update latent variables
                self.user_features_ -= u_feature_mom
                #print(self.user_features_)
                self.item_features_ -= i_feature_mom
                #print(self.item_features_)
           
            # compute RMSE

            train_preds = self.predict(ratings[:, :2])
            #print(train_preds)
            train_rmse = RMSE(train_preds, ratings[:, 2])
            #print(train_rmse)
            logger.info("iter: %d, train RMSE: %.6f", iteration, train_rmse)

            # stop when converge
            if last_rmse and abs(train_rmse - last_rmse) < self.converge:
                logger.info('converges at iteration %d. stop.', iteration)
                break
            else:
                last_rmse = train_rmse
        return self