def log_histogram_and_value(self,epoch_d_loss,epoch_g_loss,epoch_d_hard_loss,epoch_d_soft_loss,epoch): log_value('d_loss',epoch_d_loss,epoch) log_value('g_loss',epoch_g_loss,epoch) log_value('d_hard_loss',epoch_d_hard_loss,epoch) log_value('d_soft_loss',epoch_d_soft_loss,epoch) log_histogram('entity',self.embedding.entity_embedding.weight.data.cpu(),epoch) log_histogram('relation',self.embedding.relation_embedding.weight.data.cpu(),epoch) head_list = [] relation_list = [] tail_list = [] for triple in self.data.valid_triples_with_reverse: head_list.append(triple[0]) relation_list.append(triple[1]) tail_list.append(triple[2]) with torch.no_grad(): head_list = torch.LongTensor(head_list).cuda() relation_list = torch.LongTensor(relation_list).cuda() tail_list = torch.LongTensor(tail_list).cuda() head_embedding, relation_embedding, tail_embedding = self.embedding.forward(head_list, relation_list, e2=tail_list) g_output = self.geneartor.forward(head_embedding,relation_embedding).data g_true_loss = torch.mean(self.discriminator.forward(head_embedding,relation_embedding,tail_embedding)) g_fake_loss = torch.mean(self.discriminator.forward(head_embedding,relation_embedding,g_output)) log_value('valid_g_true_loss',g_true_loss.cpu(),epoch) log_value('valid_g_fake_loss',g_fake_loss.cpu(),epoch)
def log_histogram_and_value(self, epoch_loss, epoch): log_value('loss', epoch_loss, epoch) log_histogram('entity', self.model.entity_embedding.weight.data.cpu(), epoch) log_histogram('relation', self.model.relation_embedding.weight.data.cpu(), epoch)
def flush(self, step): ''' Log and start accumnulating again. ''' for key in self.vals: log_value(key, np.array(self.vals[key]).mean(), step=step) for key in self.hist: bincounts, bin_edges = np.histogram(np.array(self.hist[key]), bins=24) log_histogram(key, (bin_edges, bincounts), step=step) #print ('printed hist for key: %s. %d values' % (key, len(self.hist[key]))) self.vals = {} self.hist = {}
def log_stats(data, name, step): """Logs statistics on tensorboard for data tensor. Args: data (torch.Tensor): torch tensor. name (str): name under which stats for the tensor should be logged. step (int): step used for logging """ log_value('{}/highest'.format(name), torch.max(data).item(), step=step) log_value('{}/lowest'.format(name), torch.min(data).item(), step=step) log_value('{}/mean'.format(name), torch.mean(data).item(), step=step) log_value('{}/std'.format(name), torch.std(data).item(), step=step) log_histogram('{}'.format(name), data.data.cpu().numpy(), step=step)
args = parser.parse_args() conn = sqlite3.connect(args.pred_db_file) c = conn.cursor() c.execute('ATTACH ? AS "attached"', (args.gt_db_file, )) c.execute( 'SELECT pr.value, gt.value FROM properties pr INNER JOIN attached.properties gt ' 'WHERE pr.key == "yaw" AND gt.key == "yaw" AND pr.objectid == gt.objectid ORDER BY pr.objectid ASC' ) entries = c.fetchall() c.close() logging.info( 'Total %d objects in both the open and the ground truth databases.' % len(entries)) logging.debug(pformat(entries)) entries = [(-float(x), float(y)) for (x, y) in entries ] # "-" to fix a bug with sign in synthetic data. pr_yaws, gt_yaws = zip(*entries) metrics = angle360_l1(np.array(pr_yaws), np.array(gt_yaws)) bincounts, bin_edges = np.histogram(metrics, bins=24) configure(args.tflog_dir, flush_secs=10) step = args.epoch * 4168 // 10 # 8396 #print (step) log_histogram('hist/test/yaw', (bin_edges, bincounts), step=step) log_value('metrics/test/yaw', metrics.mean() * 10, step=step) print(metrics.mean() * 10)
# get epoch loss print("--> {} epoch {}".format(mode, id_epoch)) epoch_loss = train_eval(mode, model, optimizer, dataloader) lr = list(get_lr_optimizer(optimizer))[0] print("-----------") print("Done! {} epoch {} loss {} lr {}".format(mode, id_epoch, epoch_loss, lr)) send("{} epoch {}/{} loss {}".format(mode, id_epoch, n_epochs, epoch_loss)) # record loss log_value("loss/{}".format(mode), epoch_loss, id_epoch) log_value("lr/{}".format(mode), lr, id_epoch) for v in model.state_dict(): log_histogram("Layer {}".format(v), model.state_dict()[v], id_epoch) #save_model(model, optimizer, id_epoch, path_out, name_model='{:03d}'.format(id_epoch)) # store model if val loss improves if mode==VAL: if best_loss > epoch_loss: # update loss best_loss = epoch_loss save_model(model, optimizer, id_epoch, path_out, name_model='best') scheduler.step(epoch_loss)
def train_and_test(flags, corruption_level=0, gold_fraction=0.5, get_C=uniform_mix_C): np.random.seed(1) torch.manual_seed(1) torch.cuda.manual_seed(1) C = get_C(corruption_level) gold, silver = prepare_data(C, gold_fraction) print("Gold shape = {}, Silver shape = {}".format(gold.images.shape, silver.images.shape)) # TODO : test on whole set test_x = torch.from_numpy(mnist.test.images[:500].reshape([-1, 1, 28, 28])) test_y = torch.from_numpy(mnist.test.labels[:500]).type(torch.LongTensor) print("Test shape = {}".format(test_x.shape)) model = LeNet() optimizer = torch.optim.Adam([p for p in model.parameters()], lr=0.001) for step in range(flags.num_steps): x, y = silver.next_batch(flags.batch_size) y, y_true = np.array([l[0] for l in y]), np.array([l[1] for l in y]) x_val, y_val = gold.next_batch(min(flags.batch_size, flags.nval)) x, y = torch.from_numpy(x.reshape( [-1, 1, 28, 28])), torch.from_numpy(y).type(torch.LongTensor) x_val, y_val = torch.from_numpy(x_val.reshape( [-1, 1, 28, 28])), torch.from_numpy(y_val).type(torch.LongTensor) # forward if flags.method == "l2w": ex_wts = reweight_autodiff(model, x, y, x_val, y_val) logits, loss = model.loss(x, y, ex_wts) if step % dbg_steps == 0: tbrd.log_histogram("ex_wts", ex_wts, step=step) tbrd.log_value("More_than_0.01", sum([x > 0.01 for x in ex_wts]), step=step) tbrd.log_value("More_than_0.05", sum([x > 0.05 for x in ex_wts]), step=step) tbrd.log_value("More_than_0.1", sum([x > 0.1 for x in ex_wts]), step=step) mean_on_clean_labels = np.mean( [ex_wts[i] for i in range(len(y)) if y[i] == y_true[i]]) mean_on_dirty_labels = np.mean( [ex_wts[i] for i in range(len(y)) if y[i] != y_true[i]]) tbrd.log_value("mean_on_clean_labels", mean_on_clean_labels, step=step) tbrd.log_value("mean_on_dirty_labels", mean_on_dirty_labels, step=step) else: logits, loss = model.loss(x, y) print("Loss = {}".format(loss)) # backward optimizer.zero_grad() loss.backward() optimizer.step() tbrd.log_value("loss", loss, step=step) if step % dbg_steps == 0: model.eval() pred = torch.max(model.forward(test_x), 1)[1] test_acc = torch.sum(torch.eq(pred, test_y)).item() / float( test_y.shape[0]) model.train() print("Test acc = {}.".format(test_acc)) tbrd.log_value("test_acc", test_acc, step=step)
get_data(DATA_DIR + '/goog.csv') dates = np.reshape(dates, (len(dates), 1)) svm = SVR(kernel=kernel, C=C, degree=degree, gamma=gamma) svm.fit(dates, prices) predictions = svm.predict(dates) (rmse, mae, r2) = eval_metrics(prices, predictions) log_metrics('RMSE', rmse) log_metrics('MAE', mae) log_metrics('R2', r2) plt.plot(dates, prices, color='black', label="Data", marker='*') plt.plot(dates, predictions, color='red', label="Predictions", marker='o') plt.xlabel('Date') plt.ylabel('Price') plt.title('SVM predictions with ' + kernel + ' kernel') plt.legend() plt.savefig('svm.png') log_value('RMSE', rmse) log_value('MAE', mae) log_value('R2', r2) filename = MODEL_DIR + '/model.joblib' joblib.dump(svm, filename) img = cv2.imread('svm.png') log_histogram('Stock Prices', prices, step=1) log_images('Stock Predictions Graph', [img])
def log_histogram(self, name, histogram, step=-1): if step == -1: log_histogram(name, histogram, self.global_step) else: log_histogram(name, histogram, step) return self
def log_histogram(harn, key, value, n_iter): if False: print('{}={} @ {}'.format(key, value, n_iter)) if tensorboard_logger: tensorboard_logger.log_histogram(key, value, n_iter)
def log_tensorboard(self, weights, step): sparsity = np.mean(weights.emission < 1e-10) tl.log_value('sparsity_coefficient', sparsity, step) tl.log_histogram('weight matrix', weights.emission.tolist(), step)