ax.set_yticks(ticks) ax.set(title=title) plt.savefig(cfg.train_dir / fname, bbox_inches="tight") plt.close() # df = pd.DataFrame(data=var.cpu().numpy(), # columns=['%d' % x for x in range(1, size)]) # corr = df.corr() # mask = np.zeros_like(corr, dtype=np.bool) # mask[np.triu_indices_from(mask)] = True # f, ax = plt.subplots(figsize=(11, 9)) # cmap = sns.diverging_palette(10, 10, as_cmap=True) # sns.heatmap(corr, mask=mask, cmap=cmap, center=0, # square=True, linewidths=.5, cbar_kws={"shrink": .5}) # plt.savefig(cfg.train_dir / fname, bbox_inches='tight') # plt.close() def print_grads(module): lst = [] for i, p in enumerate(module.parameters()): lst.append(p.grad.norm().detach().cpu().numpy()) print(['%.3f' % x for x in lst]) if __name__ == "__main__": dictionary = yaml.load(config) cfg = nomen.Config(dictionary) cfg.parse_args() fit_hvm(cfg)
def create_cfg(): dictionary = yaml.load(config) cfg = nomen.Config(dictionary) cfg.parse_args() return cfg
"""Train a multi-class classification problem. Use the embedding for each ingredient in a recipe to predict the rest of the ingredients in the recipe. Debug the model with a held-out validation set. """ import tensorflow as tf import collections import nomen import numpy as np layers = tf.contrib.layers cfg = nomen.Config() cfg.define_string('train_path', '../dat/recipes', 'training file') cfg.define_string('save_path', '/tmp', 'save path') cfg.define_integer('embedding_size', 100, 'size of the embeddings') cfg.define_integer('epochs_to_train', 15, 'number of epochs to train') cfg.define_float('learning_rate', 0.025, 'initial learning rate') cfg.define_float('regularization', 0.01, 'regularization strength') cfg.define_string('optimizer', 'adam', 'optimizer') cfg.parse_args() flatten = lambda l: [item for sublist in l for item in sublist] def read_data(train_path): sentences = [] with open(train_path, 'rb') as f: for line in f:
def main(_): cfg = nomen.Config('deep_latent_gaussian_model_config.yml') train(cfg)
def main(_): cfg = nomen.Config('sigmoid_belief_network_config.yml') train(cfg)
import os import nomen def get_path(file_name): current_dir = os.path.dirname(os.path.abspath(__file__)) return os.path.join(current_dir, file_name) cfg = nomen.Config(get_path('config.yml'))
with torch.no_grad(): log_prob = log_prob_fn(model, p_u, data) total_log_prob += log_prob.sum().item() return total_log_prob / len(loader.dataset) def plot(step, out_dir, model, dataset, device): # generate some examples np_x = np.linspace(-1.1, 2.1, 50).astype(np.float32) np_y = np.linspace(-0.75, 1.25, 50).astype(np.float32) xx, yy = np.meshgrid(np_x, np_y) np_u = np.stack([xx.flatten(), yy.flatten()]).T u_tens = torch.from_numpy(np_u).to(device) _, log_prob = model(u_tens) log_prob = log_prob.sum(-1).detach().cpu().numpy() log_prob_grid = log_prob.reshape((len(xx), len(xx))) xlabels = ['%.1f' % x for x in np_x] ylabels = ['%.1f' % x for x in np_y] ax = sns.heatmap(log_prob_grid[::-1], xticklabels=xlabels, yticklabels=ylabels[::-1]) ax.set_title('Heatmap of log likelihood of the flow') name = "log-lik_maf_%d.png" % step plt.savefig(out_dir / name) plt.close() if __name__ == "__main__": cfg = nomen.Config(dictionary=yaml.load(config)) main(cfg)