示例#1
0
 train_df = pd.read_pickle(data_path + "seq-train.pkl")
 la = ["age", "gender", "demand"]
 train_df["age"] = train_df["age"] - 1
 train_df["gender"] = train_df["gender"] - 1
 for l in la:
     print(l, train_df[l].unique())
 val_df = train_df.sample(n=10000, random_state=999999)
 train_df = train_df.drop(val_df.index)
 train_df.reset_index(drop=True, inplace=True)
 val_df.reset_index(drop=True, inplace=True)
 train_ds = dataset.seq_dataset(train_df, SEQ_LEN, "gender")
 val_ds = dataset.seq_dataset(val_df, SEQ_LEN, "gender")
 model = model.baseline_model(VOCAB_SIZE, EMBED_DIM, NUN_CLASS,
                              MODE).to(device)
 criterion = torch.nn.CrossEntropyLoss()  #.to(device)
 optimizer = torch.optim.SGD(model.parameters(), weight_decay=1e-5, lr=1.0)
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                             step_size=1,
                                             gamma=0.75)
 len_train = len(train_ds)
 len_val = len(val_ds)
 loss_plt = []
 for epoch in range(N_EPOCHS):
     train_dl = DataLoader(train_ds,
                           batch_size=BATCH_SIZE,
                           shuffle=True,
                           collate_fn=train_ds.generate_batch)
     val_dl = DataLoader(val_ds,
                         batch_size=BATCH_SIZE,
                         collate_fn=val_ds.generate_batch)
     train_loss, train_acc = train_epoc(train_dl, model, criterion,
示例#2
0
 #3412772
 train_df=pd.read_pickle(data_path+"seq-train.pkl")
 la=["age","gender","demand"]
 train_df["age"]=train_df["age"]-1
 train_df["gender"]=train_df["gender"]-1
 for l in la:
     print(l,train_df[l].unique())
 val_df=train_df.sample(frac=0.2)#,random_state=999999
 train_df=train_df.drop(val_df.index)
 train_df.reset_index(drop=True, inplace=True)
 val_df.reset_index(drop=True, inplace=True)
 train_ds=dataset.seq_dataset(train_df,SEQ_LEN,"age")
 val_ds=dataset.seq_dataset(val_df,SEQ_LEN,"age")
 model = model.baseline_model(VOCAB_SIZE, EMBED_DIM, NUN_CLASS,MODE).to(device)
 criterion = torch.nn.CrossEntropyLoss().to(device)
 optimizer = torch.optim.Adam(model.parameters(),lr=0.01)#Adam 0.01
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=1,gamma=0.7)
 len_train=len(train_ds) 
 len_val=len(val_ds)  
 loss_plt=[]   
 for epoch in range(N_EPOCHS):
     train_dl = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True,
                   collate_fn=train_ds.generate_batch)
     val_dl = DataLoader(val_ds, batch_size=BATCH_SIZE, 
                   collate_fn=val_ds.generate_batch)  
     train_loss, train_acc = train_epoc(train_dl,model,criterion,optimizer,scheduler)
     valid_loss, valid_acc = val_epoc(val_dl,model,criterion)
     print("epoch",epoch)
     print(f'\tLoss: {train_loss/len_train:.8f}(train)\t|\tAcc: {train_acc/len_train * 100:.3f}%(train)')
     print(f'\tLoss: {valid_loss/len_val:.8f}(valid)\t|\tAcc: {valid_acc/len_val * 100:.3f}%(valid)')
     loss_plt.append(train_acc/len_train)
示例#3
0
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              **kwargs)
    print("Done loading datasets!")

    # LSTM Model:
    model = model.ReinforcedLSTM(IMAGE_SIZE, HIDDEN_NODES, HIDDEN_LAYERS,
                                 OUTPUT_CLASSES, args.batch_size, args.cuda)

    if args.cuda:
        print("\n---Activating GPU Training---\n")
        model.cuda()

    ### PRINTING AMOUNT OF PARAMETERS ###
    print('Number of model parameters: {}'.format(
        sum([p.data.nelement() for p in model.parameters()])))

    best_accuracy = 0.0

    acc_dict = {1: [], 2: [], 5: [], 10: []}
    test_acc_dict = {1: [], 2: [], 5: [], 10: []}
    total_accuracy = []
    total_loss = []

    ### LOADING PREVIOUS NETWORK ###
    if args.load_checkpoint:
        if os.path.isfile(args.load_checkpoint):
            print("=> loading checkpoint '{}'".format(args.load_checkpoint))
            checkpoint = torch.load(args.load_checkpoint)
            args.start_epoch = checkpoint['epoch']
            episode = checkpoint['episode']