Пример #1
0
def main(l, b, e):
    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])

    model = Model(l, b, e)
    model.train(train_x, train_y)

    output = []
    for x, y in zip(test_x, test_y):
        output.append((model.test(x), y))

    print(test.accuracy(output))
    return test.accuracy(output)
Пример #2
0
 def TestTask(self):
     text_list = [
         "При решении Р.С. с помощью метода Зейделя \n Nmax = ",
         " и eps = ", " за S = ",
         " итераций было получено решение \n с точностью eps_max = ",
         " и максимальной невязкой ||r|| = ",
         "\nМаксимальная погрешность решения тестовой задачи = ",
         " при х = ", " и у = "
     ]
     n = np.int(self.x_num_2.text())
     m = np.int(self.y_num_2.text())
     h = 2. / n
     k = 1. / m
     eps = np.float64(self.accuracy_2.text())
     nmax = np.int(self.breaks_2.text())
     x, y, v = test.grid(0, 2, 0, 1, n, m)
     ss, ee, v = test.zeidel(n, m, 0, 2, 0, 1, x, y, v, eps, nmax)
     max_nev = test.discrepancy(v, n, m, x, y, h, k)
     self.Table(v, n, m)
     Max, MaxX, MaxY = test.accuracy(n, m, x, y, v)
     text_list.insert(1, str(nmax))
     text_list.insert(3, str(eps))
     text_list.insert(5, str(ss))
     text_list.insert(7, str(ee))
     text_list.insert(9, str(max_nev))
     text_list.insert(11, str(Max))
     text_list.insert(13, str(MaxX))
     text_list.insert(15, str(MaxY))
     text = ''.join(text_list)
     self.textEdit.setText(text)
Пример #3
0
def main():

    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])
    dic = {}

    for x, y in zip(train_x, train_y):
        dic[y] = []

    for x, y in zip(train_x, train_y):
        dic[y].append(x)

    dic_centroid = {}

    for x,y in dic.items():
        y = np.asarray(y)
        mean_val = y.mean(axis = 0)
        # print(mean_val)
        dic_centroid[x] = mean_val

    print("No. of classes = ", len(dic))

    output = []

    for x,y in zip(test_x, test_y):
        min_dist = sys.maxsize
        for a,b in dic_centroid.items():
            b = np.asarray(b)
            dist = distance(x, b)
            if dist < min_dist:
                min_dist = dist
                class_predicted = a

        output.append((class_predicted, y))

    print("Accuracy = ", test.accuracy(output))
Пример #4
0
def main():

    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])
    datapoints = [DataPoint(x, y) for x, y in zip(train_x, train_y)]

    k = int(input("Enter K\n"))
    output = []

    for x, y in zip(test_x, test_y):

        que = Q.PriorityQueue(maxsize=k)
        for p in datapoints:

            p.setDistance(x)
            if que.full():
                q = que.queue[0]
                if p < q:
                    que.get()
                    que.put(p)
            else:
                que.put(p)

        output.append((analyse_queue(que), y))

    print("Accuracy = ", test.accuracy(output))
Пример #5
0
def disable_filter2(model):
    conv_layer = model.cnn[4]
    weights = conv_layer.weight
    biases = conv_layer.bias
    print(model)

    old_weight = weights[0].clone()
    old_bias = biases[0].clone()

    zero_weight = torch.zeros(weights.size()[1], weights.size()[2])
    zero_bias = torch.zeros(1)

    predictions = []

    for i in range(weights.size()[0]):
        if i != 0:
            weights[i - 1] = old_weight
            biases[i - 1] = old_bias

        old_weight = weights[i].clone()
        old_bias = biases[i].clone()
        weights[i] = zero_weight
        biases[i] = zero_bias

        print(f"Index {i}")
        prediction = accuracy(model, x_attack, y_attack, dk_plain)
        predictions.append(prediction.cpu().numpy())

    return predictions
Пример #6
0
def testing(testx, testy, mean_train, ff, feature_matrix, eigen_faces):
    a, b = testx.shape
    prediction = []

    for test_image in testx.T:
        test_image = test_image.reshape(a, 1) - mean_train
        PEF = np.matmul(eigen_faces, test_image)

        Projected_Fisher_Test_Img = np.matmul(np.transpose(feature_matrix),
                                              PEF)
        predicted_class = min_distance(ff, Projected_Fisher_Test_Img)

        if predicted_class != "imposter":
            prediction.append(predicted_class)
        else:
            prediction.append(-1)
    test.accuracy(prediction, testy)
Пример #7
0
def get_ranks(args, network_name, model_params):
    # Load the data and make it global
    global x_attack, y_attack, dk_plain, key_guesses
    x_attack, y_attack, key_guesses, real_key, dk_plain = load_data(
        args, network_name)

    folder = "{}/{}/".format(args.models_path, generate_folder_name(args))

    # Calculate the predictions before hand
    predictions = []
    for run in args.runs:
        model_path = '{}/model_r{}_{}.pt'.format(
            folder, run, get_save_name(network_name, model_params))
        print('path={}'.format(model_path))

        model = load_model(network_name, model_path)
        model.eval()
        print("Using {}".format(model))
        model.to(args.device)

        # Calculate predictions
        if require_domain_knowledge(network_name):
            prediction = accuracy(model, x_attack, y_attack, dk_plain)
            predictions.append(prediction.cpu().numpy())
        else:
            prediction = accuracy(model, x_attack, y_attack, None)
            predictions.append(prediction.cpu().numpy())

    # Check if it is only one run, if so don't do multi threading
    if len(args.runs) == 1:
        threaded_run_test(args, predictions[0], folder, args.runs[0],
                          network_name, model_params, real_key)
    else:
        # Start a thread for each run
        processes = []
        for i, run in enumerate(args.runs):
            p = Process(target=threaded_run_test,
                        args=(args, predictions[i], folder, run, network_name,
                              model_params, real_key))
            processes.append(p)
            p.start()
        # Wait for them to finish
        for p in processes:
            p.join()
            print('Joined process')
Пример #8
0
def output(weight, bias, testx, testy):
    y = []
    maxx = np.max(testy, axis=0)
    minn = np.min(testy, axis=0)
    for i in range(len(testx)):
        temp = np.add(np.matmul(testx[i], weight), bias)
        # temp = temp*(maxx - minn) + minn
        y.append(temp)
    # print_y(y, testy)
    average_error = test.accuracy(y, testy)
    print(average_error)
def main():
    trainx, trainy, testx, testy = load_data.load_data()
    w, y1 = calc_hypothesis(trainx, trainy)
    y1 = np.reshape(y1, (np.shape(y1)[0], ))

    # train_accuracy = test.accuracy(y1, trainy)
    # print(train_accuracy)

    testx, _ = convert(testx)
    n = len(testx)
    y_dash = []
    for i in range(n):
        y_dash.append(np.matmul(testx[i], w))

    test.compare(y_dash, testy)
    test_accuracy = test.accuracy(y_dash, testy)
    print(test_accuracy)
Пример #10
0
def main():
    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])
    types = {}

    #training
    for x, y in zip(train_x, train_y):
        if types.get(y) == None:
            types[y] = Type(y)
        types[y].addPoint(x)
    #end

    #testing
    output = []
    for x, y in zip(test_x, test_y):
        ans = min([(val.getDistance(x), key) for key, val in types.items()])[1]
        output.append((ans, y))

    print(test.accuracy(output))
Пример #11
0
def disable_filter3(model):
    model.to(util.device)
    conv_layer = model.cnn[0]
    print(model)

    weights = conv_layer.weight.clone()
    biases = conv_layer.bias.clone()

    conv_layer.weight = torch.nn.Parameter(
        torch.zeros(weights.size()[0],
                    weights.size()[1],
                    weights.size()[2]).float().to(util.device))
    conv_layer.bias = torch.nn.Parameter(
        torch.zeros(weights.size()[0]).float().to(util.device))
    conv_layer.weight.to(util.device)
    conv_layer.bias.to(util.device)

    predictions = []
    correct_indices = []
    sum_indices = [0] * len(y_attack)

    for i in range(weights.size()[0]):
        if i != 0:
            conv_layer.weight[i - 1] = torch.zeros(weights.size()[1],
                                                   weights.size()[2])
            conv_layer.bias[i - 1] = torch.zeros(weights.size()[1])

        conv_layer.weight[i] = weights[i]
        conv_layer.bias[i] = biases[i]

        print(f"Index {i}")
        prediction = accuracy(model, x_attack, y_attack, dk_plain)

        max_values, indices = prediction.max(1)
        res = indices.long() == torch.from_numpy(
            y_attack.reshape(len(y_attack))).long().to(util.device)

        sum_indices += res.cpu().numpy()
        correct_index = res.nonzero().view(-1)

        correct_indices.append(correct_index)
        predictions.append(prediction.cpu().numpy())

    return predictions, correct_indices, sum_indices
Пример #12
0
def main():
    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])
    
    train_y = scale_classes(train_y)
    test_y = scale_classes(test_y)
    
    mean = np.mean(train_x)
    sd = np.std(train_y)
    train_x = normalize_data(train_x,mean,sd)
    test_x = normalize_data(test_x,mean,sd)     # test data is normalized using mean and sd of train data

    b,l,e = getParameters()
    model = Perceptron(b,l,e)
    model.train(train_x, train_y)

    output = []
    for x,y in zip(test_x, test_y):
        output.append((model.test(x) , y))

    print("Accuracy = ", test.accuracy(output))
Пример #13
0
def disable_filter(model):
    conv_layer = model.cnn[0]
    weights = conv_layer.weight
    biases = conv_layer.bias
    print(model)

    old_weight = weights[0].clone()
    old_bias = biases[0].clone()

    zero_weight = torch.zeros(weights.size()[1], weights.size()[2])
    zero_bias = torch.zeros(1)

    predictions = []
    correct_indices = []
    sum_indices = [0] * len(y_attack)

    for i in range(weights.size()[0]):
        if i != 0:
            weights[i - 1] = old_weight
            biases[i - 1] = old_bias

        old_weight = weights[i].clone()
        old_bias = biases[i].clone()
        weights[i] = zero_weight
        biases[i] = zero_bias

        print(f"Index {i}")
        prediction = accuracy(model, x_attack, y_attack, dk_plain)

        print(prediction)
        max_values, indices = prediction.max(1)
        res = indices.long() == torch.from_numpy(
            y_attack.reshape(len(y_attack))).long().to(util.device)

        sum_indices += res.cpu().numpy()
        correct_index = res.nonzero().view(-1)

        correct_indices.append(correct_index)
        predictions.append(prediction.cpu().numpy())

    return predictions, correct_indices, sum_indices
Пример #14
0
def output(weight, bias, testx, testy, maxx, minn):
    y = []
    count = 0
    for i in range(len(testx)):
        temp = np.add(np.matmul(testx[i], weight), bias)
        temp = sigmoid_aux(temp)
        if temp == 1:
            count += 1
        y.append(temp)
    # print_y(y, testy)
    testy = denormalize(testy, maxx, minn)
    y = np.asarray(y)
    y = y.astype(float)
    y = denormalize(y, maxx, minn)
    accuracy = test.accuracy(y, testy)
    cnt = 0

    for i in testy:
        if i == 1:
            cnt += 1
    # print("actual ones = ", cnt)
    # print("estimated ones = ",count, len(testx))
    print("\n******************Accuracy******************\n")
    print("Accuracy = ", accuracy, "%")
Пример #15
0
def get_ranks(use_hw,
              runs,
              train_size,
              epochs,
              lr,
              sub_key_index,
              attack_size,
              rank_step,
              unmask,
              network_name,
              kernel_size_string=""):
    ranks_x = []
    ranks_y = []
    (_, _), (x_attack,
             y_attack), (metadata_profiling,
                         metadata_attack) = load_ascad(trace_file,
                                                       load_metadata=True)
    key_guesses = util.load_csv('{}/ASCAD/key_guesses.csv'.format(traces_path),
                                delimiter=' ',
                                dtype=np.int,
                                start=0,
                                size=attack_size)
    x_attack = x_attack[:attack_size]
    y_attack = y_attack[:attack_size]
    if unmask:
        if use_hw:
            y_attack = np.array([
                y_attack[i] ^ metadata_attack[i]['masks'][0]
                for i in range(len(y_attack))
            ])
        else:
            y_attack = np.array([
                util.HW[y_attack[i] ^ metadata_attack[i]['masks'][0]]
                for i in range(len(y_attack))
            ])
    real_key = metadata_attack[0]['key'][sub_key_index]

    for run in runs:
        folder = '{}/{}/subkey_{}/{}{}{}_SF{}_' \
                 'E{}_BZ{}_LR{}/train{}/'.format(
                    models_path,
                    str(data_set),
                    sub_key_index,
                    '' if unmask else 'masked/',
                    '' if desync is 0 else 'desync{}/'.format(desync),
                    type_network,
                    spread_factor,
                    epochs,
                    batch_size,
                    '%.2E' % Decimal(lr),
                    train_size)
        model_path = '{}/model_r{}_{}{}.pt'.format(folder, run, network_name,
                                                   kernel_size_string)
        print('path={}'.format(model_path))

        model = load_model(network_name, model_path)
        model.eval()
        print("Using {}".format(model))
        model.to(device)

        # Load additional plaintexts
        dk_plain = None
        if network_name in req_dk:
            dk_plain = metadata_attack[:]['plaintext'][:, sub_key_index]
            dk_plain = hot_encode(dk_plain,
                                  9 if use_hw else 256,
                                  dtype=np.float)

        # Calculate predictions
        predictions = accuracy(model, x_attack, y_attack, dk_plain)
        predictions = predictions.cpu().numpy()

        # Shuffle the data using same permutation  for n_exp and calculate mean for GE of the model
        x, y = [], []
        for exp_i in range(num_exps):
            permutation = permutations[exp_i]

            # Shuffle data
            predictions_shuffled = shuffle_permutation(permutation,
                                                       np.array(predictions))
            key_guesses_shuffled = shuffle_permutation(permutation,
                                                       key_guesses)

            # Test the data
            x_exp, y_exp = test_with_key_guess_p(key_guesses_shuffled,
                                                 predictions_shuffled,
                                                 attack_size=attack_size,
                                                 real_key=real_key,
                                                 use_hw=use_hw)
            x = x_exp
            y.append(y_exp)

        # Calculate the mean over the experiments
        y = np.mean(y, axis=0)
        util.save_np('{}/model_r{}_{}{}.exp'.format(folder, run, network_name,
                                                    kernel_size_string),
                     y,
                     f="%f")

        ranks_x.append(x)
        ranks_y.append(y)
    return ranks_x, ranks_y
Пример #16
0
from models.ConvNetKernel import ConvNetKernel
from test import accuracy
from train import train
from util import load_csv
import numpy as np

train_size = 2000
attack_size = 3000
x = load_csv('testX.csv', size=train_size)
y = load_csv('testY.csv', size=train_size)
print(np.shape(x))
print(y)

num_features = 1250
out_features = 10

network = ConvNetKernel(num_features, out_features)
train(x, y, train_size, network, epochs=80, batch_size=100, lr=0.0001)

x_test = load_csv('testX.csv', start=train_size, size=attack_size)
y_test = load_csv('testY.csv',
                  start=train_size,
                  size=attack_size,
                  dtype=np.long)
accuracy(network, x_test, y_test)
def test_features(args, model, device, train_loader_creator_l,
                  test_loader_creator, logger):
    logger.info('Evaluating linear model Began.')
    if isinstance(model, torch.nn.DataParallel):
        num_ftrs = model.module.model.classifier.in_features
        num_out = model.module.model.classifier.out_features
    else:
        num_ftrs = model.model.classifier.in_features
        num_out = model.model.classifier.out_features
    linear_model = torch.nn.Linear(num_ftrs, num_out).to(device)

    lr = 0.1
    epochs = 10
    criterion = torch.nn.CrossEntropyLoss().to(device)
    optimizer = optim.SGD(linear_model.parameters(),
                          lr=lr,
                          momentum=0.9,
                          weight_decay=1e-4)

    for task_idx, train_loader in enumerate(
            train_loader_creator_l.data_loaders):

        for param_group in optimizer.param_groups:
            param_group['lr'] = lr
        scheduler = MultiStepLR(optimizer,
                                milestones=args.milestones,
                                gamma=args.gamma)

        for epoch in range(1, epochs + 1):

            linear_model.train()
            losses = AverageMeter()
            acc = AverageMeter()

            for batch_idx, (data, _, target) in enumerate(train_loader):
                data, target = data.to(device), target.to(device)
                optimizer.zero_grad()

                h = model(data)[0].detach()
                output = linear_model(h)

                loss = criterion(output, target)

                loss.backward()
                optimizer.step()

                it_acc = accuracy(output.data, target)[0]
                losses.update(loss.item(), data.size(0))
                acc.update(it_acc.item(), data.size(0))

                if batch_idx % args.log_interval == 0:
                    logger.info(
                        'Train Task: {0} Epoch: [{1:3d}][{2:3d}/{3:3d}]\t'
                        'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                        'Acc {acc.val:.3f} ({acc.avg:.3f})'.format(
                            task_idx + 1,
                            epoch,
                            batch_idx,
                            len(train_loader),
                            loss=losses,
                            acc=acc))

            scheduler.step()

    linear_model.eval()

    with torch.no_grad():
        losses = AverageMeter()
        acc = AverageMeter()

        for test_loader in test_loader_creator.data_loaders:

            for data, _, target in test_loader:

                data, target = data.to(device), target.to(device)
                h = model(data)[0].detach()
                output = linear_model(h)

                loss = criterion(output, target)

                output = output.float()
                loss = loss.float()

                it_acc = accuracy(output.data, target)[0]
                losses.update(loss.item(), data.size(0))
                acc.update(it_acc.item(), data.size(0))

    logger.info('Test set: Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                'Acc {acc.avg:.3f}'.format(loss=losses, acc=acc))
    logger.info('Evaluating linear model Finished.')
Пример #18
0
def get_ranks(use_hw,
              runs,
              train_size,
              epochs,
              lr,
              sub_key_index,
              attack_size,
              rank_step,
              unmask,
              network_name,
              kernel_size_string=""):
    ranks_x = []
    ranks_y = []
    (_, _), (x_attack,
             y_attack), (metadata_profiling,
                         metadata_attack) = load_ascad(trace_file,
                                                       load_metadata=True)
    key_guesses = util.load_csv(
        '/media/rico/Data/TU/thesis/data/ASCAD/key_guesses.csv',
        delimiter=' ',
        dtype=np.int,
        start=0,
        size=attack_size)
    x_attack = x_attack[:attack_size]
    y_attack = y_attack[:attack_size]
    if unmask:
        if use_hw:
            y_attack = np.array([
                y_attack[i] ^ metadata_attack[i]['masks'][0]
                for i in range(len(y_attack))
            ])
        else:
            y_attack = np.array([
                util.HW[y_attack[i] ^ metadata_attack[i]['masks'][0]]
                for i in range(len(y_attack))
            ])
    real_key = metadata_attack[0]['key'][sub_key_index]

    for run in runs:
        folder = '/media/rico/Data/TU/thesis/runs2/{}/subkey_{}/{}{}{}_SF{}_' \
                     'E{}_BZ{}_LR{}/train{}/'.format(
                        str(data_set),
                        sub_key_index,
                        '' if unmask else 'masked/',
                        '' if desync is 0 else 'desync{}/'.format(desync),
                        type_network,
                        spread_factor,
                        epochs,
                        batch_size,
                        '%.2E' % Decimal(lr),
                        train_size)
        model_path = '{}/model_r{}_{}{}.pt'.format(folder, run, network_name,
                                                   kernel_size_string)
        print('path={}'.format(model_path))

        model = load_model(network_name, model_path)
        model.eval()
        print("Using {}".format(model))
        model.to(device)

        # Load additional plaintexts
        dk_plain = None
        if network_name in req_dk:
            dk_plain = metadata_attack[:]['plaintext'][:, sub_key_index]
            dk_plain = hot_encode(dk_plain,
                                  9 if use_hw else 256,
                                  dtype=np.float)

        # Calculate predictions
        predictions = accuracy(model, x_attack, y_attack, dk_plain)
        predictions = predictions.cpu().numpy()

        x, y = [], []
        for exp_i in range(num_exps):
            permutation = permutations[exp_i]

            # Shuffle data
            predictions_shuffled = shuffle_permutation(permutation,
                                                       np.array(predictions))
            key_guesses_shuffled = shuffle_permutation(permutation,
                                                       key_guesses)

            # Test the data
            x_exp, y_exp = test_with_key_guess_p(key_guesses_shuffled,
                                                 predictions_shuffled,
                                                 attack_size=attack_size,
                                                 real_key=real_key,
                                                 use_hw=use_hw)
            x = x_exp
            y.append(y_exp)

        # Calculate the mean over the experimentfs
        y = np.mean(y, axis=0)
        util.save_np('{}/model_r{}_{}{}.exp'.format(folder, run, network_name,
                                                    kernel_size_string),
                     y,
                     f="%f")

        if isinstance(model, SpreadNetIn):
            # Get the intermediate values right after the first fully connected layer
            z = np.transpose(model.intermediate_values2[0])

            # Calculate the mse for the maximum and minimum from these traces and the learned min and max
            min_z = np.min(z, axis=1)
            max_z = np.max(z, axis=1)
            msq_min = np.mean(np.square(min_z - model.tensor_min), axis=None)
            msq_max = np.mean(np.square(max_z - model.tensor_max), axis=None)
            print('msq min: {}'.format(msq_min))
            print('msq max: {}'.format(msq_max))

            # Plot the distribution of each neuron right after the first fully connected layer
            for k in [50]:
                plt.grid(True)
                plt.axvline(x=model.tensor_min[k], color='green')
                plt.axvline(x=model.tensor_max[k], color='green')
                plt.hist(z[:][k], bins=40)

                plt.show()
            exit()

            # Retrieve the intermediate values right after the spread layer,
            # and order them such that each 6 values after each other belong to the neuron of the
            # previous layer
            v = model.intermediate_values
            order = [
                int((x % spread_factor) * 100 + math.floor(x / spread_factor))
                for x in range(spread_factor * 100)
            ]
            inter = []
            for x in range(len(v[0])):
                inter.append([v[0][x][j] for j in order])

            # Calculate the standard deviation of each neuron in the spread layer
            std = np.std(inter, axis=0)
            threshold = 1.0 / attack_size * 10
            print("divby: {}".format(threshold))
            res = np.where(std < threshold, 1, 0)

            # Calculate the mean of each neuron in the spread layer
            mean_res = np.mean(inter, axis=0)
            # mean_res2 = np.where(mean_res < threshold, 1, 0)
            mean_res2 = np.where(mean_res == 0.0, 1, 0)
            print('Sum  std results {}'.format(np.sum(res)))
            print('Sum mean results {}'.format(np.sum(mean_res2)))

            # Check which neurons have a std and mean where it is smaller than threshold
            total_same = 0
            for j in range(len(mean_res2)):
                if mean_res2[j] == 1 and res[j] == 1:
                    total_same += 1
            print('Total same: {}'.format(total_same))

            # Plot the standard deviations
            plt.title('Comparison of networks')
            plt.xlabel('#neuron')
            plt.ylabel('std')
            xcoords = [j * spread_factor for j in range(100)]
            for xc in xcoords:
                plt.axvline(x=xc, color='green')
            plt.grid(True)
            plt.plot(std, label='std')
            plt.figure()

            # Plot the means
            plt.title('Performance of networks')
            plt.xlabel('#neuron')
            plt.ylabel('mean')
            for xc in xcoords:
                plt.axvline(x=xc, color='green')
            plt.grid(True)
            plt.plot(mean_res, label='mean')
            plt.legend()
            plt.show()

        ranks_x.append(x)
        ranks_y.append(y)
    return ranks_x, ranks_y
Пример #19
0
Файл: mlp.py Проект: tj96/ML
    # print("Enter learning rate:")
    # l = float(input())

    # print("Enter Max epochs:")
    # e = int(input())

    return l,b,e

if __name__ == '__main__':
    l,b,e = getParameters()
    train_x, train_y, test_x, test_y = ld.load_data(sys.argv[1])
    c = max(max(train_y),max(test_y)) + 1
    lis = []
    for i in range(len(train_y)):
        temp = [0.0]*c
        temp[train_y[i]] = 1.0
        lis.append(temp)
    train_y = lis

    M = model.Model(len(train_x[0]),l,b,e)
    M.addLayer(4)
    M.addLayer(c)

    M.train(train_x,train_y)
    output = []
    for x,y in zip(test_x,test_y):
        output.append((y, M.test(x)))

    # print(output)
    print(test.accuracy(output))
Пример #20
0
def train(args, model, device, train_loader_creator, test_loader_creator, logger):   

    criterion = torch.nn.CrossEntropyLoss().to(device)
    optimizer = optim.SGD(model.parameters(), lr=args.lr,
                          momentum=0.9, weight_decay=args.weight_decay)

    for task_idx, train_loader in enumerate(train_loader_creator.data_loaders):

        for param_group in optimizer.param_groups:
            param_group['lr'] = args.lr
        scheduler = MultiStepLR(optimizer, milestones=args.milestones, gamma=args.gamma)

        for epoch in range(1,args.epochs+1):
            
            model.train()
            losses = AverageMeter()
            acc = AverageMeter()
            batch_time = AverageMeter()
            data_time = AverageMeter()

            end = time.time()
            for batch_idx, (data, target) in enumerate(train_loader):
                data_time.update(time.time() - end)

                data, target = data.to(device), target.to(device)
                optimizer.zero_grad()

                _, output = model(data)

                loss = criterion(output, target)

                loss.backward()                
                optimizer.step()

                it_acc = accuracy(output.data, target)[0]
                losses.update(loss.item(), data.size(0))
                acc.update(it_acc.item(), data.size(0))

                batch_time.update(time.time() - end)
                end = time.time()

                if batch_idx % args.log_interval == 0:
                    logger.info('Train Task: {0} Epoch: [{1:3d}][{2:3d}/{3:3d}]\t'
                        'DTime {data_time.avg:.3f}\t'
                        'BTime {batch_time.avg:.3f}\t'
                        'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                        'Acc {acc.val:.3f} ({acc.avg:.3f})'.format(
                            task_idx+1, epoch, batch_idx, len(train_loader),
                            batch_time=batch_time, data_time=data_time, loss=losses, acc=acc))

            scheduler.step()
            if epoch % args.test_interval == 0:
                test(args, model, device, test_loader_creator, logger)

        # plot_embedding_tsne(args, task_idx, test_loader_creator, model, device)
        if args.save_model:
            model_path = args.vis_base_dir.split('/')[-2] + 'T' + str(task_idx+1) + '.pt'
            if isinstance(model, torch.nn.DataParallel):
                torch.save(model.module.state_dict(), model_path)
            else:
                torch.save(model.state_dict(), model_path)