def maximize(self, flags): self.network.eval() self.train_data.transform = self.preprocess self.train_loader = torch.utils.data.DataLoader( self.train_data, batch_size=flags.batch_size, shuffle=False, num_workers=flags.num_workers, pin_memory=True) images, labels = [], [] for i, (images_train, labels_train) in enumerate(self.train_loader): # wrap the inputs and labels in Variable inputs, targets = images_train.cuda(), labels_train.cuda() # forward with the adapted parameters inputs_embedding = self.network(x=inputs)[-1]['Embedding'].detach().clone() inputs_embedding.requires_grad_(False) inputs_max = inputs.detach().clone() inputs_max.requires_grad_(True) optimizer = sgd(parameters=[inputs_max], lr=flags.lr_max) for ite_max in range(flags.loops_adv): tuples = self.network(x=inputs_max) # loss loss = self.loss_fn(tuples[0], targets) + flags.eta * entropy_loss(tuples[0]) - \ flags.gamma * self.dist_fn(tuples[-1]['Embedding'], inputs_embedding) # init the grad to zeros first self.network.zero_grad() optimizer.zero_grad() # backward your network (-loss).backward() # optimize the parameters optimizer.step() flags_log = os.path.join(flags.logs, 'max_loss_log.txt') write_log('ite_adv:{}, {}'.format(ite_max, loss.item()), flags_log) inputs_max = inputs_max.detach().clone().cpu() for j in range(len(inputs_max)): input_max = self.image_denormalise(inputs_max[j]) input_max = self.image_transform(input_max.clamp(min=0.0, max=1.0)) images.append(input_max) labels.append(labels_train[j].item()) return np.stack(images), labels
def fit_and_plot(lambd): epochs = 100 lr = 0.003 params = init_params() train_ls, test_ls = [], [] for epoch in range(epochs): for X, y in train_iter: l = loss(net(X, *params), y) + lambd * l2_penalty(params[0]) l = l.sum() for param in params: if param.grad is not None: param.grad.data.zero_() l.backward() sgd(params, lr, batch_size) train_ls.append( loss(net(train_features, *params), train_labels).mean().item()) test_ls.append( loss(net(test_features, *params), test_labels).mean().item()) plt.semilogy(range(1, epochs + 1), train_ls, label='train loss') plt.semilogy(range(1, epochs + 1), test_ls, label='test loss') plt.legend() plt.show() print('L2 norm of w:', params[0].norm().item())
def maximize(self, flags): self.network.eval() images_train, labels_train = self.batImageGenTrain.images, self.batImageGenTrain.labels images, labels = [], [] for start, end in zip(range(0, len(labels_train), flags.batch_size), range(flags.batch_size, len(labels_train), flags.batch_size)): inputs, targets = torch.from_numpy( np.array(images_train[start:end], dtype=np.float32)), torch.from_numpy( np.array(labels_train[start:end], dtype=np.float32)) # wrap the inputs and labels in Variable inputs, targets = Variable(inputs, requires_grad=False).cuda(), \ Variable(targets, requires_grad=False).long().cuda() inputs_embedding = self.network(x=inputs)[-1]['Embedding'].detach().clone() inputs_embedding.requires_grad_(False) inputs_max = inputs.detach().clone() inputs_max.requires_grad_(True) optimizer = sgd(parameters=[inputs_max], lr=flags.lr_max) for ite_max in range(flags.loops_adv): tuples = self.network(x=inputs_max) # loss loss = self.loss_fn(tuples[0], targets) + flags.eta * entropy_loss(tuples[0]) - \ flags.gamma * self.dist_fn(tuples[-1]['Embedding'], inputs_embedding) # init the grad to zeros first self.network.zero_grad() optimizer.zero_grad() # backward your network (-loss).backward() # optimize the parameters optimizer.step() flags_log = os.path.join(flags.logs, 'max_loss_log.txt') write_log('ite_adv:{}, {}'.format(ite_max, loss.item()), flags_log) inputs_max = inputs_max.detach().clone().clamp(min=0.0, max=1.0) images.append(inputs_max.cpu().numpy()) labels.append(targets.cpu().numpy()) return np.concatenate(images), np.concatenate(labels)
H1 = dropout(H1, 0.2) H2 = (torch.matmul(H1, w2) + b2).relu() if is_training: H2 = dropout(H2, 0.5) return torch.matmul(H2, w3) + b3 # 定义损失函数 loss = torch.nn.CrossEntropyLoss() # 训练模型 lr = 100 num_epochs = 5 for epoch in range(num_epochs): train_loss_sum, train_acc_sum, n = 0.0, 0.0, 0 for X, y in train_iter: y_hat = net(X) l = loss(y_hat, y) for param in params: if param.grad is not None: param.grad.data.zero_() l.backward() sgd(params, batch_size=batch_size, lr=lr) train_loss_sum += l train_acc_sum += (y_hat.argmax(dim=1) == y).sum().item() n += y.shape[0] test_acc = evaluate_accuracy(test_iter, net) print("epoch: %d, train loss: %.4f, train acc: %.3f, test acc: %.3f" % \ (epoch + 1, train_loss_sum / n, train_acc_sum / n, test_acc))
u1 = torch.matmul(X.view(X.shape[0], -1), W1) + b1 a1 = relu(u1) return torch.matmul(a1, W2) + b2 # 定义损失函数 loss = torch.nn.CrossEntropyLoss() # 定义优化器 # 已经定义了优化器sgd # 训练网络 epochs = 5 lr = 100 for epoch in range(epochs): train_loss_sum, train_acc_num, n = 0.0, 0.0, 0 for X, y in train_iter: y_hat = net(X) l = loss(y_hat, y) for param in params: if param.grad is not None: param.grad.data.zero_() l.backward() sgd(params, lr, batch_size) train_loss_sum += l.item() train_acc_num += (y_hat.argmax(dim=1) == y).sum().item() n += y.shape[0] test_accuracy = evaluate_accuracy(test_iter, net) print("epoch: %d, train loss: %.4f, train accuracy rate: %.3f, test accuracy: %.3f" % \ (epoch + 1, train_loss_sum / n, train_acc_num / n, test_accuracy))