val_batch_size = 10
test_batch_size = 1
batching = Batching()
train_data = batching.batchify(corpus.train, BATCH_SIZE, args)
val_data = batching.batchify(corpus.valid, val_batch_size, args)
test_data = batching.batchify(corpus.test, test_batch_size, args)

print("done batchifying")

num_tokens = len(corpus.dictionary)
model = model.RNNModel(num_tokens)

# enable GPU model when run with cuda
if args.cuda:
    print("Using cuda")
    model.cuda()

# define loss
criterion = nn.CrossEntropyLoss()
loss_least = 100000000


def evaluate(data, mode):
    if mode == 'val_mode':
        bsz = val_batch_size
    elif mode == 'test_mode':
        bsz = test_batch_size
    global loss_least
    global num_tokens
    model.eval()
    total_loss = 0
예제 #2
0
config = DefaultConfig()

if config.use_hyperboard:
    from hyperboard import Agent
    agent = Agent(username='******', password='******', port=5005)
    parameter = config.todict()
    validate_loss_record = agent.register(parameter, 'loss', overwrite=True)

train_dataset = dataset.MyDataset()
validate_dataset = dataset.MyDataset()

criticer = torch.nn.MSELoss()
model = model.Model()
optimizer = optim.Adam(model.parameters(), lr=config.lr)
if config.gpu >= 0:
    model.cuda(config.gpu)

max_loss = 0
no_gain = 0
global_step = 0
train_num = len(train_dataset)

model.train()
for epoch in range(config.epoch_num):
    train_loader = DataLoader(train_dataset,
                              batch_size=config.batch_size,
                              shuffle=True)

    for step, (traindata, trainlabel) in enumerate(train_loader):
        traindata = Variable(traindata).float()
        trainlabel = Variable(trainlabel).float()
예제 #3
0
if args.resume is not None:
    print('loading pretrained model from {}'.format(args.resume))
    checkpoint = torch.load(args.resume,
                            map_location=lambda storage, loc: storage)
    model.load_state_dict(checkpoint['state_dict'])
    del checkpoint
criterion = CTCLoss()

global image, text, length
image = torch.FloatTensor(args.batch_size, 1, args.height, 500)
text = torch.LongTensor(args.batch_size * 10)
length = torch.LongTensor(args.batch_size)

if (torch.cuda.is_available() and args.cuda):
    model = model.cuda()
    image = image.cuda()
    text = text.cuda()
    criterion = criterion.cuda()

image = Variable(image)
text = Variable(text)
length = Variable(length)


def train(data_loader):
    total_loss = 0
    model.train()
    data_loader = tqdm(data_loader)
    for idx, (cpu_images, cpu_texts) in enumerate(data_loader):
        batch_size = cpu_images.size(0)
예제 #4
0
    train_dataset = IGVCDataset(train_txt, im_size=args.im_size, split='train', transform=transform, val_samples=args.val_samples)
    val_dataset = IGVCDataset(train_txt, im_size=args.im_size, split='val', transform=transform, val_samples=args.val_samples)
    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, **kwargs)
    val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batch_size, shuffle=True, **kwargs)
    
    # Optmizer
    lr = args.lr
    print('Initial lr: %f.' % lr)
    optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=args.weight_decay)
else:
    test_dataset = IGVCDataset(test_txt, im_size=args.im_size, split='test', transform=transform)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=True, **kwargs)

criterion = F.binary_cross_entropy
if args.cuda:
    model.cuda()

def train(epoch):
    iters = []
    lrs = []
    train_losses = []
    val_losses = []
    val_accuracies = []
    model.train()
    # train loop
    for batch_idx, batch in enumerate(train_loader):
        # prepare data
        images = Variable(batch[0])
        targets = Variable(batch[1])

        if args.cuda:
    len(FACTORS_LIST))  # Generate the batches for the validation data
test_data = batchify(
    testingData, EVAL_BATCH_SIZE,
    len(FACTORS_LIST))  # Generate the batches for the testing data

###############################################################################
# Build the model
###############################################################################

print("Constructing Model")
outputVocabSize = len(corpus.getFactorDict(WORD_FACTOR))
model = model.FactoredRNN(MODEL_TYPE, myFactorsInfo, outputVocabSize,
                          NUMBER_HIDDEN, NUMBER_LAYERS,
                          DROPOUT_PERCENT)  #Initialize the model
if USE_CUDA:  #If we have access to a GPU
    model.cuda()  #Load the model on the GPU

criterion = nn.CrossEntropyLoss(
)  #Define our loss function as the CrossEntropyLoss

###############################################################################
# Training code
###############################################################################


#Wraps hidden states in new Variables, to detach them from their history.
def repackage_hidden(h):
    if type(h) == Variable:  # If we are dealing with a variable
        return Variable(
            h.data)  # Then extract the data and return it in a new variable
    else:  # If it isnt a variable, then assume that its iterable