Exemplo n.º 1
0
def train():
    epochs = 5
    learning_rate = .001
    niter = 0
    losses = []
    moving_loss = 0
    smoothing_constant = .01

    # 训练
    for e in range(epochs):
        total_loss = 0

        for data, label in data_iter():
            with autograd.record():
                output = net(data)
                loss = square_loss(output, label)
            loss.backward()
            SGD(params, learning_rate)
            total_loss += nd.sum(loss).asscalar()

            # 记录每读取一个数据点后,损失的移动平均值的变化;
            niter += 1
            curr_loss = nd.mean(loss).asscalar()
            moving_loss = (1 - smoothing_constant) * moving_loss + (
                smoothing_constant) * curr_loss

            # correct the bias from the moving averages
            est_loss = moving_loss / (1 - (1 - smoothing_constant)**niter)

            if (niter + 1) % 100 == 0:
                losses.append(est_loss)
                print(
                    "Epoch %s, batch %s. Moving avg of loss: %s. Average loss: %f"
                    % (e, niter, est_loss, total_loss / num_examples))
Exemplo n.º 2
0
def train():
    for epoch in range(5):
        train_loss = 0.
        train_acc = 0.
        for data, label in train_data:
            label = label.as_in_context(ctx)
            with autograd.record():
                output = net(data)
                loss = softmax_cross_entropy(output, label)
            loss.backward()
            SGD(params, learning_rate / batch_size)

            train_loss += nd.mean(loss).asscalar()
            train_acc += accuracy(output, label)

        test_acc = evaluate_accuracy(test_data, net, ctx)
        print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" %
              (epoch, train_loss / len(train_data),
               train_acc / len(train_data), test_acc))
Exemplo n.º 3
0
def train():
    learning_rate = .1

    for epoch in range(10):
        train_loss = 0.
        train_acc = 0.
        for data, label in train_data:
            with autograd.record():
                output = net(data)
                loss = cross_entropy(output, label)
            loss.backward()
            # 将梯度做平均,这样学习率会对batch size不那么敏感
            SGD(params, learning_rate / batch_size)

            train_loss += nd.mean(loss).asscalar()
            train_acc += accuracy(output, label)

        test_acc = evaluate_accuracy(test_data, net)
        print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" %
              (epoch, train_loss / len(train_data),
               train_acc / len(train_data), test_acc))
Exemplo n.º 4
0
from utils import SGD, accuracy, evaluate_accuracy
from mxnet import gluon

softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
learning_rate = .2

print('length of train data', len(train_data))

for epoch in range(2):
    train_loss = 0.
    train_acc = 0.
    i = 0

    for data, label in train_data:
        i = i + 1
        with autograd.record():
            output = net(data, verbose=False)
            loss = softmax_cross_entropy(output, label)

        loss.backward()
        SGD(params, learning_rate/batch_size)

        train_loss += nd.mean(loss).asscalar()
        train_acc += accuracy(output, label)

    test_acc = evaluate_accuracy(test_data, net)
    print("Epoch %d. Loss: %f, train_acc: %f, test acc %f."%(epoch, train_loss / len(train_data), train_acc / len(train_data), test_acc))
    print('batch numbers :', i)

#print('train_data', train_data.shape)
Exemplo n.º 5
0
    def train(self):

        #need to put into function
        
        if not os.path.exists("./" + param_dir):
            os.mkdir(param_dir)

        for test in range(self.num_test):
            fit_list = []
            iteration_list = []

            plt.ion()
            plt.show()

            #set up environment
            env = gym.make(self.env)


            if self.continuous_action == True:
                action_space = env.action_space.shape[0]
            else:
                action_space = env.action_space.n
            state_space = env.observation_space.low.size

            param_dict = {}
            #get param for the len
            best_reward = 0


            bbnp = bb_numpy(action_space,state_space,self.max_length,continuous_action = self.continuous_action,state_renormalize = self.state_renormalize)
            state = np.zeros(state_space)[None,...]
            bbnp.forward_propagate(state,init = True)

            #initialize param
            param = np.array(bbnp.get_flat_param())
            SGD_ = SGD(param, self.lr)
            
            cma_es = cma.CMAEvolutionStrategy(param,self.sigma,{'popsize': self.num_perturbations,})



            for iteration in range(self.iterations):
                
                ts = time.time()                
                cma_param = np.array(cma_es.ask())


                if self.method_type == 'CDPP':
                    if iteration == 0:

                        X = np.random.randn(self.num_perturbations*3,len(param))
                        cond_indices = cond_kdpp([], X, k = self.num_perturbations)
                        self.buffer = X[cond_indices]

                    else:
                        dists = np.linalg.norm(self.buffer-param,axis=1)
                        num_closest= int(self.perc_reuse*self.num_perturbations)

                        closest_indices = dists.argsort()[:num_closest]
                        reused_samples = self.buffer[closest_indices]
                        X = np.random.randn(self.num_perturbations*3,len(param)) + param
                        cond_indices = cond_kdpp(self.buffer[closest_indices],X,k=(self.num_perturbations-num_closest))

                        self.buffer = np.vstack((self.buffer[closest_indices],X[cond_indices]))

                        all_indices = []





                queue = Queue()
                num_workers = []
                num_perts = self.num_perturbations
                shared_amt = self.num_perturbations//self.num_cpu
                if self.noise_type == 'CDPP' and iteration!=0:
                    num_perts = self.num_perturbations - int(self.perc_reuse*self.num_perturbations)
                    shared_amt = (self.num_perturbations - int(self.perc_reuse*self.num_perturbations))//self.num_cpu
                while num_perts > shared_amt:
                        num_perts -= shared_amt
                        num_workers.append(shared_amt)
                num_workers.append(num_perts)
                start_indices = [0]
                # print('num_workers=',num_workers)
                for i in range(1,len(num_workers)):
                   start_indices.append(start_indices[i-1]+num_workers[i-1])

                cma_param_slicer = [0]
                cma_param_slicer.extend(num_workers)
                cma_param_slicer = np.cumsum(cma_param_slicer)
                
                seed_id =  np.random.randint(np.iinfo(np.int32(10)).max, size=len(num_workers))


                workers = [Process(target = self._do_work,args = (queue,bbnp,param,action_space,state_space,self.max_length,seed_id[i],num_workers[i],env,self.continuous_action,cma_param[cma_param_slicer[i]:cma_param_slicer[i+1],:],start_indices[i])) for i in range(len(seed_id))]


                for worker in workers:
                    worker.start()

                results = [queue.get() for p in workers]
                
                # Swapping this with the above line so deadlock is avoided
                for worker in workers:
                    worker.join()
                if self.noise_type == 'CDPP':
                    if iteration != 0:
                        old_pert_fitness = pert_fitness[closest_indices]
                        old_anti_pert_fitness = anti_pert_fitness[closest_indices]
                
                pert_fitness,anti_pert_fitness,seed_id = get_info_summary(results)
                pert_fitness = np.array(pert_fitness)[...,None]
                anti_pert_fitness = np.array(anti_pert_fitness)[...,None]

                if self.noise_type == 'CDPP':
                    if iteration != 0:
                        
                        pert_fitness = np.vstack((old_pert_fitness,pert_fitness))
                        anti_pert_fitness = np.vstack((old_anti_pert_fitness,anti_pert_fitness))


                if self.noise_type in ['Gaussian','Hadamard']:
                    noises = get_noise_matrices(seed_id,num_workers,len(param),self.sigma,self.noise_type)

                #record average_fit
                average_fit = np.sum(pert_fitness)/self.num_perturbations

                fit_list.append(average_fit)
                iteration_list.append(iteration)

                #dynamic plot the graph
                plt.plot(iteration_list,fit_list,'r')
                plt.draw()
                plt.pause(0.3)

                #Ranking best
                if self.method_type == 'Rank':

                    top_ind = np.sort(np.argsort(pert_fitness,axis =0)[-self.best:][::-1],axis = 0).flatten()
                    pert_fitness = pert_fitness[top_ind]
                    gradient = (1 / len(top_ind) / self.sigma * (noises[top_ind,:].T@pert_fitness)).flatten()
                    SGD_gradient = SGD_.get_gradients(gradient)
                    param = param + SGD_gradient
                    print('param',param)


                #Vanilla
                elif self.method_type == 'Vanilla':
                    gradient = (1 / self.num_perturbations / self.sigma * (noises.T@pert_fitness)).flatten()
                    SGD_gradient = SGD_.get_gradients(gradient)
                    param = param + SGD_gradient
                    
                #CMA
                elif self.method_type == 'CMA':
                    cma_es.tell(cma_param,-pert_fitness[:,0] - compute_weight_decay(0.01,cma_param))
                    param = cma_es.result[5] # mean of all perturbations - for render and save - not used to update new space

                #ARS
                elif self.method_type == 'ARS':
                    fb_fitness = np.hstack((pert_fitness,anti_pert_fitness))
                    top_ind = (np.argsort(np.max(fb_fitness,axis = 1,keepdims = True),axis = 0)[-self.best:][::-1]).flatten()
                    fit_diff = pert_fitness - anti_pert_fitness
                    reward_noise = np.std(np.vstack((pert_fitness,anti_pert_fitness)))
                    fit_diff = fit_diff[top_ind]
                    gradient = (1 / len(top_ind) / self.sigma/reward_noise * (noises[top_ind,:].T@fit_diff)).flatten()
                    SGD_gradient = SGD_.get_gradients(gradient)
                    param = param + SGD_gradient



                #CDPP
                elif self.method_type == 'CDPP':
                    cond_noise = self.buffer - param
                    if iteration != 0:
                        noises = np.vstack((reused_samples,cond_noise))
                    else:
                        noises = cond_noise


                    top_ind = np.sort(np.argsort(pert_fitness,axis =0)[-self.best:][::-1],axis = 0).flatten()
                    best_pert_fitness = pert_fitness[top_ind]
                    
                    gradient = (1 / self.num_perturbations / self.sigma * ((self.buffer - param)[top_ind,:].T@best_pert_fitness)).flatten()
                   
                    param = param + self.lr * gradient

                if iteration % self.video_save_interval == 0 and iteration !=0:
                    normal = Normalizer(state_space)
                    video_env = gym.wrappers.Monitor(env, './videos/' + str(self.env) + '/'+ str(self.method_type) + '_perturbations_' + str(self.num_perturbations) + '_' +'state_renormalize_' + str(self.state_renormalize)+ '_Simga_' + str(self.sigma) + "_best_" + str(self.best) + "_iter_" +str(iteration) + "_test_" + str(self.test))
                    bbnp.roll_out(param,video_env,self.env,normal,render = True)
                print("-" * 100)

                te = time.time()

                #print the results
                print('iteration: {} | average_fit: {} | # params: {} | time: {:2.2f}s'.format(iteration,average_fit,len(param),te-ts))


                self.save_param(path,name,param,average_fit,iteration)
Exemplo n.º 6
0
            patches_dist_labels_val, patches_color_labels_val
        ]
    else:
        y_paths = [patches_tr_lb_h]

        val_paths = [patches_val_lb_h]

    rows = args.patch_size
    cols = args.patch_size
    channels = 3

    # Define optimizer
    if args.optimizer == 'adam':
        optm = Adam(lr=args.learning_rate, beta_1=0.9)
    elif args.optimizer == 'sgd':
        optm = SGD(lr=args.learning_rate, momentum=0.8)

    # Define Loss
    print('=' * 60)
    if args.loss == 'cross_entropy':
        print('Using Cross Entropy')
        # loss = "categorical_crossentropy"
        loss = tf.keras.losses.CategoricalCrossentropy()
        loss_bound = tf.keras.losses.BinaryCrossentropy()
        loss_reg = tf.keras.losses.MeanSquaredError()
    elif args.loss == "tanimoto":
        print('Using Tanimoto Dual Loss')
        loss = Tanimoto_dual_loss()
        loss_bound = Tanimoto_dual_loss()
        loss_reg = Tanimoto_dual_loss()
    else:
Exemplo n.º 7
0
kernel_params = {'n_rand_feat': 10}
get_phi = construct_phi_for_kernel(kernel, kernel_params)

# - model
num_particles = 10
theta, forward = LeNet5(
    batch_size=batch_size,
    num_particles=num_particles,
)

@jit
def loss(theta, x, y):
    yhat = forward(theta, x)
    return cross_entropy(y, yhat)

optimizer = SGD(0.001)
grad_loss = jit(grad(loss))


# SVGD training
for epoch in range(100):

    for i, (x, y) in tqdm_(train_loader):
        g = get_phi(theta, grad_loss(theta, x.numpy(), y.numpy()))
        theta = optimizer.update(theta, g)

    test_acc = []
    for i, (x, y) in tqdm_(test_loader):
        yhat = forward(theta, x.numpy())
        nll = cross_entropy(y.numpy(), yhat)
        pred = yhat.mean(axis=0)
Exemplo n.º 8
0
H = 300
model = SimpleNet(D_in, H, D_out)
'''
(3) Training
'''


## Evaluation
def evaluation(model, X, Y):
    pred = model.forward(X)
    result = OH2label(pred) == Y
    result = list(result)
    return result.count(1), X.shape[0]


optim = SGD(model.get_params(), lr=0.0001, reg=0.00003)
criterion = CrossEntropyLoss()

batch_size = 64
EPOCH = 5
BATCH = int(X_train.shape[0] / batch_size)
for e in range(EPOCH):
    ## shuffle data
    index = [i for i in range(X_train.shape[0])]
    random.shuffle(index)
    X_train = X_train[index, :]
    Y_train = Y_train[index]

    for b in range(BATCH):
        ## get batch, covert to one-hot
        X_batch, Y_batch = get_batch(X_train, Y_train, batch_size, b)
    h2 = h2.flatten()

    h3_linear = nd.dot(h2, w3) + b3
    h3 = nd.relu(h3_linear)

    h4_linear = nd.dot(h3, w4) + b4

    return h4_linear


batch_size = 256
train_data, test_data = load_data_fashion_mnist(batch_size=batch_size)
softmax_xentropy = gluon.loss.SoftmaxCrossEntropyLoss()
leanring_rate = 0.2
epochs = 5

for epoch in range(epochs):
    train_loss = 0.
    train_acc = 0.
    for data, label in train_data:
        with autograd.record():
            output = net(data)
            loss = softmax_xentropy(output, label)
        loss.backward()
        SGD(params, leanring_rate / batch_size)
        train_loss += nd.mean(loss).asscalar()
        train_acc += accuracy(output, label)
    test_acc = evaluate_accuracy(test_data, net)
    print('epoch %s train accuracy %s test accuracy %s' %
          (epoch, train_acc / len(train_data), test_acc))
Exemplo n.º 10
0
    return nd.mean(output.argmax(axis=1) == label).asscalar()


def evaluate_accuracy(data_iterator, net_func):
    acc = 0.
    for data, label in data_iterator:
        output = net_func(data)
        acc += accuracy(output, label)
    return acc / len(data_iterator)


LearningRate = .1

for Epoch in range(5):
    TrainLoss = 0.
    TrainAcc = 0.
    for Data, Label in TrainData:
        with autograd.record():
            Output = net(Data)
            Loss = cross_entropy(Output, Label)
        Loss.backward()
        # 将梯度做平均,这样学习率会对batch size不那么敏感
        SGD(Params, LearningRate / BatchSize)

        TrainLoss += nd.mean(Loss).asscalar()
        TrainAcc += accuracy(Output, Label)

    TestAcc = evaluate_accuracy(TestData, net)
    print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" % (
        Epoch, TrainLoss / len(TrainData), TrainAcc / len(TrainData), TestAcc))
Exemplo n.º 11
0
#     net(data,verbose=True)
#     break

from mxnet import autograd as autograd
from utils import SGD, accuracy, evaluate_accuracy
from mxnet import gluon

epochs = 3
lr = 0.2
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()

for epoch in range(epochs):
    train_acc = 0.
    train_loss = 0.
    for data, label in train_data:
        label = label.as_in_context(ctx)
        with autograd.record():
            output = net(data)
            loss = softmax_cross_entropy(output, label)
        loss.backward()
        SGD(params, lr / batch_size)

        train_loss += nd.mean(loss).asscalar()
        train_acc += accuracy(output, label)

    test_acc = evaluate_accuracy(test_data, net, ctx)

    print('Epoch %d. train loss: %f, train acc: %f, test acc: %f' %
          (epoch, train_loss / len(train_data), train_acc / len(train_data),
           test_acc))
Exemplo n.º 12
0
    }

    val_fit = {
        "segmentation": patches_val_ref_aug_h,
        "boundary": patches_bound_labels_val,
        "distance": patches_dist_labels_val,
        "color": patches_color_labels_val
    }

#%%
start_time = time.time()
exp = 2
rows = patch_size
cols = patch_size
adam = Adam(lr=0.0001, beta_1=0.9)
sgd = SGD(lr=0.01, momentum=0.8)
batch_size = 8

#weights = [0.5, 0.5, 0]
weights = [weight0, weight1, 0]
print(f"Weights: {weights}")

print('=' * 60)

loss = weighted_categorical_crossentropy(weights)
#loss = 'categorical_crossentropy'

if args.resunet_a == True:

    if args.multitasking:
        print('Multitasking enabled!')