(model, data_vali, gap_size, use_gpu)
return acc_vali
对data_vali尾部的样本可能会有舍弃的现象
对data_vali尾部的样本可能会有舍弃的现象
对data_vali尾部的样本可能会有舍弃的现象
def eval(model, data_vali, gap_size, use_gpu):
    """对data_vali尾部的样本可能会有舍弃的现象"""
    model.eval()
    acc_sum = 0
    ii = 0
    start_vali = 0
    while start_vali + gap_size <= data_vali.shape[0]:
        end_vali = start_vali + gap_size
        tmp_vali = data_vali[start_vali:end_vali]
        start_vali = end_vali

        x_vali = torch.LongTensor(tmp_vali[:,:-1])
        y_vali = torch.LongTensor(tmp_vali[:,-1])
        if use_gpu:
            x_vali = x_vali.cuda()
            y_vali = y_vali.cuda()

        out_tmp = model(x_vali)
        pre_tmp = torch.max(out_tmp, dim=1)[1]
        acc_tmp = torch.mean(torch.eq(pre_tmp, y_vali).float()).item()
        acc_sum += acc_tmp
        ii += 1

    acc_vali = acc_sum / ii
    model.train()
    return acc_vali
def eval ( model , data_vali , gap_size , use_gpu ) : model . eval ( ) acc_sum = 0 ii = 0 start_vali = 0 while start_vali + gap_size <= data_vali . shape [ 0 ] : end_vali = start_vali + gap_size tmp_vali = data_vali [ start_vali : end_vali ] start_vali = end_vali x_vali = torch . LongTensor ( tmp_vali [ : , : - 1 ] ) y_vali = torch . LongTensor ( tmp_vali [ : , - 1 ] ) if use_gpu : x_vali = x_vali . cuda ( ) y_vali = y_vali . cuda ( ) out_tmp = model ( x_vali ) pre_tmp = torch . max ( out_tmp , dim = 1 ) [ 1 ] acc_tmp = torch . mean ( torch . eq ( pre_tmp , y_vali ) . float ( ) ) . item ( ) acc_sum += acc_tmp ii += 1 acc_vali = acc_sum / ii model . train ( ) return acc_vali