t = time.time()
input = svrt.generate_vignettes(p, target)
t = time.time() - t
- log_string('DATA_SET_GENERATION {:.02f} sample/s'.format(n / t))
+ log_string('data_set_generation {:.02f} sample/s'.format(n / t))
input = input.view(input.size(0), 1, input.size(1), input.size(2)).float()
return Variable(input), Variable(target)
# -- full(120x84) -> 84 1
# -- full(84x2) -> 2 1
-class Net(nn.Module):
+class AfrozeShallowNet(nn.Module):
def __init__(self):
- super(Net, self).__init__()
+ super(AfrozeShallowNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=21)
self.conv2 = nn.Conv2d(6, 16, kernel_size=19)
self.conv3 = nn.Conv2d(16, 120, kernel_size=18)
x = self.fc2(x)
return x
-def train_model(train_input, train_target):
- model, criterion = Net(), nn.CrossEntropyLoss()
-
- nb_parameters = 0
- for p in model.parameters():
- nb_parameters += p.numel()
- log_string('NB_PARAMETERS {:d}'.format(nb_parameters))
+def train_model(model, train_input, train_target):
+ criterion = nn.CrossEntropyLoss()
if torch.cuda.is_available():
- model.cuda()
criterion.cuda()
optimizer, bs = optim.SGD(model.parameters(), lr = 1e-2), 100
model.zero_grad()
loss.backward()
optimizer.step()
- log_string('TRAIN_LOSS {:d} {:f}'.format(k, acc_loss))
+ log_string('train_loss {:d} {:f}'.format(k, acc_loss))
return model
######################################################################
for arg in vars(args):
- log_string('ARGUMENT ' + str(arg) + ' ' + str(getattr(args, arg)))
+ log_string('argument ' + str(arg) + ' ' + str(getattr(args, arg)))
for problem_number in range(1, 24):
train_input, train_target = generate_set(problem_number, args.nb_train_samples)
test_input, test_target = generate_set(problem_number, args.nb_test_samples)
+ model = AfrozeShallowNet()
if torch.cuda.is_available():
train_input, train_target = train_input.cuda(), train_target.cuda()
test_input, test_target = test_input.cuda(), test_target.cuda()
+ model.cuda()
mu, std = train_input.data.mean(), train_input.data.std()
train_input.data.sub_(mu).div_(std)
test_input.data.sub_(mu).div_(std)
- model = train_model(train_input, train_target)
+ nb_parameters = 0
+ for p in model.parameters():
+ nb_parameters += p.numel()
+ log_string('nb_parameters {:d}'.format(nb_parameters))
+
+ model_filename = 'model_' + str(problem_number) + '.param'
+
+ try:
+ model.load_state_dict(torch.load(model_filename))
+ log_string('loaded_model ' + model_filename)
+ except:
+ log_string('training_model')
+ train_model(model, train_input, train_target)
+ torch.save(model.state_dict(), model_filename)
+ log_string('saved_model ' + model_filename)
nb_train_errors = nb_errors(model, train_input, train_target)
- log_string('TRAIN_ERROR {:d} {:.02f}% {:d} {:d}'.format(
+ log_string('train_error {:d} {:.02f}% {:d} {:d}'.format(
problem_number,
100 * nb_train_errors / train_input.size(0),
nb_train_errors,
nb_test_errors = nb_errors(model, test_input, test_target)
- log_string('TEST_ERROR {:d} {:.02f}% {:d} {:d}'.format(
+ log_string('test_error {:d} {:.02f}% {:d} {:d}'.format(
problem_number,
100 * nb_test_errors / test_input.size(0),
nb_test_errors,