def __init__(self, data_set: DataSet, ngram_size: int, emb_size: int, hid_size: int): """Initialize the language recognition module. Args: data_set: the dataset from which the set of input symbols and output classes (languages) can be extracted ngram_size: size of n-gram features (e.g., use 1 for unigrams, 2 for bigrams, etc.) emb_size: size of the character embedding vectors hid_size: size of the hidden layer of the FFN use for scoring """ # Keep the size of the ngrams self.ngram_size = ngram_size # Calculate the embedding alphabet and create the embedding sub-module feat_set = self.alphabet(data_set) self.register("emb", Embedding(feat_set, emb_size)) # Encoding (mapping between langs and ints) lang_set = set(lang for (_, lang) in data_set) self.enc = Encoding(lang_set) # Scoring FFN sub-module self.register("ffn", FFN(idim=emb_size, hdim=hid_size, odim=len(lang_set))) # Additional check to verify that all the registered # parameters actually require gradients. This allows # to identify the "bug" in the embedding module. assert all([param.requires_grad is True for param in self.params()])
def __init__(self, type, x=0, y=0): Object.__init__(self, type, x, y) self.brain = FFN() self.step = 8 #print self.x, self.y self.stroll = [random.choice(['up', 'down', 'left', 'right'])] self.energy = 70
def __init__(self, h=8, d_model=512, d_ff=2048, drop_rate=0.1): super(DecoderLayer, self).__init__() # Self Attention Layer # query key and value come from previous layer. self.self_attn = SelfAttention(h, d_model, drop_rate) # Source Target Attention Layer # query come from encoded space. # key and value come from previous self attention layer self.st_attn = SourceTargetAttention(h, d_model, drop_rate) self.ff = FFN(d_model, d_ff)
def __init__(self, data_set: DataSet, emb_size: int, hid_size: int): """Initialize the language recognition module. Args: data_set: the dataset from which the set of input symbols and output classes (languages) can be extracted emb_size: size of the character embedding vectors hid_size: size of the hidden layer of the FFN use for scoring """ char_set = char_set_in(data_set) # Embedding self.register("emb", Embedding(char_set, emb_size)) lang_set = set(lang for (name, lang) in data_set) lang_num = len(lang_set) # Encoding (mapping between langs and ints) self.enc = Encoding(lang_set) # FFN self.register("ffn", FFN(idim=emb_size, hdim=hid_size, odim=lang_num))
class Predator(Object): senserange = 80 def __init__(self, type, x=0, y=0): Object.__init__(self, type, x, y) self.brain = FFN() self.step = 8 #print self.x, self.y self.stroll = [random.choice(['up', 'down', 'left', 'right'])] self.energy = 70 def tick(self, env): params = self.sense(env) result = self.brain.act(params) if params[5] == 1: if len(self.stroll)>=2: self.act(self.stroll[-1], env) self.stroll.pop() else: action = ['up', 'down', 'left', 'right'] re = ['down', 'up', 'right', 'left'] del action[re.index(self.stroll[0])] self.stroll = [action[random.randrange(0,3)]]*random.randrange(50,100) return action = self.choseaction(result, env) #if action == 'eat': #print params, result, action self.act(action, env) def sense(self, env): prey = env.find('prey', (self.x, self.y), Predator.senserange) err = 2 result = [0, 0, 0, 0, 0, 0] if prey != None and self.name not in prey.safe: if abs(prey.y-self.y) <= err and abs(prey.x-self.x) <= err: result[4] += 1 # on else: if prey.y + err < self.y: result[0] += 1 # up if prey.y - err > self.y: result[1] += 1 # down if prey.x + err < self.x: result[2] += 1 # left if prey.x - err > self.x: result[3] += 1 # right else: result[5] += 1 # nothing return result def choseaction(self, result, env): actions = ['up', 'down', 'left', 'right', 'eat'] maxr = max(result) list = [] for i in range(len(result)): if result[i] > (maxr*0.8): list.append(i) if len(list)==4: #list = list * 2 prey = env.find('prey', (self.x, self.y), diag) if prey != None and self.name not in prey.safe: if prey.y < self.y: list += [0]*5 if prey.y > self.y: list += [1]*5 if prey.x < self.x: list += [2]*5 if prey.x > self.x: list += [3]*5 return actions[random.choice(list)] def act(self, action, env): self.energy -= 0.7 if self.energy >= 70: env.snake.append(Predator('snake')) if self.energy <= 0: env.snake.remove(self) print 'snake die from food' step = self.step #if state == 6: # step = Prey.foodrange if action == 'up': self.y = self.y-step if action == 'down': self.y = self.y+step if action == 'left': self.x = self.x-step if action == 'right': self.x = self.x+step self.adjustxy() if action == 'eat': self.eat(env) #print self.x, self.y #print action def adjustxy(self): constrain = 0 if self.x < constrain: self.x = constrain self.stroll = ['right']*2 if self.x > width-constrain: self.x = width-constrain self.stroll = ['left']*2 if self.y < constrain: self.y = constrain self.stroll = ['down']*2 if self.y > height-constrain: self.y = height-constrain self.stroll = ['up']*2 def eat(self, env): prey = env.find('prey', (self.x, self.y), Predator.senserange) if prey!=None: if self.name == 'snake': self.energy += 15 env.prey.remove(prey) elif random.random()>0.5: self.energy += 15 env.prey.remove(prey) else: print 'half escape'
# Normalize the data if type(norm_method) == list: for nm in norm_method: otu_handler.normalize_data(method=nm) else: otu_handler.normalize_data(method=norm_method) # Set train and validation split otu_handler.set_train_val() # Set the GPU to use. use_gpu = torch.cuda.is_available() if use_gpu: torch.cuda.set_device(gpu_to_use) if not os.path.isdir(output_dir): os.mkdir(output_dir) save_params = (os.path.join(output_dir, model_name), os.path.join(output_dir, log_name)) if use_convs: print('Using Conv Net') ffn = ConvFFN(hidden_dim, otu_handler, seq_len, use_gpu=use_gpu) else: ffn = FFN(hidden_dim, otu_handler, seq_len, use_gpu=use_gpu) ffn.do_training(batch_size, num_epochs, learning_rate, samples_per_epoch, save_params=save_params)
out = net(words_t) loss = loss_fn(out, label_t) pred = out.argmax(dim=1) acc = pred.eq(label_t).float().sum(dim=-1) / batch_size return loss.item(), acc.item() char2idx, idx2char = get_chars(os.path.join(BASE_PATH, config.chars_path)) label2idx, idx2label = get_labels(os.path.join(BASE_PATH, config.labels_path)) train_data = TrainData(os.path.join(BASE_PATH, config.train_path), char2idx, label2idx) valid_data = ValidData(os.path.join(BASE_PATH, config.valid_path), char2idx, label2idx) train_loader = DataLoader(train_data, batch_size=config.batch_size, shuffle=True, collate_fn=collate_fn) valid_loader = DataLoader(valid_data, batch_size=config.batch_size, shuffle=False, collate_fn=collate_fn) net = FFN(config).to(device) print(net) show_model_size(net) try: model_path = os.path.join(BASE_PATH, config.load_model_path)) net.load_state_dict(torch.load(os.path.join(model_path, '%s_%.8f_lr_%d_hidsize.pt' % (net.name, config.lr, config.hidden_size)))) opt = optim.Adam(net.parameters(), lr=config.cur_lr) print('load pre-train model succeed.') except Exception as e: print(e) opt = optim.Adam(net.parameters(), lr=config.lr) print('load pre-train model failed.') loss_fn = nn.CrossEntropyLoss() lr_sche = lr_scheduler.ReduceLROnPlateau(opt, mode='min', factor=0.9, patience=30, verbose=True, min_lr=1e-6)
with torch.no_grad(): batch_size = words_t.size(0) out = net(words_t) pred = out.argmax(dim=1) return pred char2idx, idx2char = get_chars(os.path.join(BASE_PATH, config.chars_path)) label2idx, idx2label = get_labels(os.path.join(BASE_PATH, config.labels_path)) test_data = TestData(os.path.join(BASE_PATH, config.test_path), char2idx) test_loader = DataLoader(test_data, batch_size=config.batch_size, shuffle=False, collate_fn=collate_fn) net = FFN(config).to(device) print(net) show_model_size(net) try: model_path = os.path.join(BASE_PATH, config.load_model_path) net.load_state_dict(torch.load(os.path.join(model_path, '%s_%.8f_lr_%d_hidsize_cpu.pt' % (net.name, config.lr, config.hidden_size)))) print('load pre-train model succeed.') except Exception as e: print(e) print('load pre-train model failed.') predfile_path = os.path.join(BASE_PATH, config.pred_path) with open(predfile_path, 'w', encoding='utf-8') as f: for words_t in test_loader: words_t = words_t.to(device)