Beispiel #1
0
	def cuda(self):
		""" If the trainset/valset are lists of tensors, this pushes them 
			to cuda
		"""
		for attr in 'trainset', 'valset':
			if isinstance(getattr(self, attr), list):
				setattr(self, attr, [cudafy(_) for _ in getattr(self, attr)])
		return
Beispiel #2
0
def test_validation(network, valset, loss_functional=None, use_cuda=False):
	""" Report the top1 accuracy of network over the
		if loss_functional is None:
			Returns top1 accuracy in range [0.0, 1.0]
		else:
			Returns average loss value over valset
		(Assumes devices are already set)
	"""
	total = 0
	count_value = 0
	for examples, labels in valset:
		if examples.dtype != network.dtype:
			examples = examples.type(network.dtype)
		if use_cuda:
			examples, labels = cudafy([examples, labels])
		total += labels.numel()
		if loss_functional is None:
			xout = network(examples).max(1)[1]
			xout == labels
			count_value += (network(examples).max(1)[1] == labels).sum().item()
		else:
			count_value += loss_functional(examples, labels).data.item()
	return float(count_value) / total
Beispiel #3
0
def training_loop(network, train_params, epoch_start_no=0, 
				  use_cuda=False, epoch_callback=None):
	""" Trains a network over the trainset with given loss and optimizer
	ARGS:
		network: ReLUNet - network to train
		train_params: TrainParameters - parameters object governing training
		epoch_start_no: int - number to start epochs at for print purposes
		use_cuda: bool - if True, we use CUDA to train the net. Everything 
						 gets returned on CPU

		epoch_callback: if not none, is a function that takes in arguments
					    {'network': network, 'epoch_no': epoch_no}

	RETURNS:
		None, but modifies network parameters
	"""
	use_cuda = torch.cuda.is_available() and use_cuda
	if use_cuda: 
		network.cuda()
		train_params.cuda()
	else:
		network.cpu()
		train_params.cpu()

	# Unpack the parameter object
	if train_params.optimizer is None:
		optimizer = optim.Adam(network.parameters(), lr=0.001, weight_decay=0)
	else:
		optimizer = train_params.optimizer 

	if train_params.test_after_epoch is False:
		test_after_epoch = float('inf')
	else:
		test_after_epoch = int(train_params.test_after_epoch)

	if train_params.loss_functional is None:
		loss_functional = LossFunctional(regularizers=[XEntropyReg()])
	else:
		loss_functional = train_params.loss_functional
	loss_functional.attach_network(network)

	# Do the training loop
	for epoch_no in range(epoch_start_no, epoch_start_no + train_params.num_epochs + 1):
		if epoch_callback is not None:
			epoch_callback(network=network, epoch_no=epoch_no)
		
		for i, (examples, labels) in enumerate(train_params.trainset):
			if examples.dtype != network.dtype:
				examples = examples.type(network.dtype)
			if use_cuda:
				examples, labels = cudafy([examples, labels])

			optimizer.zero_grad()
			loss = loss_functional(examples, labels)
			loss.backward()
			optimizer.step()
		if (epoch_no) % test_after_epoch == 0:
			# If we run test accuracy this test
			test_acc = test_validation(network, train_params.valset, 
									   use_cuda=use_cuda)
			test_acc_str = '| Accuracy: %.02f' % (test_acc * 100)
			print(("Epoch %02d " % epoch_no) + test_acc_str)


	if use_cuda:
		network = network.cpu()
		train_params.cpu()