def __init__(self, n_in_channel, nclass, attention=False, activation="Relu", dropout=0, train_cnn=True, rnn_type='BGRU', n_RNN_cell=64, n_layers_RNN=1, dropout_recurrent=0, **kwargs): super(CRNN, self).__init__() self.attention = attention self.cnn = CNN(n_in_channel, activation, dropout, **kwargs) if not train_cnn: for param in self.cnn.parameters(): param.requires_grad = False self.train_cnn = train_cnn if rnn_type == 'BGRU': self.rnn = BidirectionalGRU(self.cnn.nb_filters[-1], n_RNN_cell, dropout=dropout_recurrent, num_layers=n_layers_RNN) else: NotImplementedError("Only BGRU supported for CRNN for now") self.dropout = nn.Dropout(dropout) self.dense = nn.Linear(n_RNN_cell * 2, nclass) self.sigmoid = nn.Sigmoid() if self.attention: self.dense_softmax = nn.Linear(n_RNN_cell * 2, nclass) self.softmax = nn.Softmax(dim=-1)
def get_model(state, args, init_model_name=None): if init_model_name is not None and os.path.exists(init_model_name): model, optimizer, state = load_model(init_model_name, return_optimizer=True, return_state=True) else: if "conv_dropout" in args: conv_dropout = args.conv_dropout else: conv_dropout = cfg.conv_dropout cnn_args = {1} if args.fixed_segment is not None: frames = cfg.frames else: frames = None nb_layers = 4 cnn_kwargs = { "activation": cfg.activation, "conv_dropout": conv_dropout, "batch_norm": cfg.batch_norm, "kernel_size": nb_layers * [3], "padding": nb_layers * [1], "stride": nb_layers * [1], "nb_filters": [16, 16, 32, 65], "pooling": [(2, 2), (2, 2), (1, 4), (1, 2)], "aggregation": args.agg_time, "norm_out": args.norm_embed, "frames": frames, } nb_frames_staying = cfg.frames // (2**2) model = CNN(*cnn_args, **cnn_kwargs) # model.apply(weights_init) state.update({ 'model': { "name": model.__class__.__name__, 'args': cnn_args, "kwargs": cnn_kwargs, 'state_dict': model.state_dict() }, 'nb_frames_staying': nb_frames_staying }) if init_model_name is not None: save_model(state, init_model_name) pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad) LOG.info( "number of parameters in the model: {}".format(pytorch_total_params)) return model, state
def __init__(self, w2v_cfg, n_in_channel, nclass, attention=False, activation="Relu", dropout=0, train_cnn=True, rnn_type='BGRU', n_RNN_cell=64, n_layers_RNN=1, dropout_recurrent=0, cnn_integration=False, **kwargs): super(WCRNN, self).__init__() self.w2v = w2v_encoder(w2v_cfg) #Wav2Vec2Config) #self.w2v = Wav2VecEncoder(Wav2Vec2SedConfig, None) self.pooling = nn.Sequential(nn.MaxPool2d((1, 4), (1, 4))) self.n_in_channel = n_in_channel self.attention = attention self.cnn_integration = cnn_integration n_in_cnn = n_in_channel if cnn_integration: n_in_cnn = 1 self.cnn = CNN(n_in_cnn, activation, dropout, **kwargs) if not train_cnn: for param in self.cnn.parameters(): param.requires_grad = False self.train_cnn = train_cnn if rnn_type == 'BGRU': nb_in = self.cnn.nb_filters[-1] if self.cnn_integration: # self.fc = nn.Linear(nb_in * n_in_channel, nb_in) nb_in = nb_in * n_in_channel self.rnn = BidirectionalGRU(nb_in, n_RNN_cell, dropout=dropout_recurrent, num_layers=n_layers_RNN) else: NotImplementedError("Only BGRU supported for CRNN for now") self.dropout = nn.Dropout(dropout) self.dense = nn.Linear(n_RNN_cell * 2, nclass) self.sigmoid = nn.Sigmoid() if self.attention: self.dense_softmax = nn.Linear(n_RNN_cell * 2, nclass) self.softmax = nn.Softmax(dim=-1)
def build_model(model_name, **kwargs): if model_name == 'CNN': return CNN(**kwargs) elif model_name == 'DAE_CNN': return DAE_CNN(**kwargs) else: raise Exception('Model unknown %s' % model_name)
def main(_): #train('data/', 'data.csv','trained_models/') X,Y,_ = get_data_info('data/', 'data.csv') # read input tensor X = get_tensor(X).astype(np.float32) models = {} models['pants'] = CNN('pants') texture = np.array([ [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0],[1,0,0], [1,0,0], [1,0,0], [1,0,0]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]], [[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1],[0,0,1], [0,0,1], [0,0,1], [0,0,1]] ],dtype=np.float32) texture_swap(texture, labels()[0], models, X[0][:,:,:3])
def train(datafolder, datafile, modelfolder): ''' @param datafolder {string} the path to the folder with dataset labels i.e. db/data/ @param datafile {string} the name of the file with dataset labels, no path @param modelfolder {string} the path to folder where models should be stored @return dict{} dictionary of models for each label in mlfashion.labels() ''' models = {} X,Y,_ = get_data_info(datafolder, datafile) # read input tensor X = get_tensor(X).astype(np.float32) for label in labels(): models[label] = CNN(label) # create separate thread or process # get output tensor print('Training for ' + label) T = get_output_tensor(Y, label).astype(np.float32) train_segmentation(models[label], X, T, modelfolder) models[label].save(modelfolder + 'safesave/' + label + '.ckpt') models[label].save(modelfolder + label + '.ckpt') del T # end of part in parallel return models
def __init__(self, env, args, device='cpu'): """ Instantiate an NEC Agent ---------- env: gym.Env gym environment to train on args: args class from argparser args are from from train.py: see train.py for help with each arg device: string 'cpu' or 'cuda:0' depending on use_cuda flag from train.py """ self.environment_type = args.environment_type self.env = env self.device = device # Hyperparameters self.epsilon = args.initial_epsilon self.final_epsilon = args.final_epsilon self.epsilon_decay = args.epsilon_decay self.gamma = args.gamma self.N = args.N # Transition queue and replay memory self.transition_queue = [] self.replay_every = args.replay_every self.replay_buffer_size = args.replay_buffer_size self.replay_memory = ReplayMemory(self.replay_buffer_size) # CNN for state embedding network self.frames_to_stack = args.frames_to_stack self.embedding_size = args.embedding_size self.in_height = args.in_height self.in_width = args.in_width self.cnn = CNN(self.frames_to_stack, self.embedding_size, self.in_height, self.in_width).to(self.device) # Differentiable Neural Dictionary (DND): one for each action self.kernel = inverse_distance self.num_neighbors = args.num_neighbors self.max_memory = args.max_memory self.lr = args.lr self.dnd_list = [] for i in range(env.action_space.n): self.dnd_list.append( DND(self.kernel, self.num_neighbors, self.max_memory, args.optimizer, self.lr)) # Optimizer for state embedding CNN self.q_lr = args.q_lr self.batch_size = args.batch_size self.optimizer = get_optimizer(args.optimizer, self.cnn.parameters(), self.lr)
def init_model(self): ''' pooling, rnn, lstm, bilstm, cnn, multi_cnn, gru :return: ''' if self.opts.model == 'pooling': self.model = Pooling(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'cnn': self.model = CNN(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'multi_channel_cnn': self.model = Multi_Channel_CNN(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'multi_layer_cnn': self.model = Multi_Layer_CNN(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'char_cnn': self.char = True self.model = Char_CNN(opts=self.opts, vocab=self.vocab, char_vocab=self.char_vocab, label_vocab=self.label_vocab) elif self.opts.model == 'lstm': self.model = LSTM(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'gru': self.model = GRU(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'lstm_cnn': self.model = LSTM_CNN(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'treelstm': self.tree = True self.model = BatchChildSumTreeLSTM(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'cnn_treelstm': self.tree = True self.model = CNN_TreeLSTM(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) elif self.opts.model == 'lstm_treelstm': self.tree = True self.model = LSTM_TreeLSTM(opts=self.opts, vocab=self.vocab, label_vocab=self.label_vocab) else: raise RuntimeError('please choose your model first!') if self.opts.use_cuda: self.model = self.model.cuda()
def load_models(modelsfolder): ''' @param modelfolder the path to the folder where models are saved @return dict{} dictionary of the models for each label in mlfashion.labels() ''' models = {} for label in labels(): models[label] = CNN.load_model(modelsfolder + label + '0.ckpt', label) return models
def setup(opt): if opt.model == "cnn": model = CNN(opt) elif opt.model == "bilstm": model = BiLSTM(opt) elif opt.model == "transformer": model = Transformer(opt) elif opt.model == "gah": model = GAH(opt) elif opt.model == 'gahs': model = GAHs(opt) else: raise Exception("model not supported: {}".format(opt.model)) return model
def setup(opt): if opt.contatenate == 1: opt.max_sequence_length = opt.max_sequence_length_contatenate if opt.model == "lstm_2L": model = LSTM2L(opt) elif opt.model == "cnn": model = CNN(opt) elif opt.model == "bilstm": model = BiLSTM(opt) elif opt.model == "bilstm_2L": model = BiLSTM2L(opt) else: raise Exception("model not supported: {}".format(opt.model)) return model
def get_model(dataloader): if hp.model_name == "CNN": from models.CNN import Trainer, CNN model = CNN() elif hp.model_name == "RNN": from models.RNN import Trainer, RNN model = RNN(hp.n_features, 64, 3, hp.num_classes, hp.device, classes=None) elif hp.model_name == "AudioEncoder": from models.AudioEncoder import Trainer, AudioEncoder model = AudioEncoder() elif hp.model_name == "GAN": from models.GAN import Gan, Trainer model = Gan() elif hp.model_name == "Test": from models.TestModul import TestModule, Trainer model = TestModule() if os.path.isfile(hp.model_path): model.load_state_dict(torch.load(hp.model_path)) print("model loaded from: {}".format(hp.model_path)) trainer = Trainer(dataloader, model) return model, trainer
def compute_results(subnet_type, weight_sharing, aux_loss, rounds=1): print("=" * 100) print('\nWeight sharing:', weight_sharing, ' Aux loss:', aux_loss, ' Subnet:', subnet_type) if subnet_type == 'FCN': alpha = config.BEST_ALPHA_FCN nb_hidden_layers = config.SIAMESE_NET_BEST_NB_FCN hidden_layer = config.SIAMESE_NET_BEST_HIDDEN_FCN print('\nSubnet parameters:', ' HL: ', config.FCN_BEST_HIDDEN, ' KL: ', config.FCN_BEST_NB) print('\nSiamese parameters:', ' HL: ', hidden_layer, ' KL: ', nb_hidden_layers, ' alpha: ', alpha) subnet1 = FCN(nb_hidden_layers=config.FCN_BEST_NB, hidden_layer=config.FCN_BEST_HIDDEN) subnet2 = FCN(nb_hidden_layers=config.FCN_BEST_NB, hidden_layer=config.FCN_BEST_HIDDEN) else: alpha = config.BEST_ALPHA_CNN nb_hidden_layers = config.SIAMESE_NET_BEST_NB_CNN hidden_layer = config.SIAMESE_NET_BEST_HIDDEN_CNN print('\nSubnet parameters:', ' BC: ', config.CNN_BEST_CHANNEL, ' KC: ', config.CNN_BEST_KERNEL_SIZE, ' HL: ', config.CNN_BEST_HIDDEN, ' KL: ', config.CNN_BEST_NB) print('\nSiamese parameters:', ' HL: ', hidden_layer, ' KL: ', nb_hidden_layers, ' alpha: ', alpha) subnet1 = CNN(nb_hidden_layers=config.CNN_BEST_NB, hidden_layer=config.CNN_BEST_HIDDEN, base_channel_size=config.CNN_BEST_CHANNEL, kernel_size=config.CNN_BEST_KERNEL_SIZE) subnet2 = CNN(nb_hidden_layers=config.CNN_BEST_NB, hidden_layer=config.CNN_BEST_HIDDEN, base_channel_size=config.CNN_BEST_CHANNEL, kernel_size=config.CNN_BEST_KERNEL_SIZE) if weight_sharing: model = SiameseNet(subnet1, nb_hidden_layers=nb_hidden_layers, hidden_layer=hidden_layer) else: model = SiameseNet(subnet1, subnet2, nb_hidden_layers=nb_hidden_layers, hidden_layer=hidden_layer) loss_train = [] acc_train = [] loss_test = [] acc_test = [] for i in range(rounds): print('\nTrain beginning...') training_losses, training_acc, _, _, test_losses, test_acc, _, _ = train_siamese( model=model, dataloader=train_dataloader, test_dataloader=test_dataloader, aux_loss=aux_loss, alpha=alpha) print('\nTrain complete !') final_train_loss, final_train_acc = training_losses[-1], training_acc[ -1] loss_train.append(final_train_loss) acc_train.append(final_train_acc) final_test_loss, final_test_acc = test_losses[-1], test_acc[-1] loss_test.append(final_test_loss) acc_test.append(final_test_acc) mean_train_loss = sum(loss_train) / len(loss_train) mean_train_acc = sum(acc_train) / len(acc_train) print( "In epoch {0}, on the train set we obtain a loss of {1} and an accuracy of {2}" .format(config.EPOCHS, round(mean_train_loss, 3), round(mean_train_acc, 3))) mean_test_loss = sum(loss_test) / len(loss_test) mean_test_acc = sum(acc_test) / len(acc_test) print( "In epoch {0}, on the test set we obtain a loss of {1} and an accuracy of {2}" .format(config.EPOCHS, round(mean_test_loss, 3), round(mean_test_acc, 3)))
def createNeuralNetworkModel(self, neuralNetworkModel): if neuralNetworkModel == "cnn": return CNN() elif neuralNetworkModel == "rnn": return RNN()
def __init__(self, pretrained=False): super(DAGCN_features, self).__init__() self.model_cnn = CNN(pretrained) self.model_GCN = MRF_GCN(pretrained) self.__in_features = 256*1
def trainClassify(): args = parser_args() device_id = args.gpu X_train, y_train, X_test, y_test = get_data(train_data, test_data, train_name, test_name) model = CNN(n_class=num_cate) if device_id >= 0: xp = cuda.cupy model.to_gpu(device_id) else: xp = np optimizer = optimizers.MomentumSGD(lr=0.005, momentum=0.9) # optimizer = optimizers.Adam() optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(args.weight_decay)) N_train = len(X_train) N_test = len(X_test) n_epoch = 50 batchsize = 30 with open(log_txt, 'w'): pass with open(log_dat, 'w'): pass for epoch in range(n_epoch): print('Epoch %d :' % (epoch + 1), end=" ") data = open(log_dat, "a") data.write('%d ' % (epoch + 1)) data.close() # Training sum_loss = 0 pred_y = [] perm = np.random.permutation(N_train) for i in range(0, N_train, batchsize): # x = Variable(xp.array(augument(X_train[perm[i: i+batchsize]])).astype(xp.float32)) x = Variable( xp.array(X_train[perm[i:i + batchsize]]).astype(xp.float32)) t = Variable( xp.array(y_train[perm[i:i + batchsize]]).astype(xp.int32)) optimizer.update(model, x, t) if device_id >= 0: sum_loss += cuda.to_cpu(model.loss.data) * len(x.data) pred_y.extend((np.sign(cuda.to_cpu(model.y.data)) + 1) / 2) else: sum_loss += np.array(model.loss.data) * len(x.data) pred_y.extend((np.sign(np.array(model.y.data)) + 1) / 2) loss = sum_loss / N_train accuracy = xp.sum( xp.array(pred_y, dtype=xp.int32) == xp.array( y_train[perm], dtype=xp.int32)) / N_train / num_cate print('\nTrain loss %.3f, accuracy %.4f |' % (loss, accuracy), end=" ") log = open(log_txt, "a") log.write('Train loss %.3f, accuracy %.4f |' % (loss, accuracy)) log.close() data = open(log_dat, "a") data.write('%.3f %.4f ' % (loss, accuracy)) data.close() for i in range(num_cate): accu = xp.sum( xp.array(pred_y, dtype=xp.int32)[:, i] == xp.array( y_train[perm][:, i], dtype=xp.int32)) / N_train print('Category %d: %.4f |' % (i, accu), end=" ") log = open(log_txt, "a") log.write('Category %d: %.4f |' % (i, accu)) log.close() data = open(log_dat, "a") data.write('%.4f ' % accu) data.close() # Testing sum_loss = 0 pred_y = [] for i in range(0, N_test, batchsize): # x = Variable(xp.array(augument(X_test[i: i+batchsize])).astype(xp.float32)) x = Variable(xp.array(X_test[i:i + batchsize]).astype(xp.float32)) t = Variable(xp.array(y_test[i:i + batchsize]).astype(xp.int32)) if device_id >= 0: with chainer.using_config('train', False): sum_loss += cuda.to_cpu(model(x, t).data) * len(x.data) pred_y.extend((np.sign(cuda.to_cpu(model.y.data)) + 1) / 2) else: with chainer.using_config('train', False): sum_loss += np.array(model(x, t).data) * len(x.data) pred_y.extend((np.sign(np.array(model.y.data)) + 1) / 2) loss = sum_loss / N_test accuracy = xp.sum( xp.array(pred_y, dtype=xp.int32) == xp.array( y_test, dtype=xp.int32)) / N_test / num_cate print('\n Test loss %.3f, accuracy %.4f |' % (loss, accuracy), end=" ") log = open(log_txt, "a") log.write('\n Test loss %.3f, accuracy %.4f |' % (loss, accuracy)) log.close() data = open(log_dat, "a") data.write('%.3f %.4f ' % (loss, accuracy)) data.close() for i in range(num_cate): accu = xp.sum( xp.array(pred_y, dtype=xp.float32)[:, i] == xp.array( y_test[:, i], dtype=xp.float32)) / N_test print('Category %d: %.4f |' % (i, accu), end=" ") log = open(log_txt, "a") log.write('Category %d: %.4f |' % (i, accu)) log.close() data = open(log_dat, "a") data.write('%.4f ' % accu) data.close() print() log = open(log_txt, "a") log.write('\n') log.close() data = open(log_dat, "a") data.write('\n') data.close() if (epoch + 1) % 5 == 0: serializers.save_hdf5( SavedModelFolder + '/FaceCl_{0:03d}.model'.format(epoch + 1), model)
try: os.makedirs(params.plot_dir) except FileExistsError: pass logger = logging.getLogger('CNN.cv{}'.format(i)) set_logger(os.path.join(model_dir, 'train.cv{}.log'.format(i)), logger) params.restore = False # use GPU if available cuda_exist = torch.cuda.is_available() # Set random seeds for reproducible experiments if necessary if cuda_exist: params.device = torch.device('cuda') # torch.cuda.manual_seed(240) logger.info('Using Cuda...') model = CNN(params, logger).cuda() else: params.device = torch.device('cpu') # torch.manual_seed(230) logger.info('Not using cuda...') model = CNN(params, logger) logger.info('Loading the datasets for batch-training') logger.info('Loading complete.') logger.info(f'Model: \n{str(model)}') params.num_epochs = 100 model.xfit(train_loader, val_loader, restore_file=os.path.join( params.model_dir, 'best.pth.tar'))
class CRNN(nn.Module): def __init__(self, n_in_channel, nclass, attention=False, activation="Relu", dropout=0, train_cnn=True, rnn_type='BGRU', n_RNN_cell=64, n_layers_RNN=1, dropout_recurrent=0, **kwargs): super(CRNN, self).__init__() self.attention = attention self.cnn = CNN(n_in_channel, activation, dropout, **kwargs) if not train_cnn: for param in self.cnn.parameters(): param.requires_grad = False self.train_cnn = train_cnn if rnn_type == 'BGRU': self.rnn = BidirectionalGRU(self.cnn.nb_filters[-1], n_RNN_cell, dropout=dropout_recurrent, num_layers=n_layers_RNN) else: NotImplementedError("Only BGRU supported for CRNN for now") self.dropout = nn.Dropout(dropout) self.dense = nn.Linear(n_RNN_cell * 2, nclass) self.sigmoid = nn.Sigmoid() if self.attention: self.dense_softmax = nn.Linear(n_RNN_cell * 2, nclass) self.softmax = nn.Softmax(dim=-1) def load_cnn(self, parameters): self.cnn.load(parameters) if not self.train_cnn: for param in self.cnn.parameters(): param.requires_grad = False def load(self, filename=None, parameters=None): if filename is not None: parameters = torch.load(filename) if parameters is None: raise NotImplementedError( "load is a filename or a list of parameters (state_dict)") self.cnn.load(parameters=parameters["cnn"]) self.rnn.load_state_dict(parameters["rnn"]) self.dense.load_state_dict(parameters["dense"]) def state_dict(self, destination=None, prefix='', keep_vars=False): state_dict = { "cnn": self.cnn.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars), "rnn": self.rnn.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars), 'dense': self.dense.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars) } return state_dict def save(self, filename): parameters = { 'cnn': self.cnn.state_dict(), 'rnn': self.rnn.state_dict(), 'dense': self.dense.state_dict() } torch.save(parameters, filename) def forward(self, x): # input size : (batch_size, n_channels, n_frames, n_freq) # conv features x = self.cnn(x) bs, chan, frames, freq = x.size() if freq != 1: warnings.warn("Output shape is: {}".format( (bs, frames, chan * freq))) x = x.permute(0, 2, 1, 3) x = x.contiguous().view(bs, frames, chan * freq) else: x = x.squeeze(-1) x = x.permute(0, 2, 1) # [bs, frames, chan] # rnn features x = self.rnn(x) x = self.dropout(x) strong = self.dense(x) # [bs, frames, nclass] strong = self.sigmoid(strong) if self.attention: sof = self.dense_softmax(x) # [bs, frames, nclass] sof = self.softmax(sof) sof = torch.clamp(sof, min=1e-7, max=1) weak = (strong * sof).sum(1) / sof.sum(1) # [bs, nclass] else: weak = strong.mean(1) return strong, weak
log_file = open(save_path + model_name + "_test_logs.txt", 'a+') # prepare test loader for the test set test_file = args.data_path + args.test_file test_data = ArticlesDataset(csv_file=test_file, vocab=vocab, label2id=label2id, max_text_len=args.text_len) test_loader = DataLoader(test_data, batch_size=args.batch_size, shuffle=False) scores_dict = {'f1': [], 'recall': [], 'precision': [], 'confidence': []} for run_num in range(args.num_runs): model_run_name = model_name + "_run"+str(run_num+1) print("-"*10, "Run", run_num+1, "-"*10) print("Model name:", model_run_name) print("Loading model from", save_path + model_run_name + ".pt") best_model = CNN(cnn_args=cnn_args, mlp_args=mlp_args).to(device) optimizer = torch.optim.Adam(best_model.parameters(), lr=0.005) load_checkpoint(save_path + model_run_name + ".pt", best_model, optimizer, device, log_file) results = evaluate(best_model, test_loader) scores_dict['f1'].append(results['f1']) scores_dict['recall'].append(results['recall']) scores_dict['precision'].append(results['precision']) # if args.save_confidence is True: # scores_dict['confidence'].append(results['confidence']) # scores_dict['labels'].append(results['labels']) # scores_dict['content'].append(results['content']) # sentence_encodings = results['sentence_encodings']
def run_episode(strategies, policy, beta, device, num_worker): states, actions = [], [] # all strategies use same initial training data and model weights reinit_seed(prop.RANDOM_SEED) if prop.MODEL == "MLP": model = MLP().apply(weights_init).to(device) if prop.MODEL == "CNN": model = CNN().apply(weights_init).to(device) if prop.MODEL == "RESNET18": model = models.resnet.ResNet18().to(device) init_weights = deepcopy(model.state_dict()) # re-init seed was here before use_learner = True if np.random.rand(1) > beta else False if use_learner: policy = policy.to( device) # load policy only when learner is used for states dataset_pool, valid_dataset, test_dataset = get_policy_training_splits() train_dataset, pool_dataset = stratified_split_dataset( dataset_pool, prop.INIT_SIZE, prop.NUM_CLASSES) # Initial sampling if prop.SINGLE_HEAD: my_strategies = [] for StrategyClass in strategies: my_strategies.append( StrategyClass(dataset_pool, valid_dataset, test_dataset)) if prop.CLUSTER_EXPERT_HEAD: UncertaintyStrategieClasses, DiversityStrategieClasses = strategies un_strategies = [] di_strategies = [] for StrategyClass in UncertaintyStrategieClasses: un_strategies.append( StrategyClass(dataset_pool, valid_dataset, test_dataset)) for StrategyClass in DiversityStrategieClasses: di_strategies.append( StrategyClass(dataset_pool, valid_dataset, test_dataset)) if prop.CLUSTERING_AUX_LOSS_HEAD: my_strategies = [] for StrategyClass in strategies: my_strategies.append( StrategyClass(dataset_pool, valid_dataset, test_dataset)) init_acc = train_validate_model(model, device, train_dataset, valid_dataset, test_dataset) t = trange(1, prop.NUM_ACQS + 1, desc="Aquisitions (size {})".format(prop.ACQ_SIZE), leave=True) for acq_num in t: subset_ind = np.random.choice(a=len(pool_dataset), size=prop.K, replace=False) pool_subset = make_tensordataset(pool_dataset, subset_ind) if prop.CLUSTER_EXPERT_HEAD: un_sel_ind = expert(acq_num, model, init_weights, un_strategies, train_dataset, pool_subset, valid_dataset, test_dataset, device) di_sel_ind = expert(acq_num, model, init_weights, un_strategies, train_dataset, pool_subset, valid_dataset, test_dataset, device) state, action = get_state_action(model, train_dataset, pool_subset, un_sel_ind=un_sel_ind, di_sel_ind=di_sel_ind) if prop.SINGLE_HEAD: sel_ind = expert(acq_num, model, init_weights, my_strategies, train_dataset, pool_subset, valid_dataset, test_dataset, device) state, action = get_state_action(model, train_dataset, pool_subset, sel_ind=sel_ind) if prop.CLUSTERING_AUX_LOSS_HEAD: sel_ind = expert(acq_num, model, init_weights, my_strategies, train_dataset, pool_subset, valid_dataset, test_dataset, device) state, action = get_state_action(model, train_dataset, pool_subset, sel_ind=sel_ind, clustering=None) # not implemented states.append(state) actions.append(action) if use_learner: with torch.no_grad(): if prop.SINGLE_HEAD: policy_outputs = policy(state.to(device)).flatten() sel_ind = torch.topk(policy_outputs, prop.ACQ_SIZE)[1].cpu().numpy() if prop.CLUSTER_EXPERT_HEAD: policy_output_uncertainty, policy_output_diversity = policy( state.to(device)) # clustering_space = policy_output_diversity.reshape(prop.K, prop.POLICY_OUTPUT_SIZE) # one topk for uncertainty, one topk for diversity diversity_selection = torch.topk( policy_output_diversity.reshape(prop.K), int(prop.ACQ_SIZE / 2.0))[1].cpu().numpy() uncertainty_selection = torch.topk( policy_output_uncertainty.reshape(prop.K), int(prop.ACQ_SIZE / 2.0))[1].cpu().numpy() sel_ind = (uncertainty_selection, diversity_selection) if prop.CLUSTERING_AUX_LOSS_HEAD: # not implemented policy_outputs = policy(state.to(device)).flatten() sel_ind = torch.topk(policy_outputs, prop.ACQ_SIZE)[1].cpu().numpy() if prop.SINGLE_HEAD: q_idxs = subset_ind[sel_ind] # from subset to full pool if prop.CLUSTER_EXPERT_HEAD: unified_sel_ind = np.concatenate((sel_ind[0], sel_ind[1])) q_idxs = subset_ind[unified_sel_ind] # from subset to full pool remaining_ind = list(set(np.arange(len(pool_dataset))) - set(q_idxs)) sel_dataset = make_tensordataset(pool_dataset, q_idxs) train_dataset = concat_datasets(train_dataset, sel_dataset) pool_dataset = make_tensordataset(pool_dataset, remaining_ind) test_acc = train_validate_model(model, device, train_dataset, valid_dataset, test_dataset) return states, actions
# np.random.seed(seed_num) # torch.manual_seed(seed_num) # print("seed:", seed_num) emb_name = args.pretrained_emb.split("/")[-1][:-4] model_name = "cnn_" + str(args.emb_dim) + "embDim_" + \ str(args.num_kernel) + "kernels_" + \ str(args.stride) + "stride_" + \ str(args.mlp_hidden_size) + "MLPhidden_" + \ str(args.num_epochs) + "epochs" + "_" + emb_name model_name += "_run" + str(run_num + 1) print("Model name:", model_name) log_file = open(save_path + model_name + "_logs.txt", 'w') model = CNN(cnn_args=cnn_args, mlp_args=mlp_args).to(device) print(model) optimizer = optim.Adam(model.parameters(), lr=0.001) loss_fn = nn.BCELoss() train(model=model, optimizer=optimizer, num_epochs=num_epochs, criterion=loss_fn, eval_every=args.step_size, train_loader=train_loader, valid_loader=valid_loader, save_path=save_path, model_name=model_name)
print(len(input_array)) print(target_array) h.randomShuffleTogether(input_array, target_array) train_data, train_labels, test_data, test_labels = h.splitToTrainAndTest(input_array, target_array, 0.66) #method = "train" method = "read" cnn = CNN() cnn.data(train_data, train_labels, test_data, test_labels) cnn.createModel() cnn.findBestWeights(method) cnn.testModel() print() #method = "train" method = "read" lstm = LSTM() lstm.data(train_data, train_labels, test_data, test_labels) lstm.createModel() lstm.findBestWeights(method)
def main(): args = parse_args() train_iter = DataIterator(args.train_file, args.max_seq_len, args.batch_size) dev_iter = DataIterator(args.dev_file, args.max_seq_len, args.batch_size) test_iter = DataIterator(args.test_file, args.max_seq_len, args.batch_size) train_data = DataLoader(args.train_file) vocab_size = len(train_data.vocab) weights = train_data.get_pretrained_weights(args.pretrained_embeddings) args.pretrained_weights = weights # udpate the args args.vocab_size = vocab_size # a small step to solve the problem of command line input interpreted as string args.kernel_sizes = [int(x) for x in args.kernel_sizes] logger = getLogger(args.run_log) print("\nParameters:") for attr, value in sorted(args.__dict__.items()): logger.info("\t{}={}".format(attr.upper(), value)) if args.model == "CNN": model = CNN(args) elif args.model == "BiLSTM": model = BiLSTM(args) elif args.model == "CNN_BiLSTM": model = CNN_BiLSTM(args) elif args.model == "CNN_BiLSTM_ATT": model = CNN_BiLSTM_ATTENTION(args) print(model) # starting training, comment this line if you are load a pretrained model if args.test is False: ## for epoch in range(args.num_epochs): best_acc = 0.0 #model.train() steps = train(epoch, train_iter, model, args, logger) for mode in ['development', 'testing']: if mode == 'development': evaluate(dev_iter, model, args, logger, mode) elif mode == 'testing': test_acc, predictions = evaluate(test_iter, model, args, logger, mode) if test_acc > best_acc: best_acc = test_acc save(model, args.saved_model, 'best', steps) save_predictions(args.test_file, args.prediction_file, predictions) else: model = torch.load(args.saved_model, map_location=lambda storage, loc: storage) #model.eval() evaluate(test_iter, model, args, logger, mode='testing')
from models.CNN import CNN class Main: def __init__(self): Xtrain = self.Xtrain Xtest = self.Xtest Ytrain = self.Ytrain Ytrain = self.Ytest cnn = CNN() cnn.addConvolutionalLayer() cnn.maxPooling() cnn.flattening() cnn.fullConnection() cnn.compile() cnn.trainTest()
def run(args, kwargs): args.model_signature = str(datetime.datetime.now())[0:19] model_name = '' + args.model_name if args.loc_gauss or args.loc_inv_q or args.loc_att: args.loc_info = True if args.att_gauss_abnormal or args.att_inv_q_abnormal or args.att_gauss_spatial or args.att_inv_q_spatial or \ args.att_module or args.laplace_att: args.self_att = True print(args) with open('experiment_log_' + args.operator + '.txt', 'a') as f: print(args, file=f) # IMPORT MODEL====================================================================================================== from models.CNN import CNN as Model # START KFOLDS====================================================================================================== print('\nSTART KFOLDS CROSS VALIDATION\n') # print(f'{args.kfold_test} Test-Train folds each has {args.epochs} epochs for a {1.0/args.kfold_val}/{(args.kfold_val - 1.0)/args.kfold_val} Valid-Train split\n') train_folds, test_folds = kfold_indices_warwick(args.dataset_size, args.kfold_test, seed=args.seed) train_error_folds = [] test_error_folds = [] for current_fold in range(1, args.kfold_test + 1): print('#################### Train-Test fold: {}/{} ####################'.format(current_fold, args.kfold_test)) # DIRECTORY FOR SAVING========================================================================================== snapshots_path = 'snapshots/' dir = snapshots_path + model_name + '_' + args.model_signature + '/' sw = SummaryWriter(f'tensorboard/{model_name}_{args.model_signature}_fold_{current_fold}') if not os.path.exists(dir): os.makedirs(dir) # LOAD DATA===================================================================================================== train_fold, val_fold = kfold_indices_warwick(len(train_folds[current_fold - 1]), args.kfold_val, seed=args.seed) train_fold = [train_folds[current_fold - 1][i] for i in train_fold] val_fold = [train_folds[current_fold - 1][i] for i in val_fold] loc = True if args.loc_info or args.out_loc else False train_set, val_set, test_set = load_breast(train_fold[0], val_fold[0], test_folds[current_fold - 1], loc) # CREATE MODEL================================================================================================== print('\tcreate models') model = Model(args) if args.cuda: model.cuda() # INIT OPTIMIZER================================================================================================ print('\tinit optimizer') if args.optimizer == 'Adam': optimizer = optim.Adam(model.parameters(), lr=args.lr, betas=(0.9, 0.999), weight_decay=args.reg) elif args.optimizer == 'SGD': optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.reg, momentum=0.9) else: raise Exception('Wrong name of the optimizer!') scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=60, gamma=0.1) # PERFORM EXPERIMENT============================================================================================ print('\tperform experiment\n') train_error, test_error = experiment( args, kwargs, current_fold, train_set, val_set, test_set, model, optimizer, scheduler, dir, sw, ) # APPEND FOLD RESULTS=========================================================================================== train_error_folds.append(train_error) test_error_folds.append(test_error) with open('final_results_' + args.operator + '.txt', 'a') as f: print('RESULT FOR A SINGLE FOLD\n' 'SEED: {}\n' 'OPERATOR: {}\n' 'FOLD: {}\n' 'ERROR (TRAIN): {}\n' 'ERROR (TEST): {}\n\n'.format(args.seed, args.operator, current_fold, train_error, test_error), file=f) # ====================================================================================================================== print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-') with open('experiment_log_' + args.operator + '.txt', 'a') as f: print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n', file=f) return np.mean(train_error_folds), np.std(train_error_folds), np.mean(test_error_folds), np.std(test_error_folds)
import torch.nn as nn from torchvision import transforms from torchvision.datasets import ImageFolder from models.CNN import CNN from trainer.trainer import Trainer from data_loader.few_shot_dataloaders import get_few_shot_dataloader transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor() ]) train_set = ImageFolder('/datasets/imagenetwhole/ilsvrc2012/train/', transform=transform) val_set = ImageFolder('/datasets/imagenetwhole/ilsvrc2012/val/', transform=transform) model = CNN() loss = nn.CrossEntropyLoss() train_loader = get_few_shot_dataloader(train_set) val_loader = get_few_shot_dataloader(val_set) trainer = Trainer(model, loss, train_loader, val_loader) trainer.train(epochs=100)
class WCRNN(nn.Module): #, BaseFairseqModel): def __init__(self, w2v_cfg, n_in_channel, nclass, attention=False, activation="Relu", dropout=0, train_cnn=True, rnn_type='BGRU', n_RNN_cell=64, n_layers_RNN=1, dropout_recurrent=0, cnn_integration=False, **kwargs): super(WCRNN, self).__init__() self.w2v = w2v_encoder(w2v_cfg) #Wav2Vec2Config) #self.w2v = Wav2VecEncoder(Wav2Vec2SedConfig, None) self.pooling = nn.Sequential(nn.MaxPool2d((1, 4), (1, 4))) self.n_in_channel = n_in_channel self.attention = attention self.cnn_integration = cnn_integration n_in_cnn = n_in_channel if cnn_integration: n_in_cnn = 1 self.cnn = CNN(n_in_cnn, activation, dropout, **kwargs) if not train_cnn: for param in self.cnn.parameters(): param.requires_grad = False self.train_cnn = train_cnn if rnn_type == 'BGRU': nb_in = self.cnn.nb_filters[-1] if self.cnn_integration: # self.fc = nn.Linear(nb_in * n_in_channel, nb_in) nb_in = nb_in * n_in_channel self.rnn = BidirectionalGRU(nb_in, n_RNN_cell, dropout=dropout_recurrent, num_layers=n_layers_RNN) else: NotImplementedError("Only BGRU supported for CRNN for now") self.dropout = nn.Dropout(dropout) self.dense = nn.Linear(n_RNN_cell * 2, nclass) self.sigmoid = nn.Sigmoid() if self.attention: self.dense_softmax = nn.Linear(n_RNN_cell * 2, nclass) self.softmax = nn.Softmax(dim=-1) def load_cnn(self, state_dict): self.cnn.load_state_dict(state_dict) if not self.train_cnn: for param in self.cnn.parameters(): param.requires_grad = False def load_state_dict(self, state_dict, strict=True): self.w2v.load_state_dice(state_dict["w2v"]) self.cnn.load_state_dict(state_dict["cnn"]) self.rnn.load_state_dict(state_dict["rnn"]) self.dense.load_state_dict(state_dict["dense"]) def state_dict(self, destination=None, prefix='', keep_vars=False): state_dict = { "w2v": self.w2v.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars), "cnn": self.cnn.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars), "rnn": self.rnn.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars), 'dense': self.dense.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars) } return state_dict def save(self, filename): parameters = { 'w2v': self.w2v.state_dict(), 'cnn': self.cnn.state_dict(), 'rnn': self.rnn.state_dict(), 'dense': self.dense.state_dict() } torch.save(parameters, filename) def forward(self, audio): x = audio.squeeze() import pdb pdb.set_trace() feature = self.w2v(x) x = feature['x'] x = x.transpose(1, 0) x = x.unsqueeze(1) # input size : (batch_size, n_channels, n_frames, n_freq) if self.cnn_integration: bs_in, nc_in = x.size(0), x.size(1) x = x.view(bs_in * nc_in, 1, *x.shape[2:]) # conv features before = x x = self.cnn(x) bs, chan, frames, freq = x.size() if self.cnn_integration: x = x.reshape(bs_in, chan * nc_in, frames, freq) if freq != 1: warnings.warn( f"Output shape is: {(bs, frames, chan * freq)}, from {freq} staying freq" ) x = x.permute(0, 2, 1, 3) x = x.contiguous().view(bs, frames, chan * freq) else: x = x.squeeze(-1) x = x.permute(0, 2, 1) # [bs, frames, chan] # rnn features x = self.rnn(x) x = self.dropout(x) strong = self.dense(x) # [bs, frames, nclass] strong = self.sigmoid(strong) if self.attention: sof = self.dense_softmax(x) # [bs, frames, nclass] sof = self.softmax(sof) sof = torch.clamp(sof, min=1e-7, max=1) weak = (strong * sof).sum(1) / (sof.sum(1) + 1e-08) # [bs, nclass] else: weak = strong.mean(1) return strong, weak
def active_learn(exp_num, StrategyClass, subsample): # all strategies use same initial training data and model weights reinit_seed(prop.RANDOM_SEED) test_acc_list = [] if prop.MODEL.lower() == "mlp": model = MLP().apply(weights_init).to(device) if prop.MODEL.lower() == "cnn": model = CNN().apply(weights_init).to(device) if prop.MODEL.lower() == "resnet18": model = models.resnet.ResNet18().to(device) init_weights = copy.deepcopy(model.state_dict()) reinit_seed(exp_num * 10) dataset_pool, valid_dataset, test_dataset = get_data_splits() train_dataset, pool_dataset = stratified_split_dataset( dataset_pool, 2 * prop.NUM_CLASSES, prop.NUM_CLASSES) # # initial data strategy = StrategyClass(dataset_pool, valid_dataset, test_dataset, device) # calculate the overlap of strategy with other strategies strategies = [ MCDropoutSampling, EnsembleSampling, EntropySampling, LeastConfidenceSampling, CoreSetAltSampling, BadgeSampling ] overlapping_strategies = [] for StrategyClass in strategies: overlapping_strategies.append( StrategyClass(dataset_pool, valid_dataset, test_dataset)) t = trange(1, prop.NUM_ACQS + 1, desc="Aquisitions (size {})".format(prop.ACQ_SIZE), leave=True) for acq_num in t: model.load_state_dict(init_weights) test_acc = train_validate_model(model, device, train_dataset, valid_dataset, test_dataset) test_acc_list.append(test_acc) if subsample: subset_ind = np.random.choice(a=len(pool_dataset), size=prop.K, replace=False) pool_subset = make_tensordataset(pool_dataset, subset_ind) sel_ind, remain_ind = strategy.query(prop.ACQ_SIZE, model, train_dataset, pool_subset) q_idxs = subset_ind[sel_ind] # from subset to full pool remaining_ind = list( set(np.arange(len(pool_dataset))) - set(q_idxs)) sel_dataset = make_tensordataset(pool_dataset, q_idxs) train_dataset = concat_datasets(train_dataset, sel_dataset) pool_dataset = make_tensordataset(pool_dataset, remaining_ind) else: # all strategies work on k-sized windows in semi-batch setting sel_ind, remaining_ind = strategy.query(prop.ACQ_SIZE, model, train_dataset, pool_dataset) sel_dataset = make_tensordataset(pool_dataset, sel_ind) pool_dataset = make_tensordataset(pool_dataset, remaining_ind) train_dataset = concat_datasets(train_dataset, sel_dataset) logging.info( "Accuracy for {} sampling and {} acquisition is {}".format( strategy.name, acq_num, test_acc)) return test_acc_list
from data import data path.append('..') for loc in ["SIO"]: print(loc) for d_type in ["std_anomalies"]: print(d_type) file = "../data/" + d_type + "_" + loc + ".nc" d = data(file) d_train, d_test, d_val = d.get_data() if loc == "EPO": input_dim = (40, 60) else: input_dim = (20, 120) n_features = 3 from models.CNN import CNN for j in range(50): l = CNN(input_dim, n_features, batch_norm=True, num_layers=5, Dropout=0.1, num_dense_layers=0, location=loc) losses = l.train(d_train, d_test, num_epochs=80, lr=1e-3) losses = l.train(d_train, d_test, num_epochs=80, lr=1e-4) l.save_weights('../models/saved_models/' + loc + '/' + d_type + '/CNN_ens/ens_' + str(j) + '/cnn') tf.keras.backend.clear_session()
class NEC: def __init__(self, env, args, device='cpu'): """ Instantiate an NEC Agent ---------- env: gym.Env gym environment to train on args: args class from argparser args are from from train.py: see train.py for help with each arg device: string 'cpu' or 'cuda:0' depending on use_cuda flag from train.py """ self.environment_type = args.environment_type self.env = env self.device = device # Hyperparameters self.epsilon = args.initial_epsilon self.final_epsilon = args.final_epsilon self.epsilon_decay = args.epsilon_decay self.gamma = args.gamma self.N = args.N # Transition queue and replay memory self.transition_queue = [] self.replay_every = args.replay_every self.replay_buffer_size = args.replay_buffer_size self.replay_memory = ReplayMemory(self.replay_buffer_size) # CNN for state embedding network self.frames_to_stack = args.frames_to_stack self.embedding_size = args.embedding_size self.in_height = args.in_height self.in_width = args.in_width self.cnn = CNN(self.frames_to_stack, self.embedding_size, self.in_height, self.in_width).to(self.device) # Differentiable Neural Dictionary (DND): one for each action self.kernel = inverse_distance self.num_neighbors = args.num_neighbors self.max_memory = args.max_memory self.lr = args.lr self.dnd_list = [] for i in range(env.action_space.n): self.dnd_list.append( DND(self.kernel, self.num_neighbors, self.max_memory, args.optimizer, self.lr)) # Optimizer for state embedding CNN self.q_lr = args.q_lr self.batch_size = args.batch_size self.optimizer = get_optimizer(args.optimizer, self.cnn.parameters(), self.lr) def choose_action(self, state_embedding): """ Choose epsilon-greedy policy according to Q-estimates from DNDs """ if random.uniform(0, 1) < self.epsilon: return random.randint(0, self.env.action_space.n - 1) else: qs = [dnd.lookup(state_embedding) for dnd in self.dnd_list] action = torch.argmax(torch.cat(qs)) return action def Q_lookahead(self, t, warmup=False): """ Return the N-step Q-value lookahead from time t in the transition queue """ if warmup or len(self.transition_queue) <= t + self.N: lookahead = [tr.reward for tr in self.transition_queue[t:]] discounted = discount(lookahead, self.gamma) Q_N = torch.tensor([discounted], requires_grad=True) return Q_N else: lookahead = [ tr.reward for tr in self.transition_queue[t:t + self.N] ] discounted = discount(lookahead, self.gamma) state = self.transition_queue[t + self.N].state state = torch.tensor(state).permute(2, 0, 1).unsqueeze(0) # (N,C,H,W) state = state.to(self.device) state_embedding = self.cnn(state) Q_a = [dnd.lookup(state_embedding) for dnd in self.dnd_list] maxQ = torch.cat(Q_a).max() Q_N = discounted + (self.gamma**self.N) * maxQ Q_N = torch.tensor([Q_N], requires_grad=True) return Q_N def Q_update(self, Q, Q_N): """ Return the Q-update for DND updates """ return Q + self.q_lr * (Q_N - Q) def update(self): """ Iterate through the transition queue and make NEC updates """ # Insert transitions into DNDs for t in range(len(self.transition_queue)): tr = self.transition_queue[t] action = tr.action tr = self.transition_queue[t] state = torch.tensor(tr.state).permute(2, 0, 1) # (C,H,W) state = state.unsqueeze(0).to(self.device) # (N,C,H,W) state_embedding = self.cnn(state) dnd = self.dnd_list[action] Q_N = self.Q_lookahead(t).to(self.device) embedding_index = dnd.get_index(state_embedding) if embedding_index is None: dnd.insert(state_embedding.detach(), Q_N.detach().unsqueeze(0)) else: Q = self.Q_update(dnd.values[embedding_index], Q_N) dnd.update(Q.detach(), embedding_index) Q_N = Q_N.detach().to(self.device) self.replay_memory.push(tr.state, action, Q_N) # Commit inserts for dnd in self.dnd_list: dnd.commit_insert() # Train CNN on minibatch for t in range(len(self.transition_queue)): if t % self.replay_every == 0 or t == len( self.transition_queue) - 1: # Train on random mini-batch from self.replay_memory batch = self.replay_memory.sample(self.batch_size) actual_Qs = torch.cat([sample.Q_N for sample in batch]) predicted_Qs = [] for sample in batch: state = torch.tensor(sample.state).permute(2, 0, 1) # (C,H,W) state = state.unsqueeze(0).to(self.device) # (N,C,H,W) state_embedding = self.cnn(state) dnd = self.dnd_list[sample.action] predicted_Q = dnd.lookup(state_embedding, update_flag=True) predicted_Qs.append(predicted_Q) predicted_Qs = torch.cat(predicted_Qs).to(self.device) loss = torch.dist(actual_Qs, predicted_Qs) self.optimizer.zero_grad() loss.backward() self.optimizer.step() for dnd in self.dnd_list: dnd.update_params() # Clear out transition queue self.transition_queue = [] def run_episode(self): """ Train an NEC agent for a single episode: Interact with environment Append (state, action, reward) transitions to transition queue Call update at the end of the episode """ if self.epsilon > self.final_epsilon: self.epsilon = self.epsilon * self.epsilon_decay state = self.env.reset() if self.environment_type == 'fourrooms': fewest_steps = self.env.shortest_path_length(self.env.state) total_steps = 0 total_reward = 0 total_frames = 0 done = False while not done: state_embedding = torch.tensor(state).permute(2, 0, 1) # (C,H,W) state_embedding = state_embedding.unsqueeze(0).to(self.device) state_embedding = self.cnn(state_embedding) action = self.choose_action(state_embedding) next_state, reward, done, _ = self.env.step(action) self.transition_queue.append(Transition(state, action, reward)) total_reward += reward total_frames += self.env.skip total_steps += 1 state = next_state self.update() if self.environment_type == 'fourrooms': n_extra_steps = total_steps - fewest_steps return n_extra_steps, total_frames, total_reward else: return total_frames, total_reward def warmup(self): """ Warmup the DND with values from an episode with a random policy """ state = self.env.reset() total_reward = 0 total_frames = 0 done = False while not done: action = random.randint(0, self.env.action_space.n - 1) next_state, reward, done, _ = self.env.step(action) total_reward += reward total_frames += self.env.skip self.transition_queue.append(Transition(state, action, reward)) state = next_state for t in range(len(self.transition_queue)): tr = self.transition_queue[t] state_embedding = torch.tensor(tr.state).permute(2, 0, 1) # (C,H,W) state_embedding = state_embedding.unsqueeze(0).to(self.device) state_embedding = self.cnn(state_embedding) action = tr.action dnd = self.dnd_list[action] Q_N = self.Q_lookahead(t, True).to(self.device) if dnd.keys_to_be_inserted is None and dnd.keys is None: dnd.insert(state_embedding, Q_N.detach().unsqueeze(0)) else: embedding_index = dnd.get_index(state_embedding) if embedding_index is None: state_embedding = state_embedding.detach() dnd.insert(state_embedding, Q_N.detach().unsqueeze(0)) else: Q = self.Q_update(dnd.values[embedding_index], Q_N) dnd.update(Q.detach(), embedding_index) self.replay_memory.push(tr.state, action, Q_N.detach()) for dnd in self.dnd_list: dnd.commit_insert() # Clear out transition queue self.transition_queue = [] return total_frames, total_reward