示例#1
0
文件: agent.py 项目: rhoowd/h-dqn
 def set_tensorboard(self):
     tb = TensorBoard()
     tb.add_graph('train_ep')
     tb.add_label('train_ep', 'reward')
     tb.add_graph('test_period')
     tb.add_label('test_period', 'reward')
     return tb
示例#2
0
    def __init__(self,
                 dataset,
                 n_tranformers,
                 n_scalers,
                 n_constructers,
                 n_selecters,
                 n_models,
                 lstm_size,
                 temperature,
                 tanh_constant,
                 save_dir,
                 func_names,
                 controller_max_step=100,
                 controller_grad_clip=0,
                 optimizer='sgd',
                 controller_lr=0.001,
                 entropy_weight=0.001,
                 ema_baseline_decay=0.95,
                 use_tensorboard=True,
                 model_dir=None,
                 log_step=10):

        self.dataset = dataset
        self.controller_max_step = controller_max_step
        self.controller_grad_clip = controller_grad_clip
        self.n_tranformers = n_tranformers
        self.n_scalers = n_scalers
        self.n_constructers = n_constructers
        self.n_selecters = n_selecters
        self.n_models = n_models
        self.lstm_size = lstm_size
        self.temperature = temperature
        self.tanh_constant = tanh_constant
        self.save_dir = save_dir
        self.optimizer = optimizer
        self.controller_lr = controller_lr
        self.entropy_weight = entropy_weight
        self.ema_baseline_decay = ema_baseline_decay
        self.func_names = func_names
        self.use_tensorboard = use_tensorboard
        self.log_step = log_step
        self.model_dir = model_dir

        if self.use_tensorboard:
            self.tb = TensorBoard(self.model_dir)
        else:
            self.tb = None

        self.controller_step = 0
示例#3
0
def initialize(algo, window_length=5):
    algo.stocks = STOCKS
    algo.sids = [algo.symbol(symbol) for symbol in algo.stocks]
    algo.m = len(algo.stocks)
    algo.price = {}
    algo.b_t = np.ones(algo.m) / algo.m
    algo.last_desired_port = np.ones(algo.m) / algo.m
    algo.init = True
    algo.days = 0
    algo.window_length = window_length
    algo.add_transform('mavg', 5)

    algo.set_commission(commission.PerShare(cost=0.005))

    if algo.tb_log_dir:
        algo.tensorboard = TensorBoard(log_dir=algo.tb_log_dir)
    else:
        algo.tensorboard = None
示例#4
0
    def __init__(self, args, dataset):
        self.args = args
        self.cuda = args.cuda
        self.dataset = dataset

        self.train_data = batchify(dataset.train, args.batch_size, self.cuda)
        self.valid_data = batchify(dataset.valid, args.batch_size, self.cuda)
        self.test_data = batchify(dataset.test, args.test_batch_size, self.cuda)
        
        self.max_length = self.args.shared_rnn_max_length

        if args.use_tensorboard:
            self.tb = TensorBoard(args.model_dir)
        else:
            self.tb = None
        self.build_model()

        if self.args.load_path:
            self.load_model()
示例#5
0
    def __init__(self, args, dataset):
        self.args = args
        self.cuda = args.cuda
        self.dataset = dataset
        if args.network_type in ['seq2seq'] and args.dataset in ['msrvtt']:
            self.train_data = dataset['train']
            self.valid_data = dataset['val']
            self.test_data = dataset['test']
        else:
            raise Exception(f"Unknown network type: {args.network_type} and unknown dataset: {args.dataset} combination !!")

        if args.use_tensorboard and args.mode == 'train':
            self.tb = TensorBoard(args.model_dir)
        else:
            self.tb = None
        self.build_model()

        if self.args.load_path:
            self.load_model()

        if self.args.loss_function in ['rl','xe+rl'] and self.args.reward_type=='CIDEnt':
            self.build_load_entailment_model()
示例#6
0
dataset = lastfm

# which type of session representation to use. False: Average pooling, True: Last hidden state
use_last_hidden_state = False

# use gpu
use_cuda = False

# dataset path
HOME = os.path.expanduser('~')
DATASET_PATH = HOME + '/datasets/' + dataset + '/4_train_test_split.pickle'

# logging
DATE_NOW = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d')
LOG_FILE = './testlog/' + str(DATE_NOW) + '-testing-plain-rnn.txt'
tensorboard = TensorBoard('./logs')

# set seed
seed = 0
torch.manual_seed(seed)

# RNN configuration
if dataset == reddit:
    INTRA_INTERNAL_SIZE = 50
    LEARNING_RATE = 0.001
    DROPOUT_RATE = 0.0
    MAX_EPOCHS = 31
elif dataset == lastfm:
    INTRA_INTERNAL_SIZE = 100
    LEARNING_RATE = 0.001
    DROPOUT_RATE = 0.2
示例#7
0
import numpy as np
import random

import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from tensorboard import TensorBoard
from model import FCN
from synth import Generator    # Used to add noise to images
G = Generator()

from bezier import *
from vggnet import *
Encoder = VGG(16, 36)      #Initializing a VGGnet architecture with 16 depth and 39 (9*4) as the num_outputs.
                           #Now we have to pass in the data 
writer = TensorBoard('log/')
import torch.optim as optim
criterion = nn.MSELoss()
criterion2 = nn.CrossEntropyLoss()

Decoder = FCN(64)   #Initializing the FCN network with width 64 (which is useless as the entire thing is hardcoded)
optimizerE = optim.Adam(Encoder.parameters(), lr=3e-4)
optimizerD = optim.Adam(Decoder.parameters(), lr=3e-4)
batch_size = 64
data_size = 100000
generated_size = 0
val_data_size = 512
first_generate = True

use_cuda = True
step = 0
示例#8
0
def main(args):

    # Step 1: init data folders
    '''if os.path.exists('save_state/'+args.regime+'/normalization_stats.pkl'):
        print('Loading normalization stats')
        x_mean, x_sd = misc.load_file('save_state/'+args.regime+'/normalization_stats.pkl')
    else:
        x_mean, x_sd = preprocess.save_normalization_stats(args.regime)
        print('x_mean: %.3f, x_sd: %.3f' % (x_mean, x_sd))'''

    val_loader=load_data(args, "val")

    tb=TensorBoard(args.model_dir)

    # Step 2: init neural networks
    print("network is:",args.net)
    if args.net == 'Reab3p16':
        model = Reab3p16(args)
    elif args.net=='RN_mlp':
        model =WildRelationNet()
    if args.gpunum > 1:
        model = nn.DataParallel(model, device_ids=range(args.gpunum))

    weights_path = args.path_weight+"/"+args.load_weight

    if os.path.exists(weights_path) and args.restore:
        pretrained_dict = torch.load(weights_path)
        model_dict = model.state_dict()
        pretrained_dict1 = {}
        for k, v in pretrained_dict.items():
            if k in model_dict:
                pretrained_dict1[k] = v
                #print(k)
        model_dict.update(pretrained_dict1)
        model.load_state_dict(model_dict)

        print('load weight')

    style_raven={65:0, 129:1, 257:2, 66:3, 132:4, 36:5, 258:6, 136:7, 264:8, 72:9, 130:10
         , 260:11, 40:12, 34:13, 49:14, 18:15, 20:16, 24:17}
    model.cuda()
    optimizer = optim.SGD(model.parameters(), lr=args.lr,momentum=args.mo, weight_decay=5e-4)
   
    if args.gpunum>1:
        optimizer = nn.DataParallel(optimizer, device_ids=range(args.gpunum))

    iter_count = 1
    epoch_count = 1
    #iter_epoch=int(len(train_files) / args.batch_size)
    print(time.strftime('%H:%M:%S', time.localtime(time.time())), 'training')
    style_raven_len = len(style_raven)
    
    if args.rl_style=="dqn":
        dqn = DQN()
    elif args.rl_style=="ddpg":
        ram = MemoryBuffer(1000)
        ddpg = Trainer(style_raven_len*4+2, style_raven_len, 1, ram)
    alpha_1=0.1

    if args.rl_style=="dqn":
        a = dqn.choose_action([0.5] * 3)  # TODO
    elif args.rl_style=="ddpg":
        action_ = ddpg.get_exploration_action(np.zeros([style_raven_len*4+2]).astype(np.float32),alpha_1)
    if args.type_loss:loss_fn=nn.BCELoss()
    best_acc=0.0
    while True:
        since=time.time()
        print(action_)
        for i in range(style_raven_len):
            tb.scalar_summary("action/a"+str(i), action_[i], epoch_count)

        data_files = preprocess.provide_data(args.regime, style_raven_len, action_,style_raven)

        train_files = [data_file for data_file in data_files if 'train' in data_file]
        print("train_num:", len(train_files))
        train_loader = torch.utils.data.DataLoader(Dataset(args,train_files), batch_size=args.batch_size, shuffle=True,
                                                   num_workers=args.numwork)
        model.train()
        iter_epoch = int(len(train_files) / args.batch_size)
        acc_part_train=np.zeros([style_raven_len,2]).astype(np.float32)

        mean_loss_train= np.zeros([style_raven_len, 2]).astype(np.float32)
        loss_train=0
        for x, y,style,me in train_loader:
            if x.shape[0]<10:
                print(x.shape[0])
                break
            x, y ,meta = Variable(x).cuda(), Variable(y).cuda(), Variable(me).cuda()
            if args.gpunum > 1:
                optimizer.module.zero_grad()
            else:
                optimizer.zero_grad()
            if args.type_loss:
                pred_train, pred_meta= model(x)
            else:
                pred_train = model(x)
            loss_ = F.nll_loss(pred_train, y,reduce=False)
            loss=loss_.mean() if not args.type_loss else loss_.mean()+10*loss_fn(pred_meta,meta)
            loss.backward()
            if args.gpunum > 1:
                optimizer.module.step()
            else:
                optimizer.step()
            iter_count += 1
            pred = pred_train.data.max(1)[1]
            correct = pred.eq(y.data).cpu()
            loss_train+=loss.item()
            for num, style_pers in enumerate(style):
                style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                for style_per in style_pers:
                    style_per=int(style_per)
                    if correct[num] == 1:
                        acc_part_train[style_per, 0] += 1
                    acc_part_train[style_per, 1] += 1
                    #mean_pred_train[style_per,0] += pred_train[num,y[num].item()].data.cpu()
                    #mean_pred_train[style_per, 1] += 1
                    mean_loss_train[style_per,0] += loss_[num].item()
                    mean_loss_train[style_per, 1] += 1
            accuracy_total = correct.sum() * 100.0 / len(y)

            if iter_count %10 == 0:
                iter_c = iter_count % iter_epoch
                print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                      ('train_epoch:%d,iter_count:%d/%d, loss:%.3f, acc:%.1f') % (
                      epoch_count, iter_c, iter_epoch, loss, accuracy_total))
                tb.scalar_summary("train_loss",loss,iter_count)
        loss_train=loss_train/len(train_files)
        #mean_pred_train=[x[0]/ x[1] for x in mean_pred_train]
        mean_loss_train=[x[0]/ x[1] for x in mean_loss_train]
        acc_part_train = [x[0] / x[1] if x[1]!=0 else 0  for x in acc_part_train]
        print(acc_part_train)
        if epoch_count %args.lr_step ==0:
            print("change lr")
            adjust_learning_rate(optimizer, epoch_count, args.lr_step,args.gpunum)
        time_elapsed = time.time() - since
        print('train epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        #acc_p=np.array([x[0]/x[1] for x in acc_part])
        #print(acc_p)
        with torch.no_grad():
            model.eval()
            accuracy_all = []
            iter_test=0
            acc_part_val = np.zeros([style_raven_len, 2]).astype(np.float32)
            for x, y, style,me in val_loader:
                iter_test+=1
                x, y = Variable(x).cuda(), Variable(y).cuda()
                pred,_ = model(x)
                pred = pred.data.max(1)[1]
                correct = pred.eq(y.data).cpu().numpy()
                accuracy = correct.sum() * 100.0 / len(y)
                for num, style_pers in enumerate(style):
                    style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                    for style_per in style_pers:
                        style_per = int(style_per)
                        if correct[num] == 1:
                            acc_part_val[style_per, 0] += 1
                        acc_part_val[style_per, 1] += 1
                accuracy_all.append(accuracy)

                # if iter_test % 10 == 0:
                #
                #     print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                #           ('test_iter:%d, acc:%.1f') % (
                #               iter_test, accuracy))

        accuracy_all = sum(accuracy_all) / len(accuracy_all)
        acc_part_val = [x[0] / x[1] if x[1]!=0 else 0 for x in acc_part_val ]
        baseline_rl=70
        reward=np.mean(acc_part_val)*100-baseline_rl
        tb.scalar_summary("valreward", reward,epoch_count)
        action_list=[x for x in a]
        cur_state=np.array(acc_part_val+acc_part_train+action_list+mean_loss_train
                           +[loss_train]+[epoch_count]).astype(np.float32)
        #np.expand_dims(, axis=0)
        if args.rl_style == "dqn":
            a = dqn.choose_action(cur_state)  # TODO
        elif args.rl_style == "ddpg":
            a = ddpg.get_exploration_action(cur_state,alpha_1)

        if alpha_1<1:
            alpha_1+=0.005#0.1
        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.store_transition(last_state, a, reward , cur_state)
            elif args.rl_style == "ddpg":ram.add(last_state, a, reward, cur_state)


        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.learn()
            elif args.rl_style == "ddpg":loss_actor, loss_critic=ddpg.optimize()
            print('------------------------------------')
            print('learn q learning')
            print('------------------------------------')
            tb.scalar_summary("loss_actor", loss_actor, epoch_count)
            tb.scalar_summary("loss_critic", loss_critic, epoch_count)


        last_state=cur_state
        time_elapsed = time.time() - since
        print('test epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        print('------------------------------------')
        print(('epoch:%d, acc:%.1f') % (epoch_count, accuracy_all))
        print('------------------------------------')
        if accuracy_all>best_acc:
            best_acc=max(best_acc,accuracy_all)
            #ddpg.save_models(args.model_dir + '/', epoch_count)
            save_state(model.state_dict(), args.model_dir + "/epochbest")
        epoch_count += 1
        if epoch_count%20==0:
            print("save weights")
            ddpg.save_models(args.model_dir+'/',epoch_count )
            save_state(model.state_dict(), args.model_dir+"/epoch"+str(epoch_count))
示例#9
0
    def __init__(self, args, dataset):
        """Constructor for training algorithm.

        Args:
            args: From command line, picked up by `argparse`.
            dataset: Currently only `data.text.Corpus` is supported.

        Initializes:
            - Data: train, val and test.
            - Model: shared and controller.
            - Inference: optimizers for shared and controller parameters.
            - Criticism: cross-entropy loss for training the shared model.
        """
        self.args = args
        if self.args.cuda:
            self.device = torch.device('cuda')
        else:
            self.device = torch.device('cpu')
        self.controller_step = 0
        self.cuda = args.cuda
        self.dataset = dataset
        self.epoch = 0
        self.shared_step = 0
        self.start_epoch = 0

        # logger.info('regularizing:')
        # for regularizer in [('activation regularization',
        #                      self.args.activation_regularization),
        #                     ('temporal activation regularization',
        #                      self.args.temporal_activation_regularization),
        #                     ('norm stabilizer regularization',
        #                      self.args.norm_stabilizer_regularization)]:
        #     if regularizer[1]:
        #         logger.info(f'{regularizer[0]}')

        self.train_data = utils.batchify(dataset.train, args.batch_size,
                                         self.cuda)
        # NOTE(brendan): The validation set data is batchified twice
        # separately: once for computing rewards during the Train Controller
        # phase (valid_data, batch size == 64), and once for evaluating ppl
        # over the entire validation set (eval_data, batch size == 1)
        self.valid_data = utils.batchify(dataset.valid, args.batch_size,
                                         self.cuda)
        self.eval_data = utils.batchify(dataset.valid, args.test_batch_size,
                                        self.cuda)
        self.test_data = utils.batchify(dataset.test, args.test_batch_size,
                                        self.cuda)

        self.max_length = self.args.shared_rnn_max_length

        if args.use_tensorboard:
            self.tb = TensorBoard(args.model_dir)
        else:
            self.tb = None
        self.build_model()

        if self.args.load_path:
            self.load_model()

        shared_optimizer = _get_optimizer(self.args.shared_optim)
        controller_optimizer = _get_optimizer(self.args.controller_optim)

        self.shared_optim = shared_optimizer(
            self.shared.parameters(),
            lr=self.shared_lr,
            weight_decay=self.args.shared_l2_reg)

        self.controller_optim = controller_optimizer(
            self.controller.parameters(), lr=self.args.controller_lr)

        self.ce = nn.CrossEntropyLoss()
示例#10
0
def main(args):

    # Step 1: init data folders
    '''if os.path.exists('save_state/'+args.regime+'/normalization_stats.pkl'):           ##to load raw data and preprocess it 
        print('Loading normalization stats')
        x_mean, x_sd = misc.load_file('save_state/'+args.regime+'/normalization_stats.pkl')
    else:
        x_mean, x_sd = preprocess.save_normalization_stats(args.regime)
        print('x_mean: %.3f, x_sd: %.3f' % (x_mean, x_sd))'''

    val_loader=load_data(args, "val")              ##loading already preprocessed validation/testing data 

    tb=TensorBoard(args.model_dir)                ##The model_dir arguments represents the directory to save model parameters, graph and etc. This can also be used to 
                                                  ##load checkpoints from the directory into a estimator to continue training a previously saved model.

    # Step 2: init neural networks
    print("network is:",args.net)
    if args.net == 'Reab3p16':                ##if want to use model Reab3p16
        model = Reab3p16(args)
    elif args.net=='RN_mlp':                  ##if want to use model WildRelationNet
        model =WildRelationNet()
    if args.gpunum > 1:                        
        model = nn.DataParallel(model, device_ids=range(args.gpunum)) ##The nn package defines a set of Modules, which you can think of as a neural network layer that has produces output from 
                                                                       ##input and may have some trainable weights.
                                                                    ##when more than one gpu, want to save model weights using DataParrallel module prefix
    weights_path = args.path_weight+"/"+args.load_weight               ##saved weigths of model 

    if os.path.exists(weights_path) and args.restore:             ##pretrained weights
        pretrained_dict = torch.load(weights_path)                 ##pretrained_dict is the state dictionary of the pre-trained model available
        model_dict = model.state_dict()                           ## https://pytorch.org/tutorials/recipes/recipes/what_is_state_dict.htmlA state_dict is an integral entity 
        pretrained_dict1 = {}                                      ##..if you are interested in saving or loading models from PyTorch
        for k, v in pretrained_dict.items():                      ##filter out unnecessary keys k
            if k in model_dict:                                   ##only when keys match(like conv2D..and so forth)
                pretrained_dict1[k] = v
                #print(k)                   
        model_dict.update(pretrained_dict1)                        ##overwrite entries in the existing state dict 
        model.load_state_dict(model_dict)                          ##load the new state dict, new weights

        print('load weight')

    style_raven={65:0, 129:1, 257:2, 66:3, 132:4, 36:5, 258:6, 136:7, 264:8, 72:9, 130:10    ##dictionary(key:value pair of      
         , 260:11, 40:12, 34:13, 49:14, 18:15, 20:16, 24:17}

##After setting weights using optimizer for training.

##The standard way in PyTorch to train a model in multiple GPUs is to use nn.DataParallel which copies the model to the GPUs 
##and during training splits the batch among them and combines the individual outputs.
##model.cuda() by default will send your model to the "current device"

#If you need to move a model to GPU via .cuda(), please do so before constructing optimizers for it. Parameters of a model 
#after .cuda() will be different objects with those before the call.

##A very popular technique that is used along with SGD is called Momentum. Instead of using only the gradient of the current 
##step to guide the search, momentum also accumulates the gradient of the past steps to determine the direction to go
    model.cuda()
    optimizer = optim.SGD(model.parameters(), lr=args.lr,momentum=args.mo, weight_decay=5e-4) ##Adam has convergence problems that often SGD + momentum can converge better 
                                                                               ##with longer training time. We often see a lot of papers in 2018 and 2019 were still using SGD
    if args.gpunum>1:
        optimizer = nn.DataParallel(optimizer, device_ids=range(args.gpunum))
                                  ##setting iter-count and epoch to 1 before starting training
    iter_count = 1               ## number of batches of data the algorithm has seen (or simply the number of passes the algorithm has done on the dataset)
    epoch_count = 1              ##number of times a learning algorithm sees the complete dataset 
    #iter_epoch=int(len(train_files) / args.batch_size)
    print(time.strftime('%H:%M:%S', time.localtime(time.time())), 'training')
    style_raven_len = len(style_raven)  ##length of  style raven dict
    
    if args.rl_style=="dqn":     ##calling reinforcemt model for training
        dqn = DQN()                ##if want to use dqn model
    elif args.rl_style=="ddpg":    ##if want to use ddpg model (aiming to use this)
        ram = MemoryBuffer(1000)   
        ddpg = Trainer(style_raven_len*4+2, style_raven_len, 1, ram)        ##creating an instance of Trainer class defined  in rl folder (ddpg.py) why style_raven_len*4+2? 
    alpha_1=0.1

    if args.rl_style=="dqn":
        a = dqn.choose_action([0.5] * 3)  # TODO
    elif args.rl_style=="ddpg":
        action_ = ddpg.get_exploration_action(np.zeros([style_raven_len*4+2]).astype(np.float32),alpha_1) ##calling exploration which returns action? 
    if args.type_loss:loss_fn=nn.BCELoss()                      ##Creates a criterion that measures the Binary Cross Entropy between the target and the output.
    best_acc=0.0                                                ##setting accuracy to 0.0
    while True:                                                ##loop(train)  until
        since=time.time()
        print(action_)                                            
        for i in range(style_raven_len):                
            tb.scalar_summary("action/a"+str(i), action_[i], epoch_count) ##saving summary such as poch counts and actions

        data_files = preprocess.provide_data(args.regime, style_raven_len, action_,style_raven) 

        train_files = [data_file for data_file in data_files if 'train' in data_file]               #creating a list of training files
        print("train_num:", len(train_files))
    
        ##torch.utils.data.DataLoader` supports both map-style and iterable-style datasets with single- or multi-process loading,
        ##customizing loading order and optional automatic batching (collation) and memory pinning
        ##shuffle true because we want independent B training batches from Dataset
        train_loader = torch.utils.data.DataLoader(Dataset(args,train_files), batch_size=args.batch_size, shuffle=True,  
                                                   num_workers=args.numwork)
        model.train()                      ##start training model
        iter_epoch = int(len(train_files) / args.batch_size)         ##setting iteration count for total dataset
        acc_part_train=np.zeros([style_raven_len,2]).astype(np.float32)       ##defining variable for saving part accuracy while training

        mean_loss_train= np.zeros([style_raven_len, 2]).astype(np.float32)     ##defining variable for saving mean loss while training
        loss_train=0
        for x, y,style,me in train_loader:                              
            if x.shape[0]<10:                             ##x.shape[0] will give the number of rows in an array  (10 by 1024 2D array)                 
                print(x.shape[0])
                break                                                            
            x, y ,meta = Variable(x).cuda(), Variable(y).cuda(), Variable(me).cuda()  ##Components are accessible as variable.x,  variable.y,  variable.z
            if args.gpunum > 1:                                                        
                optimizer.module.zero_grad()             ##to set the gradient of the parameters in the model to 0, module beacause DataParallel
            else:
                optimizer.zero_grad()                    ## same as above set the gradient of the parameters to zero
            if args.type_loss:
                pred_train, pred_meta= model(x)              ##applying model to x where x is from training data
            else:
                pred_train = model(x)                        ##x is images y is actual label/category
            loss_ = F.nll_loss(pred_train, y,reduce=False)     ##calculating loss occurred while training
            loss=loss_.mean() if not args.type_loss else loss_.mean()+10*loss_fn(pred_meta,meta)##If your loss is not a scalar value, then you should certainly use either 
            loss.backward()             ##loss.mean() or loss.sum() to convert it to a scalar before calling the backward. Otherwise, it will cause an error
        
        #When you call loss.backward(), all it does is compute gradient of loss w.r.t all the parameters in loss that have 
        ##requires_grad = True and store them in parameter.grad attribute for every parameter.
        ##optimizer.step() updates all the parameters based on parameter.grad
            if args.gpunum > 1:
                optimizer.module.step()      ##module for DataParallel
            else:
                optimizer.step()
            iter_count += 1                ##update iter-count by 1 evrytime
            pred = pred_train.data.max(1)[1]  
            correct = pred.eq(y.data).cpu()       ##compare actual and predicted category
            loss_train+=loss.item()               ##The average of the batch losses will give you an estimate of the “epoch loss” during training.
            for num, style_pers in enumerate(style):
                style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                for style_per in style_pers:
                    style_per=int(style_per)
                    if correct[num] == 1:
                        acc_part_train[style_per, 0] += 1
                    acc_part_train[style_per, 1] += 1
                    #mean_pred_train[style_per,0] += pred_train[num,y[num].item()].data.cpu()
                    #mean_pred_train[style_per, 1] += 1
                    mean_loss_train[style_per,0] += loss_[num].item()
                    mean_loss_train[style_per, 1] += 1
            accuracy_total = correct.sum() * 100.0 / len(y)       ####calc accuracy 

            if iter_count %10 == 0:                        ##do this for 10 iterations
                iter_c = iter_count % iter_epoch
                print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                      ('train_epoch:%d,iter_count:%d/%d, loss:%.3f, acc:%.1f') % (
                      epoch_count, iter_c, iter_epoch, loss, accuracy_total))
                tb.scalar_summary("train_loss",loss,iter_count)               ##saving train loss to summary
        loss_train=loss_train/len(train_files)                             ##The average of the batch losses will give you an estimate of the “epoch loss” during training.
        #mean_pred_train=[x[0]/ x[1] for x in mean_pred_train]
        mean_loss_train=[x[0]/ x[1] for x in mean_loss_train]
        acc_part_train = [x[0] / x[1] if x[1]!=0 else 0  for x in acc_part_train]
        print(acc_part_train)
        if epoch_count %args.lr_step ==0:                 ##adjusting learning rate after  30 epochs
            print("change lr")
            adjust_learning_rate(optimizer, epoch_count, args.lr_step,args.gpunum)
        time_elapsed = time.time() - since
        print('train epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        #acc_p=np.array([x[0]/x[1] for x in acc_part])
        #print(acc_p)
        with torch.no_grad():
            model.eval()             ##evaluating model 
            accuracy_all = []
            iter_test=0
            acc_part_val = np.zeros([style_raven_len, 2]).astype(np.float32)
            for x, y, style,me in val_loader:             ##using validation data
                iter_test+=1
                x, y = Variable(x).cuda(), Variable(y).cuda()
                pred,_ = model(x)
                pred = pred.data.max(1)[1]
                correct = pred.eq(y.data).cpu().numpy()
                accuracy = correct.sum() * 100.0 / len(y)   ##accuracy is calc basd on how many labels match
                for num, style_pers in enumerate(style):
                    style_pers = style_pers[:-4].split("/")[-1].split("_")[3:]
                    for style_per in style_pers:
                        style_per = int(style_per)
                        if correct[num] == 1:
                            acc_part_val[style_per, 0] += 1
                        acc_part_val[style_per, 1] += 1
                accuracy_all.append(accuracy)                    ##append to accuracy list

                # if iter_test % 10 == 0:
                #
                #     print(time.strftime('%H:%M:%S', time.localtime(time.time())),
                #           ('test_iter:%d, acc:%.1f') % (
                #               iter_test, accuracy))

        accuracy_all = sum(accuracy_all) / len(accuracy_all)              ##total accuracy is calculated 
        acc_part_val = [x[0] / x[1] if x[1]!=0 else 0 for x in acc_part_val ]
        baseline_rl=70                                          ##baseline for accuracy
        reward=np.mean(acc_part_val)*100-baseline_rl          ##calculating reward using val accuracy
        tb.scalar_summary("valreward", reward,epoch_count)        ##saving summary
        action_list=[x for x in a]
        cur_state=np.array(acc_part_val+acc_part_train+action_list+mean_loss_train ##saving all calc in currnt state
                           +[loss_train]+[epoch_count]).astype(np.float32)
        #np.expand_dims(, axis=0)
        if args.rl_style == "dqn":
            a = dqn.choose_action(cur_state)  # TODO
        elif args.rl_style == "ddpg":                              ##passing current state to rl model's get_exploration_action
            a = ddpg.get_exploration_action(cur_state,alpha_1)

        if alpha_1<1:
            alpha_1+=0.005#0.1
        if epoch_count > 1:                                ##saving  last state and current state ,reward in memory  for epoch >1
            if args.rl_style == "dqn":dqn.store_transition(last_state, a, reward , cur_state)
            elif args.rl_style == "ddpg":ram.add(last_state, a, reward, cur_state)    


        if epoch_count > 1:
            if args.rl_style == "dqn":dqn.learn()
            elif args.rl_style == "ddpg":loss_actor, loss_critic=ddpg.optimize()      ##using rl ddpg model's optimize function to for teaching
            print('------------------------------------')
            print('learn q learning')
            print('------------------------------------')
            tb.scalar_summary("loss_actor", loss_actor, epoch_count)
            tb.scalar_summary("loss_critic", loss_critic, epoch_count)


        last_state=cur_state
        time_elapsed = time.time() - since
        print('test epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        print('------------------------------------')
        print(('epoch:%d, acc:%.1f') % (epoch_count, accuracy_all))
        print('------------------------------------')
        if accuracy_all>best_acc:                                        ##save the best accuracy obtained from val data as best accuracy for next epoch
            best_acc=max(best_acc,accuracy_all)
            #ddpg.save_models(args.model_dir + '/', epoch_count)
            save_state(model.state_dict(), args.model_dir + "/epochbest")             ##saving the current state
        epoch_count += 1                                                         ##increasing  epoch count by 1
        if epoch_count%20==0:              ##Do this for 20 epochs for complete dataset 
            print("save weights")
            ddpg.save_models(args.model_dir+'/',epoch_count )                  ##saving the model
            save_state(model.state_dict(), args.model_dir+"/epoch"+str(epoch_count))
示例#11
0
model = Dense(numClasses, activation='softmax', name='outputs')(
    model)  #This is for the final layer of size number of classes

model = Model(
    base_model.input, model
)  #This will take the nase model input and concatenate the model we have created

#Freezing the initial 16 layers so that it doesn't get used during training
for i in model.layers[:16]:
    i.trainable = False

#it sets the hyperparmaters
model.compile(loss='categorical_crossentropy', optimizer='adam')
print("Model Created")

tfBoard = TensorBoard(log_dir="./logs")

X, y = load_data_full("./data", numClasses)
#Data augmentation to get more photos from existing photos
datagen = ImageDataGenerator(rotation_range=50,
                             horizontal_flip=True,
                             shear_range=0.2,
                             fill_mode='nearest')
datagen.fit(X)

print("Starting Training")
model.fit_generator(datagen.flow(X, y, batch_size=3),
                    steps_per_epoch=len(X) / 3,
                    epochs=20,
                    callbacks=[tfBoard])
print("Saving Model")
示例#12
0
def main(args):

    # Step 1: init data folders
    '''if os.path.exists('save_state/'+args.regime+'/normalization_stats.pkl'):
        print('Loading normalization stats')
        x_mean, x_sd = misc.load_file('save_state/'+args.regime+'/normalization_stats.pkl')
    else:
        x_mean, x_sd = preprocess.save_normalization_stats(args.regime)
        print('x_mean: %.3f, x_sd: %.3f' % (x_mean, x_sd))'''
    data_dir = args.datapath
    data_files = []
    for x in os.listdir(data_dir):
        for y in os.listdir(data_dir + x):
            data_files.append(data_dir + x + "/" + y)
    test_files = [
        data_file for data_file in data_files
        if 'val' in data_file and 'npz' in data_file
    ]

    train_files = [
        data_file for data_file in data_files
        if 'train' in data_file and 'npz' in data_file
    ]

    print("train_num:", len(train_files), "test_num:", len(test_files))

    train_loader = torch.utils.data.DataLoader(Dataset(args, train_files),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.numwork)  #
    test_loader = torch.utils.data.DataLoader(Dataset(args, test_files),
                                              batch_size=args.batch_size,
                                              num_workers=args.numwork)

    tb = TensorBoard(args.model_dir)

    # Step 2: init neural networks
    print("network is:", args.net)
    if args.net == 'Reab3p16':
        model = Reab3p16(args)

    if args.gpunum > 1:
        model = nn.DataParallel(model, device_ids=range(args.gpunum))

    weights_path = args.path_weight

    if os.path.exists(weights_path):
        pretrained_dict = torch.load(weights_path)
        model_dict = model.state_dict()
        pretrained_dict1 = {}
        for k, v in pretrained_dict.items():
            if k in model_dict:
                pretrained_dict1[k] = v
                #print(k)
        model_dict.update(pretrained_dict1)
        model.load_state_dict(model_dict)

        print('load weight: ' + weights_path)

    model.cuda()
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.mo,
                          weight_decay=5e-4)
    #optimizer = optim.Adam(model.parameters(), lr=args.lr)
    if args.gpunum > 1:
        optimizer = nn.DataParallel(optimizer, device_ids=range(args.gpunum))

    iter_count = 1
    epoch_count = 1
    #iter_epoch=int(len(train_files) / args.batch_size)
    print(time.strftime('%H:%M:%S', time.localtime(time.time())), 'training')

    while True:
        since = time.time()

        with torch.no_grad():
            model.eval()
            accuracy_all = []

            for x, y, style, me in test_loader:
                x, y = Variable(x).cuda(), Variable(y).cuda()
                pred = model(x)
                pred = pred.data.max(1)[1]
                correct = pred.eq(y.data).cpu().numpy()
                accuracy = correct.sum() * 100.0 / len(y)

                accuracy_all.append(accuracy)

        accuracy_all = sum(accuracy_all) / len(accuracy_all)

        reward = accuracy_all * 100
        tb.scalar_summary("test_acc", reward, epoch_count)

        # np.expand_dims(, axis=0)

        time_elapsed = time.time() - since
        print('test epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        print('------------------------------------')
        print(('epoch:%d, acc:%.1f') % (epoch_count, accuracy_all))
        print('------------------------------------')

        model.train()
        iter_epoch = int(len(train_files) / args.batch_size)
        for x, y, style, me in train_loader:
            if x.shape[0] < 10:
                print(x.shape[0])
                break
            x, y = Variable(x).cuda(), Variable(y).cuda()
            if args.gpunum > 1:
                optimizer.module.zero_grad()
            else:
                optimizer.zero_grad()
            pred = model(x)
            loss = F.nll_loss(pred, y, reduce=False)
            #train_loss=loss
            loss = loss.mean()
            loss.backward()
            if args.gpunum > 1:
                optimizer.module.step()
            else:
                optimizer.step()
            iter_count += 1
            pred = pred.data.max(1)[1]
            correct = pred.eq(y.data).cpu()
            accuracy_total = correct.sum() * 100.0 / len(y)
            if iter_count % 100 == 0:
                iter_c = iter_count % iter_epoch
                print(
                    time.strftime('%H:%M:%S', time.localtime(time.time())),
                    ('train_epoch:%d,iter_count:%d/%d, loss:%.3f, acc:%.1f') %
                    (epoch_count, iter_c, iter_epoch, loss, accuracy_total))

                tb.scalar_summary("train_loss", loss, iter_count)

        #print(acc_part_train)
        if epoch_count % args.lr_step == 0:
            print("change lr")
            adjust_learning_rate(optimizer, epoch_count, args.lr_step,
                                 args.gpunum)
        time_elapsed = time.time() - since
        print('train epoch in {:.0f}h {:.0f}m {:.0f}s'.format(
            time_elapsed // 3600, time_elapsed // 60 % 60, time_elapsed % 60))
        #acc_p=np.array([x[0]/x[1] for x in acc_part])
        #print(acc_p)

        epoch_count += 1
        if epoch_count % 1 == 0:
            print("save!!!!!!!!!!!!!!!!")
            save_state(model.state_dict(),
                       args.model_dir + "/epoch" + str(epoch_count))
示例#13
0
文件: main.py 项目: sksq96/nyu-nlp
parser.add_argument('--log_every', type=int, default=50)
parser.add_argument('--dev_every', type=int, default=1000)
parser.add_argument('--experiment', type=str, default='test')
params = parser.parse_args()


# gpu business
if torch.cuda.is_available():
    torch.cuda.set_device(params.gpu)
    device = torch.device('cuda:{}'.format(params.gpu))
else:
    device = torch.device('cpu')

# tensoboard logging
model_dir = f"runs/{params.experiment}/{time.asctime(time.localtime())}/"
tb = TensorBoard(model_dir)


# define text felids
# TODO: Other tokenizers?
inputs = data.Field(lower=True, tokenize='spacy')
answers = data.Field(sequential=False, unk_token=None)


train, valid = data.TabularDataset.splits(
    path="data",
    train=params.train_file, validation=params.val_file,
    format='tsv',
    skip_header=True,
    fields=[("sentence1", inputs), ("sentence2", inputs), ("label", answers)])
import random
import minerl
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from tensorboard import TensorBoard
from cdqn_model_res import DQN
from rpm import rpm
from craft import envstep_done

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
writer = TensorBoard('../train_log/large_train_-25-25')

#np.random.seed(1)
#random.seed(1)
#torch.manual_seed(1)
#torch.cuda.manual_seed(1)
#torch.cuda.manual_seed_all(1)
#torch.backends.cudnn.benchmark = False
#torch.backends.cudnn.deterministic = True

start_time = time.time()


def time_limit(time_out):
    global start_time
    end_time = time.time()
示例#15
0
    def __init__(self, args, dataset):
        """Constructor for training algorithm.

        Args:
            args: From command line, picked up by `argparse`.
            dataset: Currently only `data.text.Corpus` is supported.

        Initializes:
            - Data: train, val and test.
            - Model: shared and controller.
            - Inference: optimizers for shared and controller parameters.
            - Criticism: cross-entropy loss for training the shared model.
        """
        self.args = args
        self.controller_step = 0
        self.cuda = args.cuda
        self.device = gpu = torch.device("cuda:0")
        self.dataset = dataset
        self.epoch = 0
        self.shared_step = 0
        self.start_epoch = 0
        self.compute_fisher = False

        logger.info('regularizing:')
        for regularizer in [('activation regularization',
                             self.args.activation_regularization),
                            ('temporal activation regularization',
                             self.args.temporal_activation_regularization),
                            ('norm stabilizer regularization',
                             self.args.norm_stabilizer_regularization)]:
            if regularizer[1]:
                logger.info(f'{regularizer[0]}')

        self.image_dataset = isinstance(dataset, Image)
        if self.image_dataset:
            self._train_data = dataset.train
            self._valid_data = dataset.valid
            self._test_data = dataset.test
            self._eval_data = dataset.valid
            self.train_data = wrap_iterator_with_name(self._train_data,
                                                      'train')
            self.valid_data = wrap_iterator_with_name(self._valid_data,
                                                      'valid')
            self.test_data = wrap_iterator_with_name(self._test_data, 'test')
            self.eval_data = wrap_iterator_with_name(self._eval_data, 'eval')

            self.max_length = 0

        else:
            self.train_data = utils.batchify(dataset.train, args.batch_size,
                                             self.cuda)
            self.valid_data = utils.batchify(dataset.valid, args.batch_size,
                                             self.cuda)
            self.eval_data = utils.batchify(dataset.valid,
                                            args.test_batch_size, self.cuda)
            self.test_data = utils.batchify(dataset.test, args.test_batch_size,
                                            self.cuda)

            self.max_length = self.args.shared_rnn_max_length

        self.train_data_size = self.train_data.size(
            0) if not self.image_dataset else len(self.train_data)
        self.valid_data_size = self.valid_data.size(
            0) if not self.image_dataset else len(self.valid_data)
        self.test_data_size = self.test_data.size(
            0) if not self.image_dataset else len(self.test_data)

        # Visualization
        if args.use_tensorboard:
            self.tb = TensorBoard(args.model_dir)
        else:
            self.tb = None
        self.draw_network = utils.draw_network

        self.build_model()

        if self.args.load_path:
            self.load_model()

        shared_optimizer = _get_optimizer(self.args.shared_optim)
        controller_optimizer = _get_optimizer(self.args.controller_optim)

        # As fisher information, and it should be seen by this model, to get the loss.

        self.shared_optim = shared_optimizer(
            self.shared.parameters(),
            lr=self.shared_lr,
            weight_decay=self.args.shared_l2_reg)

        self.controller_optim = controller_optimizer(
            self.controller.parameters(), lr=self.args.controller_lr)

        self.ce = nn.CrossEntropyLoss()
        self.top_k_acc = top_k_accuracy
示例#16
0
                                             s=30,
                                             m=0.35)
    elif opt.metric == 'arc_margin':
        metric_fc = metrics.ArcMarginProduct(512,
                                             opt.num_classes,
                                             s=30,
                                             m=0.5,
                                             easy_margin=opt.easy_margin)
    elif opt.metric == 'sphere':
        metric_fc = metrics.SphereProduct(512, opt.num_classes, m=4)
    else:
        # metric_fc = nn.Linear(512, opt.num_classes)
        metric_fc = nn.Linear(512, opt.num_classes)

    ############ visual_model and model_to_device ##############
    tensor_board = TensorBoard(opt.train_batch_size, 3, 112, 112)
    #tensor_board.visual_model(model)
    model.to(device)
    model = DataParallel(model)
    metric_fc.to(device)
    metric_fc = DataParallel(metric_fc)

    ############ choose optimizer and optimizer ################
    if opt.optimizer == 'sgd':
        optimizer = torch.optim.SGD([{
            'params': model.parameters()
        }, {
            'params': metric_fc.parameters()
        }],
                                    lr=opt.lr,
                                    weight_decay=opt.weight_decay)
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from tensorboard import TensorBoard
from meta import Agent as metaagent
from treechop import Agent as Agent1
from craft import Agent as Agent2
from stone import Agent as Agent3
from rpm import rpm
from treechop import train as treechop_train

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
writer = TensorBoard('../train_log/metacontroller')

class invent(object):

    def __init__(self):
        self.log = 0
        self.plank = 0
        self.stick = 0
        self.crafttable = 0
        self.wdpkaxe = 0
        self.stone = 0
        self.stpkaxe = 0
        self.furnace = 0

        self.log1 = 0
        self.plank1 = 0