def train_model(model, train_iterator, val_iterator, optimizer, scheduler, criterion, n_epochs, clip, model_path): ''' 开始训练我们的模型: 1.每一次epoch,都会检查模型是否达到的最佳的validation loss,如果达到了,就更新 最好的validation loss以及保存模型参数 2.打印每个epoch的loss以及困惑度。 ''' best_valid_loss = float('inf') for epoch in range(n_epochs): start_time = time.time() train_loss = train(model, train_iterator, optimizer, criterion, clip) valid_loss = evaluate(model, val_iterator, criterion) end_time = time.time() epoch_mins, epoch_secs = epoch_time(start_time, end_time) if valid_loss < best_valid_loss: best_valid_loss = valid_loss torch.save(model.state_dict(), model_path) scheduler.step() print('epoch:{},time-mins:{},time-secs:{}'.format( epoch + 1, epoch_mins, epoch_secs)) print('train loss:{},train perplexity:{}'.format( train_loss, math.exp(train_loss))) print('val loss:{}, val perplexity:{}'.format(valid_loss, math.exp(valid_loss)))
def time_updated(self): """ Should be called once the system time is set correctly (ex. from NTP or a browser client). Updates the metadata now that we know the real time of the data we've been collecting. """ self.start_time_offset = epoch_time() * 1000 - time.ticks_ms() for log in self.our_logs_without_start_time: log["start_time_offset"] = self.start_time_offset self.our_logs_without_start_time = [] with open(self.meta_file, "r") as f: with open(self.tmp_meta, "w") as f2: # print("time_updated():") for line in f.readlines(): # print("<%s>" % line.strip()) index, active, start_time, start_time_offset = line.strip().split(",") if start_time_offset == "None": start_time_offset = str(self.start_time_offset) line2 = ",".join([index, active, start_time, start_time_offset]) # print("[%s]" % line2) f2.write(line2) f2.write("\n") os.rename(self.tmp_meta, self.meta_file)
def collect(self): time_start = datetime.utcnow() collected_data = self.collect_fn() time_end = datetime.utcnow() data = { 'nick': self.nick, 'time_start': epoch_time(time_start), 'time_end': epoch_time(time_end), 'time_start_pretty': format_time(time_start), 'time_end_pretty': format_time(time_end), 'data': collected_data } log('%s: Collected the following data: %s' % (self.name, json.dumps(data, indent=2)), self.verbose) self.upload_queue.append(data) self.upload()
SAVE_DIR = 'models' MODEL_SAVE_PATH = os.path.join(SAVE_DIR, 'transformer-seq2seq.pt') best_valid_loss = float('inf') if not os.path.isdir(f'{SAVE_DIR}'): os.makedirs(f'{SAVE_DIR}') train_total = [] valid_total = [] test_total = [] for epoch in range(N_EPOCHS): start_time = time.time() train_loss = trainer(model, train_iter, optimizer, criterion, CLIP) valid_loss = evaluate(model, val_iter, criterion) train_total.append(train_loss) valid_total.append(valid_loss) end_time = time.time() epoch_mins, epoch_secs = epoch_time(start_time, end_time) if valid_loss < best_valid_loss: best_valid_loss = valid_loss torch.save(model.state_dict(), MODEL_SAVE_PATH) print(f'| Epoch: {epoch+1:03} | Time: {epoch_mins}m {epoch_secs}s| Train Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f} | Val. Loss: {valid_loss:.3f} | Val. PPL: {math.exp(valid_loss):7.3f} |')
print(model.embedding.weight.data) print('Just started:') valid_loss, valid_acc = util.evaluate(model, valid_iterator, criterion, labelName) print(f'\t Val. Loss: {valid_loss:.3e} | Val. Acc: {valid_acc*100:.2e}%') for epoch in range(N_EPOCHS): start_time = time.time() train_loss, train_acc = util.train(model, train_iterator, optimizer, criterion, labelName) valid_loss, valid_acc = util.evaluate(model, valid_iterator, criterion, labelName) end_time = time.time() epoch_mins, epoch_secs = util.epoch_time(start_time, end_time) if valid_loss < best_valid_loss: best_valid_loss = valid_loss torch.save(model.state_dict(), modelName) print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s') print( f'\tTrain Loss: {train_loss:.3e} | Train Acc: {train_acc*100:.2e}%' ) print( f'\t Val. Loss: {valid_loss:.3e} | Val. Acc: {valid_acc*100:.2e}%' ) model.load_state_dict(torch.load(modelName)) test_loss, test_acc = util.evaluate(model, test_iterator, criterion, labelName)
disp.fill_rect(0, 20, 128, 128, 0) disp.text( "In: %.2fA %.0fW" % (sense.get_current("in") or 0, sense.get_power("in") or 0), 0, 22) disp.text( "Out: %.2fA %.0fW" % (sense.get_current("out") or 0, sense.get_power("out") or 0), 0, 32) disp.text( "%02d:%02d:%02d UTC %3s" % (time.localtime()[3:6] + (len(open_web_sockets), )), 0, 55) disp.show() ws_text = ",".join([ str(epoch_time() * 1000), str(sense.get_power("in")), str(sense.get_power("out")), str(sense.get_voltage("in")), str(sense.get_voltage("out")), str(sense.get_current("in")), str(sense.get_current("out")), str(sense.get_available_power("in")), ]) # print(ws_text) for ws in open_web_sockets: start_time = time.ticks_ms() ws.SendText(ws_text) gc.collect()