Example #1
0
def classify_experiment(filename_train,
                        filename_test,
                        save_path,
                        range_start,
                        range_end,
                        range_step,
                        ap_N,
                        num_inter_samples):
  """
  Supervised Classification Task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param range_start: number, start number of neuron range
  @param range_end: number, end number of neuron range
  @param range_step: number, neuron step
  """
  
  # Load data
  train_data, test_data=load_arlab_feature(filename_train, filename_test);
  
  # Global paramete settings
  num_classes=int(np.max(train_data[:,0])+1);
  num_train=train_data.shape[0];
  num_test=test_data.shape[0];
  
  neuron_range=np.arange(range_start, range_end+range_step, range_step);
  
  train_input, train_label, test_input, test_label=util.parse_arlab_feature(train_data, test_data);
  
  print test_label[0:100];
  train_input, test_input=util.normalize_arlab_feature(train_input, test_input);
  train_input=train_input.T;
  test_input=test_input.T;
  _, tr_start_idx=np.unique(train_label, return_index=True);
  #_, te_start_idx=np.unique(test_label, return_index=True);
  
  print "[MESSAGE] Data is prepared.";
  
  for trail in xrange(len(neuron_range)):
    start_time=time.clock();
    
    ## parameter settings
    save_file=open(save_path, "a+");
    num_in=train_input.shape[0];
    num_neuron=neuron_range[trail];
    print "[MESSAGE] Trail %d --- Number of Neuron: %d" % ((trail+1), num_neuron);
    
    ## create network
    network=net.ConceptorNetwork(num_in=num_in,
                                 num_neuron=num_neuron,
                                 sr=1.5,
                                 in_scale=1.5,
                                 bias_scale=0.2,
                                 washout_length=0,
                                 learn_length=1,
                                 signal_plot_length=0,
                                 tychonov_alpha_readout=0.01,
                                 tychonov_alpha_readout_w=0.0001);
    
    def calculate_block(block, num_classes):
      if block==num_classes-1:
        start_idx=tr_start_idx[block];
        end_idx=train_input.shape[1];
      else:
        start_idx=tr_start_idx[block];
        end_idx=tr_start_idx[block+1];
      return start_idx, end_idx;
    
    all_train_states=np.array([]);
    for block in xrange(num_classes):
      start_idx, end_idx=calculate_block(block, num_classes);
      
      temp_train_states=network.drive_class(train_input[:, start_idx:end_idx]);
      if not all_train_states.size:
        all_train_states=temp_train_states;
      else:
        all_train_states=np.hstack((all_train_states,temp_train_states));
    print "[MESSAGE] Train data driven"
    
    R_all=all_train_states.dot(all_train_states.T);
    C_poss=[]; C_negs=[]; R_poss=[]; R_others=[];
    for block in xrange(num_classes):
      start_idx, end_idx=calculate_block(block, num_classes);
      
      C_pos_class, C_neg_class, R, R_other=network.compute_conceptor(all_train_states[:, start_idx:end_idx],
                                                                     ap_N,
                                                                     R_all,
                                                                     (num_train-(end_idx-start_idx)));
    
      C_poss.append(C_pos_class);
      C_negs.append(C_neg_class);
      R_poss.append(R);
      R_others.append(R_other);
    print "[MESSAGE] Conceptors Computed"
    
    best_aps_poss=np.zeros(num_classes);
    best_aps_negs=np.zeros(num_classes);
    for i in xrange(num_classes):
      best_aps_poss[i], best_aps_negs[i]=network.compute_aperture(C_poss[i],
                                                                  C_negs[i],
                                                                  ap_N,
                                                                  num_inter_samples);
    
    best_ap_pos=np.mean(best_aps_poss);
    best_ap_neg=np.mean(best_aps_negs);                       
    
    print "[MESSAGE] Best Positive Aperture: %.2f, Best Negative Aperture: %.2f" % (best_ap_pos, best_ap_neg);
    
    C_pos_best=[]; C_neg_best=[];
    for block in xrange(num_classes):
      start_idx, end_idx=calculate_block(block, num_classes);
      
      c_pos_best, c_neg_best=network.compute_best_conceptor(R_poss[block],
                                                            R_others[block],
                                                            best_ap_pos,
                                                            best_ap_neg,
                                                            end_idx-start_idx,
                                                            num_train-(end_idx-start_idx));
      C_pos_best.append(c_pos_best);
      C_neg_best.append(c_neg_best);
      
    print "[MESSAGE] Best conceptors computed"
    
    x_test=network.drive_class(test_input);
    xTx=x_test.T.dot(x_test).diagonal();
        
    pos_ev=np.zeros((num_classes, num_test));
    neg_ev=np.zeros((num_classes, num_test));
    comb_ev=np.zeros((num_classes, num_test));
    
    for i in xrange(num_classes):
      for j in xrange(num_test):
        pos_ev[i,j]=x_test[:,j].dot(C_pos_best[i]).dot(x_test[:,j][None].T)/xTx[j];
        neg_ev[i,j]=x_test[:,j].dot(C_neg_best[i]).dot(x_test[:,j][None].T)/xTx[j];
        comb_ev[i,j]=pos_ev[i,j]+neg_ev[i,j];
        
      print "[MESSAGE] %i class evidence is calculated" % (i+1);
    
    output_label=np.argmax(comb_ev, axis=0);
    
    pos_out_label=np.argmax(pos_ev, axis=0);
    neg_out_label=np.argmax(neg_ev, axis=0);
    
    accuracy=float(np.sum(output_label==test_label))/float(num_test);
    pos_accuracy=float(np.sum(pos_out_label==test_label))/float(num_test);
    neg_accuracy=float(np.sum(neg_out_label==test_label))/float(num_test);
    
    print "[MESSAGE] Accuracy %.2f %%" % (accuracy*100);
    
    end_time=time.clock();
    print "[MESSAGE] Total for %.2fm" % ((end_time-start_time)/60);
    
    info=np.column_stack((num_neuron, best_ap_pos, best_ap_pos, accuracy, pos_accuracy, neg_accuracy, ((end_time-start_time)/60)));
    np.savetxt(save_file, info, delimiter=',',newline='\n');
    save_file.close();
    
    ## remove variable
    
    del output_label; del pos_out_label; del neg_out_label;
    del pos_ev; del neg_ev; del comb_ev;
    del xTx;
    del network;
Example #2
0
def inc_learn_exp(filename_train, filename_test, save_path, num_neuron,
                  ap_pos):
    """
  Setup incremental learning task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param num_neuron: numbe of hidden neurons
  """

    # Load data
    train_data, test_data = load_arlab_feature(filename_train, filename_test)

    # Global paramete settings
    num_classes = int(np.max(train_data[:, 0]) + 1)
    num_train = train_data.shape[0]
    num_test = test_data.shape[0]

    train_input, train_label, test_input, test_label = util.parse_arlab_feature(
        train_data, test_data)

    train_input, test_input = util.normalize_arlab_feature(
        train_input, test_input)
    train_input = train_input.T
    test_input = test_input.T
    _, tr_start_idx = np.unique(train_label, return_index=True)
    _, te_start_idx = np.unique(test_label, return_index=True)

    print "[MESSAGE] Data is prepared."

    # setup network

    save_file = open(save_path, "a+")
    num_in = train_input.shape[0]

    network = net.ConceptorNetwork(num_in=num_in,
                                   num_neuron=num_neuron,
                                   sr=1.5,
                                   in_scale=1.5,
                                   bias_scale=0.2,
                                   washout_length=0,
                                   learn_length=1,
                                   signal_plot_length=0,
                                   tychonov_alpha_readout=0.01,
                                   tychonov_alpha_readout_w=0.0001)

    def calculate_block(set_idx, block, num_classes, dataset="train"):
        if block == num_classes - 1:
            start_idx = set_idx[block]
            if dataset == "train":
                end_idx = train_input.shape[1]
            elif dataset == "test":
                end_idx = test_input.shape[1]
        else:
            start_idx = set_idx[block]
            end_idx = set_idx[block + 1]
        return start_idx, end_idx

    all_train_states = np.array([])
    for block in xrange(num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        temp_train_states = network.drive_class(train_input[:,
                                                            start_idx:end_idx])
        if not all_train_states.size:
            all_train_states = temp_train_states
        else:
            all_train_states = np.hstack((all_train_states, temp_train_states))
    print "[MESSAGE] Train data driven"

    x_test = network.drive_class(test_input)
    xTx = x_test.T.dot(x_test).diagonal()
    print "[MESSAGE] Test data driven"

    # setup for classification 2 classes
    curr_num_classes = 2
    _, curr_num_train = calculate_block(tr_start_idx, curr_num_classes - 1,
                                        num_classes)
    _, curr_num_test = calculate_block(te_start_idx, curr_num_classes - 1,
                                       num_classes, "test")

    print "[MESSAGE] Current number of classes: %d" % (curr_num_classes)

    ## init for correlation matrix list
    R_poss = []
    for block in xrange(curr_num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        states_c = all_train_states[:, start_idx:end_idx]
        R_poss.append(states_c.dot(states_c.T))

    ## compute positive conceptors for each class
    C_pos = []
    for block in xrange(curr_num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        c_pos = network.compute_pos_conceptor(R_poss[block], ap_pos,
                                              end_idx - start_idx)
        C_pos.append(c_pos)

        print "[MESSAGE] Best conceptors computed"

    pos_ev = np.zeros((curr_num_classes, curr_num_test))
    for i in xrange(curr_num_classes):
        for j in xrange(curr_num_test):
            pos_ev[i, j] = x_test[:, j].dot(C_pos[i]).dot(
                x_test[:, j][None].T) / xTx[j]

        print "[MESSAGE] %i class evidence is calculated" % (i + 1)

    pos_out_label = np.argmax(pos_ev, axis=0)
    pos_accuracy = float(np.sum(
        pos_out_label == test_label[0:curr_num_test])) / float(curr_num_test)
    print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy * 100)

    info = np.column_stack((curr_num_classes, pos_accuracy))
    np.savetxt(save_file, info, delimiter=',', newline='\n')

    while curr_num_classes < num_classes:
        curr_num_classes += 1
        _, curr_num_train = calculate_block(tr_start_idx, curr_num_classes - 1,
                                            num_classes)
        _, curr_num_test = calculate_block(te_start_idx, curr_num_classes - 1,
                                           num_classes, "test")

        print "[MESSAGE] Current number of classes: %d" % (curr_num_classes)

        # get new R
        start_idx, end_idx = calculate_block(tr_start_idx,
                                             curr_num_classes - 1, num_classes)
        states_c = all_train_states[:, start_idx:end_idx]
        R_poss.append(states_c.dot(states_c.T))

        # get new Conceptor
        c_pos = network.compute_pos_conceptor(R_poss[curr_num_classes - 1],
                                              ap_pos, end_idx - start_idx)
        C_pos.append(c_pos)

        print "[MESSAGE] New conceptor is computed"

        pos_ev = np.zeros((curr_num_classes, curr_num_test))
        for i in xrange(curr_num_classes):
            for j in xrange(curr_num_test):
                pos_ev[i, j] = x_test[:, j].dot(C_pos[i]).dot(
                    x_test[:, j][None].T) / xTx[j]

            print "[MESSAGE] %i class evidence is calculated" % (i + 1)

        pos_out_label = np.argmax(pos_ev, axis=0)
        #print pos_out_label;
        #print test_label[0:curr_num_test];
        pos_accuracy = float(
            np.sum(pos_out_label == test_label[0:curr_num_test])) / float(
                curr_num_test)
        print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy * 100)

        info = np.column_stack((curr_num_classes, pos_accuracy))
        np.savetxt(save_file, info, delimiter=',', newline='\n')

        del pos_ev

    save_file.close()
Example #3
0
def classify_experiment(filename_train, filename_test, save_path, range_start,
                        range_end, range_step, ap_N, num_inter_samples):
    """
  Supervised Classification Task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param range_start: number, start number of neuron range
  @param range_end: number, end number of neuron range
  @param range_step: number, neuron step
  """

    # Load data
    train_data, test_data = load_arlab_feature(filename_train, filename_test)

    # Global paramete settings
    num_classes = int(np.max(train_data[:, 0]) + 1)
    num_train = train_data.shape[0]
    num_test = test_data.shape[0]

    neuron_range = np.arange(range_start, range_end + range_step, range_step)

    train_input, train_label, test_input, test_label = util.parse_arlab_feature(
        train_data, test_data)

    print test_label[0:100]
    train_input, test_input = util.normalize_arlab_feature(
        train_input, test_input)
    train_input = train_input.T
    test_input = test_input.T
    _, tr_start_idx = np.unique(train_label, return_index=True)
    #_, te_start_idx=np.unique(test_label, return_index=True);

    print "[MESSAGE] Data is prepared."

    for trail in xrange(len(neuron_range)):
        start_time = time.clock()

        ## parameter settings
        save_file = open(save_path, "a+")
        num_in = train_input.shape[0]
        num_neuron = neuron_range[trail]
        print "[MESSAGE] Trail %d --- Number of Neuron: %d" % (
            (trail + 1), num_neuron)

        ## create network
        network = net.ConceptorNetwork(num_in=num_in,
                                       num_neuron=num_neuron,
                                       sr=1.5,
                                       in_scale=1.5,
                                       bias_scale=0.2,
                                       washout_length=0,
                                       learn_length=1,
                                       signal_plot_length=0,
                                       tychonov_alpha_readout=0.01,
                                       tychonov_alpha_readout_w=0.0001)

        def calculate_block(block, num_classes):
            if block == num_classes - 1:
                start_idx = tr_start_idx[block]
                end_idx = train_input.shape[1]
            else:
                start_idx = tr_start_idx[block]
                end_idx = tr_start_idx[block + 1]
            return start_idx, end_idx

        all_train_states = np.array([])
        for block in xrange(num_classes):
            start_idx, end_idx = calculate_block(block, num_classes)

            temp_train_states = network.drive_class(
                train_input[:, start_idx:end_idx])
            if not all_train_states.size:
                all_train_states = temp_train_states
            else:
                all_train_states = np.hstack(
                    (all_train_states, temp_train_states))
        print "[MESSAGE] Train data driven"

        R_all = all_train_states.dot(all_train_states.T)
        C_poss = []
        C_negs = []
        R_poss = []
        R_others = []
        for block in xrange(num_classes):
            start_idx, end_idx = calculate_block(block, num_classes)

            C_pos_class, C_neg_class, R, R_other = network.compute_conceptor(
                all_train_states[:, start_idx:end_idx], ap_N, R_all,
                (num_train - (end_idx - start_idx)))

            C_poss.append(C_pos_class)
            C_negs.append(C_neg_class)
            R_poss.append(R)
            R_others.append(R_other)
        print "[MESSAGE] Conceptors Computed"

        best_aps_poss = np.zeros(num_classes)
        best_aps_negs = np.zeros(num_classes)
        for i in xrange(num_classes):
            best_aps_poss[i], best_aps_negs[i] = network.compute_aperture(
                C_poss[i], C_negs[i], ap_N, num_inter_samples)

        best_ap_pos = np.mean(best_aps_poss)
        best_ap_neg = np.mean(best_aps_negs)

        print "[MESSAGE] Best Positive Aperture: %.2f, Best Negative Aperture: %.2f" % (
            best_ap_pos, best_ap_neg)

        C_pos_best = []
        C_neg_best = []
        for block in xrange(num_classes):
            start_idx, end_idx = calculate_block(block, num_classes)

            c_pos_best, c_neg_best = network.compute_best_conceptor(
                R_poss[block], R_others[block], best_ap_pos, best_ap_neg,
                end_idx - start_idx, num_train - (end_idx - start_idx))
            C_pos_best.append(c_pos_best)
            C_neg_best.append(c_neg_best)

        print "[MESSAGE] Best conceptors computed"

        x_test = network.drive_class(test_input)
        xTx = x_test.T.dot(x_test).diagonal()

        pos_ev = np.zeros((num_classes, num_test))
        neg_ev = np.zeros((num_classes, num_test))
        comb_ev = np.zeros((num_classes, num_test))

        for i in xrange(num_classes):
            for j in xrange(num_test):
                pos_ev[i, j] = x_test[:, j].dot(C_pos_best[i]).dot(
                    x_test[:, j][None].T) / xTx[j]
                neg_ev[i, j] = x_test[:, j].dot(C_neg_best[i]).dot(
                    x_test[:, j][None].T) / xTx[j]
                comb_ev[i, j] = pos_ev[i, j] + neg_ev[i, j]

            print "[MESSAGE] %i class evidence is calculated" % (i + 1)

        output_label = np.argmax(comb_ev, axis=0)

        pos_out_label = np.argmax(pos_ev, axis=0)
        neg_out_label = np.argmax(neg_ev, axis=0)

        accuracy = float(np.sum(output_label == test_label)) / float(num_test)
        pos_accuracy = float(
            np.sum(pos_out_label == test_label)) / float(num_test)
        neg_accuracy = float(
            np.sum(neg_out_label == test_label)) / float(num_test)

        print "[MESSAGE] Accuracy %.2f %%" % (accuracy * 100)

        end_time = time.clock()
        print "[MESSAGE] Total for %.2fm" % ((end_time - start_time) / 60)

        info = np.column_stack(
            (num_neuron, best_ap_pos, best_ap_pos, accuracy, pos_accuracy,
             neg_accuracy, ((end_time - start_time) / 60)))
        np.savetxt(save_file, info, delimiter=',', newline='\n')
        save_file.close()

        ## remove variable

        del output_label
        del pos_out_label
        del neg_out_label
        del pos_ev
        del neg_ev
        del comb_ev
        del xTx
        del network
def super_sub_exp(filename_train,
                  filename_test,
                  save_path,
                  num_neuron,
                  ap_pos):
  """
  Setup incremental learning task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param num_neuron: numbe of hidden neurons
  """
  
  # Load data
  train_data, test_data=load_arlab_feature(filename_train, filename_test);
  
  # Global paramete settings
  num_classes=int(np.max(train_data[:,0])+1);
  num_train=train_data.shape[0];
  num_test=test_data.shape[0];
  
  train_input, train_label, test_input, test_label=util.parse_arlab_feature(train_data, test_data);
  
  train_input, test_input=util.normalize_arlab_feature(train_input, test_input);
  train_input=train_input.T;
  test_input=test_input.T;
  _, tr_start_idx=np.unique(train_label, return_index=True);
  _, te_start_idx=np.unique(test_label, return_index=True);
  
  print "[MESSAGE] Data is prepared.";
  
  # setup network
  
  save_file=open(save_path, "a+");
  num_in=train_input.shape[0];
  
  network=net.ConceptorNetwork(num_in=num_in,
                               num_neuron=num_neuron,
                               sr=1.5,
                               in_scale=1.5,
                               bias_scale=0.2,
                               washout_length=0,
                               learn_length=1,
                               signal_plot_length=0,
                               tychonov_alpha_readout=0.01,
                               tychonov_alpha_readout_w=0.0001);
                               
  def calculate_block(set_idx, block, num_classes, dataset="train"):
      if block==num_classes-1:
        start_idx=set_idx[block];
        if dataset=="train":
          end_idx=train_input.shape[1];
        elif dataset=="test":
          end_idx=test_input.shape[1];
      else:
        start_idx=set_idx[block];
        end_idx=set_idx[block+1];
      return start_idx, end_idx;
    
  all_train_states=np.array([]);
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
      
    temp_train_states=network.drive_class(train_input[:, start_idx:end_idx]);
    if not all_train_states.size:
      all_train_states=temp_train_states;
    else:
      all_train_states=np.hstack((all_train_states,temp_train_states));
  print "[MESSAGE] Train data driven"
  
  x_test=network.drive_class(test_input);
  xTx=x_test.T.dot(x_test).diagonal();
  print "[MESSAGE] Test data driven"
  
  ## init for correlation matrix list
  R_poss=[];
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
    
    states_c=all_train_states[:, start_idx:end_idx];
    R_poss.append(states_c.dot(states_c.T));
  
  ## compute positive conceptors for each class
  C_pos=[];
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
      
    c_pos=network.compute_pos_conceptor(R_poss[block],
                                        ap_pos,
                                        end_idx-start_idx);
    C_pos.append(c_pos);
    print "[MESSAGE] Conceptor %i is computed" % (block+1);
      
  print "[MESSAGE] Best conceptors computed"

  ## combine conceptors
  
  ### use CIFAR-100;
  
  C_pos_super=[];
  num_super_classes=20;
  for idx in xrange(num_super_classes):
    c_temp=logic.OR(C_pos[idx*5], C_pos[idx*5+1]);
    c_temp=logic.OR(c_temp, C_pos[idx*5+2]);
    c_temp=logic.OR(c_temp, C_pos[idx*5+3]);
    c_temp=logic.OR(c_temp, C_pos[idx*5+4]);
    
    C_pos_super.append(c_temp);
    
    print "[MESSAGE] Super conceptor %i is computed" % (idx+1);
    
  pos_ev=np.zeros((num_super_classes, num_test));
  for i in xrange(num_super_classes):
    for j in xrange(num_test):
      pos_ev[i,j]=x_test[:,j].dot(C_pos[i]).dot(x_test[:,j][None].T)/xTx[j];
        
    print "[MESSAGE] %i class evidence is calculated" % (i+1);
  
  pos_out_label=np.argmax(pos_ev, axis=0);
  pos_accuracy=float(np.sum(pos_out_label==test_label))/float(num_test);
  print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy*100);
    
    
    
    
    
    
    
    
    
    
    
  
Example #5
0
def classify_experiment(filename_train,
                        filename_test,
                        save_path,
                        ap_N,
                        num_inter_samples):
  """
  Supervised Classification Task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  """
  
  # Load data
  train_data, test_data=load_arlab_feature(filename_train, filename_test);
  
  # Global paramete settings
  num_classes=int(np.max(train_data[:,0])+1);
  num_train=train_data.shape[0];
  num_test=test_data.shape[0];
  
  train_input, train_label, test_input, test_label=util.parse_arlab_feature(train_data, test_data);
  
  train_input, test_input=util.normalize_arlab_feature(train_input, test_input);
  train_input=train_input.T;
  test_input=test_input.T;
  _, tr_start_idx=np.unique(train_label, return_index=True);
  
  print "[MESSAGE] Data is prepared.";
  
  start_time=time.clock();
  save_file=open(save_path, "a+");
  feature_size=train_input.shape[0];
  
  network=FeatureNet(feature_size=feature_size);
  
  def calculate_block(block, num_classes):
      if block==num_classes-1:
        start_idx=tr_start_idx[block];
        end_idx=train_input.shape[1];
      else:
        start_idx=tr_start_idx[block];
        end_idx=tr_start_idx[block+1];
      return start_idx, end_idx;
    
  C_poss=[]; R_poss=[];
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(block, num_classes);
      
    C_pos_class, R=network.compute_conceptor(train_input[:, start_idx:end_idx],
                                             ap_N,
                                             (num_train-(end_idx-start_idx)));
    
    C_poss.append(C_pos_class);
    R_poss.append(R);
    
  print "[MESSAGE] Conceptors Computed"
  
  best_aps_poss=np.zeros(num_classes);
  for i in xrange(num_classes):
    best_aps_poss[i]=network.compute_pos_aperture(C_poss[i],
                                                  ap_N,
                                                  num_inter_samples);
    
  best_ap_pos=np.mean(best_aps_poss);
  
  print "[MESSAGE] Best Positive Aperture: %.2f" % (best_ap_pos);
  
  C_pos_best=[];
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(block, num_classes);
      
    c_pos_best=network.compute_pos_conceptor(R_poss[block],
                                             best_ap_pos,
                                             end_idx-start_idx);
    C_pos_best.append(c_pos_best);
      
      
  print "[MESSAGE] Best conceptors computed"
  
  
  xTx=test_input.T.dot(test_input).diagonal();
        
  pos_ev=np.zeros((num_classes, num_test));
    
  for i in xrange(num_classes):
    for j in xrange(num_test):
        pos_ev[i,j]=test_input[:,j].dot(C_pos_best[i]).dot(test_input[:,j][None].T)/xTx[j];
        
  print "[MESSAGE] %i class evidence is calculated" % (i+1);
  
  pos_out_label=np.argmax(pos_ev, axis=0);
  
  pos_accuracy=float(np.sum(pos_out_label==test_label))/float(num_test);
  
  print "[MESSAGE] Pos Accuracy %.2f %%" % (pos_accuracy*100);
  
  end_time=time.clock();
  print "[MESSAGE] Total for %.2fm" % ((end_time-start_time)/60);

  info=np.column_stack((best_ap_pos, best_ap_pos, pos_accuracy, ((end_time-start_time)/60)));
  np.savetxt(save_file, info, delimiter=',',newline='\n');
  save_file.close();
Example #6
0
def super_sub_exp(filename_train, filename_test, save_path, num_neuron,
                  ap_pos):
    """
  Setup incremental learning task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param num_neuron: numbe of hidden neurons
  """

    # Load data
    train_data, test_data = load_arlab_feature(filename_train, filename_test)

    # Global paramete settings
    num_classes = int(np.max(train_data[:, 0]) + 1)
    num_train = train_data.shape[0]
    num_test = test_data.shape[0]

    train_input, train_label, test_input, test_label = util.parse_arlab_feature(
        train_data, test_data)

    train_input, test_input = util.normalize_arlab_feature(
        train_input, test_input)
    train_input = train_input.T
    test_input = test_input.T
    _, tr_start_idx = np.unique(train_label, return_index=True)
    _, te_start_idx = np.unique(test_label, return_index=True)

    print "[MESSAGE] Data is prepared."

    # setup network

    save_file = open(save_path, "a+")
    num_in = train_input.shape[0]

    network = net.ConceptorNetwork(num_in=num_in,
                                   num_neuron=num_neuron,
                                   sr=1.5,
                                   in_scale=1.5,
                                   bias_scale=0.2,
                                   washout_length=0,
                                   learn_length=1,
                                   signal_plot_length=0,
                                   tychonov_alpha_readout=0.01,
                                   tychonov_alpha_readout_w=0.0001)

    def calculate_block(set_idx, block, num_classes, dataset="train"):
        if block == num_classes - 1:
            start_idx = set_idx[block]
            if dataset == "train":
                end_idx = train_input.shape[1]
            elif dataset == "test":
                end_idx = test_input.shape[1]
        else:
            start_idx = set_idx[block]
            end_idx = set_idx[block + 1]
        return start_idx, end_idx

    all_train_states = np.array([])
    for block in xrange(num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        temp_train_states = network.drive_class(train_input[:,
                                                            start_idx:end_idx])
        if not all_train_states.size:
            all_train_states = temp_train_states
        else:
            all_train_states = np.hstack((all_train_states, temp_train_states))
    print "[MESSAGE] Train data driven"

    x_test = network.drive_class(test_input)
    xTx = x_test.T.dot(x_test).diagonal()
    print "[MESSAGE] Test data driven"

    ## init for correlation matrix list
    R_poss = []
    for block in xrange(num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        states_c = all_train_states[:, start_idx:end_idx]
        R_poss.append(states_c.dot(states_c.T))

    ## compute positive conceptors for each class
    C_pos = []
    for block in xrange(num_classes):
        start_idx, end_idx = calculate_block(tr_start_idx, block, num_classes)

        c_pos = network.compute_pos_conceptor(R_poss[block], ap_pos,
                                              end_idx - start_idx)
        C_pos.append(c_pos)
        print "[MESSAGE] Conceptor %i is computed" % (block + 1)

    print "[MESSAGE] Best conceptors computed"

    ## combine conceptors

    ### use CIFAR-100;

    C_pos_super = []
    num_super_classes = 20
    for idx in xrange(num_super_classes):
        c_temp = logic.OR(C_pos[idx * 5], C_pos[idx * 5 + 1])
        c_temp = logic.OR(c_temp, C_pos[idx * 5 + 2])
        c_temp = logic.OR(c_temp, C_pos[idx * 5 + 3])
        c_temp = logic.OR(c_temp, C_pos[idx * 5 + 4])

        C_pos_super.append(c_temp)

        print "[MESSAGE] Super conceptor %i is computed" % (idx + 1)

    pos_ev = np.zeros((num_super_classes, num_test))
    for i in xrange(num_super_classes):
        for j in xrange(num_test):
            pos_ev[i, j] = x_test[:, j].dot(C_pos[i]).dot(
                x_test[:, j][None].T) / xTx[j]

        print "[MESSAGE] %i class evidence is calculated" % (i + 1)

    pos_out_label = np.argmax(pos_ev, axis=0)
    pos_accuracy = float(np.sum(pos_out_label == test_label)) / float(num_test)
    print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy * 100)
def inc_learn_exp(filename_train,
                  filename_test,
                  save_path,
                  num_neuron,
                  ap_pos):
  """
  Setup incremental learning task
  
  @param filename_train: train data file
  @param filename_test: test data file
  @param save_path: result save path
  @param num_neuron: numbe of hidden neurons
  """
  
  # Load data
  train_data, test_data=load_arlab_feature(filename_train, filename_test);
  
  # Global paramete settings
  num_classes=int(np.max(train_data[:,0])+1);
  num_train=train_data.shape[0];
  num_test=test_data.shape[0];
  
  train_input, train_label, test_input, test_label=util.parse_arlab_feature(train_data, test_data);
  
  train_input, test_input=util.normalize_arlab_feature(train_input, test_input);
  train_input=train_input.T;
  test_input=test_input.T;
  _, tr_start_idx=np.unique(train_label, return_index=True);
  _, te_start_idx=np.unique(test_label, return_index=True);
  
  print "[MESSAGE] Data is prepared.";
  
  # setup network
  
  save_file=open(save_path, "a+");
  num_in=train_input.shape[0];
  
  network=net.ConceptorNetwork(num_in=num_in,
                               num_neuron=num_neuron,
                               sr=1.5,
                               in_scale=1.5,
                               bias_scale=0.2,
                               washout_length=0,
                               learn_length=1,
                               signal_plot_length=0,
                               tychonov_alpha_readout=0.01,
                               tychonov_alpha_readout_w=0.0001);
                               
  def calculate_block(set_idx, block, num_classes, dataset="train"):
      if block==num_classes-1:
        start_idx=set_idx[block];
        if dataset=="train":
          end_idx=train_input.shape[1];
        elif dataset=="test":
          end_idx=test_input.shape[1];
      else:
        start_idx=set_idx[block];
        end_idx=set_idx[block+1];
      return start_idx, end_idx;
    
  all_train_states=np.array([]);
  for block in xrange(num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
      
    temp_train_states=network.drive_class(train_input[:, start_idx:end_idx]);
    if not all_train_states.size:
      all_train_states=temp_train_states;
    else:
      all_train_states=np.hstack((all_train_states,temp_train_states));
  print "[MESSAGE] Train data driven"
  
  x_test=network.drive_class(test_input);
  xTx=x_test.T.dot(x_test).diagonal();
  print "[MESSAGE] Test data driven"
    
  # setup for classification 2 classes
  curr_num_classes=2;
  _, curr_num_train=calculate_block(tr_start_idx, curr_num_classes-1, num_classes);
  _, curr_num_test=calculate_block(te_start_idx, curr_num_classes-1, num_classes, "test");
  
  print "[MESSAGE] Current number of classes: %d" % (curr_num_classes);
  
  ## init for correlation matrix list
  R_poss=[];
  for block in xrange(curr_num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
    
    states_c=all_train_states[:, start_idx:end_idx];
    R_poss.append(states_c.dot(states_c.T));
  
  ## compute positive conceptors for each class
  C_pos=[];
  for block in xrange(curr_num_classes):
    start_idx, end_idx=calculate_block(tr_start_idx, block, num_classes);
      
    c_pos=network.compute_pos_conceptor(R_poss[block],
                                        ap_pos,
                                        end_idx-start_idx);
    C_pos.append(c_pos);
      
    print "[MESSAGE] Best conceptors computed"
    
  pos_ev=np.zeros((curr_num_classes, curr_num_test));
  for i in xrange(curr_num_classes):
    for j in xrange(curr_num_test):
      pos_ev[i,j]=x_test[:,j].dot(C_pos[i]).dot(x_test[:,j][None].T)/xTx[j];
        
    print "[MESSAGE] %i class evidence is calculated" % (i+1);
  
  pos_out_label=np.argmax(pos_ev, axis=0);
  pos_accuracy=float(np.sum(pos_out_label==test_label[0:curr_num_test]))/float(curr_num_test);
  print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy*100);
  
  info=np.column_stack((curr_num_classes, pos_accuracy));
  np.savetxt(save_file, info, delimiter=',',newline='\n');
  
  while curr_num_classes<num_classes:
    curr_num_classes+=1;
    _, curr_num_train=calculate_block(tr_start_idx, curr_num_classes-1, num_classes);
    _, curr_num_test=calculate_block(te_start_idx, curr_num_classes-1, num_classes, "test");
    
    print "[MESSAGE] Current number of classes: %d" % (curr_num_classes);
    
    # get new R
    start_idx, end_idx=calculate_block(tr_start_idx, curr_num_classes-1, num_classes);
    states_c=all_train_states[:, start_idx:end_idx];
    R_poss.append(states_c.dot(states_c.T));
  
    # get new Conceptor
    c_pos=network.compute_pos_conceptor(R_poss[curr_num_classes-1],
                                        ap_pos,
                                        end_idx-start_idx);
    C_pos.append(c_pos);
    
    print "[MESSAGE] New conceptor is computed"
    
    pos_ev=np.zeros((curr_num_classes, curr_num_test));
    for i in xrange(curr_num_classes):
      for j in xrange(curr_num_test):
        pos_ev[i,j]=x_test[:,j].dot(C_pos[i]).dot(x_test[:,j][None].T)/xTx[j];
        
      print "[MESSAGE] %i class evidence is calculated" % (i+1);
  
    pos_out_label=np.argmax(pos_ev, axis=0);
    #print pos_out_label;
    #print test_label[0:curr_num_test];
    pos_accuracy=float(np.sum(pos_out_label==test_label[0:curr_num_test]))/float(curr_num_test);
    print "[MESSAGE] Accuracy %.2f %%" % (pos_accuracy*100);
    
    info=np.column_stack((curr_num_classes, pos_accuracy));
    np.savetxt(save_file, info, delimiter=',',newline='\n');
    
    del pos_ev;
  
  save_file.close();