Exemple #1
0
def run(constant_overwrites):
    x_train, y_train, x_val, y_val, x_test, y_test = load_mnist_dataset(
        flatten=True)

    plt.figure(figsize=[6, 6])
    for i in range(4):
        plt.subplot(2, 2, i + 1)
        plt.title('Label: %i' % y_train[i])
        plt.imshow(x_train[i].reshape([28, 28]), cmap='gray')

    constants = merge_dict(get_constants(), constant_overwrites)
    n_classes = constants['n_classes']
    data = {
        'X_train': x_train,
        'y_train': one_hot_encode(y_train, n_classes),
        'X_val': x_val,
        'y_val': one_hot_encode(y_val, n_classes),
        'X_test': x_test,
        'y_test': one_hot_encode(y_test, n_classes)
    }
    input_x, input_y = get_inputs(constants)
    parameters = get_parameters(constants)
    optimizer, loss_op, model, y_ = model_builder(network_builder, input_x,
                                                  input_y, parameters,
                                                  constants)
    train(data,
          constants, (input_x, input_y),
          optimizer,
          loss_op,
          model,
          y_,
          minibatch=True)
Exemple #2
0
def denoising_autoencoder(constant_overwrites):
    img_shape, attr, x_train, x_test = load_faces_dataset()
    constants = merge_dict(get_constants(), constant_overwrites)
    constants['img_shape'] = img_shape
    constants['code_size'] = 512
    autoencoder, encoder, decoder = model_builder(network_builder, constants)
    reset_tf_session()
    iterations = 25
    for i in range(iterations):
        print('Epoch %i/%i, Generating corrupted samples...' %
              (i + 1, iterations))
        x_train_noise = apply_gaussian_noise(x_train)
        x_test_noise = apply_gaussian_noise(x_test)

        # continue to train model with new noise-augmented data
        autoencoder.fit(x=x_train_noise,
                        y=x_train,
                        epochs=1,
                        validation_data=[x_test_noise, x_test],
                        callbacks=[TQDMProgressCallback()],
                        verbose=0)

    x_test_noise = apply_gaussian_noise(x_test)
    denoising_mse = autoencoder.evaluate(x_test_noise, x_test, verbose=0)
    print('Denoising MSE:', denoising_mse)
    for i in range(5):
        img = x_test_noise[i]
        visualize(img, encoder, decoder)
Exemple #3
0
def run(constant_overwrites):
    env = gym.make('CartPole-v0')
    state_size = env.observation_space.shape[0]
    action_size = env.action_space.n
    constants = merge_dict(get_constants(), constant_overwrites)

    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    agent = DQNAgent(state_size, action_size, constants)

    n_episodes = constants['n_episodes']
    batch_size = constants['batch_size']
    for e in range(n_episodes):
        state = env.reset()
        state = np.reshape(state, [1, state_size])
        for time in range(5000):
            env.render()
            action = agent.act(state)
            next_state, reward, done, _ = env.step(action)
            reward = reward if not done else -10
            next_state = np.reshape(next_state, [1, state_size])
            agent.remember(state, action, reward, next_state, done)
            state = next_state
            if done:
                print('episode: {}/{}, score: {}, e: {:.2}'.format(e, n_episodes, time, agent.epsilon))
                break

            if len(agent.memory) > batch_size:
                agent.replay(batch_size)

            if e % 50 == 0:
                agent.save(OUTPUT_DIR + 'weights_' + '{:04d}'.format(e) + '.hdf5')
Exemple #4
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    env = GameEnvironment(partial=False, size=5)

    n_hidden = constants['n_hidden']
    start_epsilon = constants['start_epsilon']
    end_epsilon = constants['end_epsilon']
    annealing_steps = constants['annealing_steps']
    tau = constants['tau']
    gamma = constants['gamma']
    learning_rate = constants['learning_rate']
    save_path = constants['save_path']
    load_model = constants['load_model']
    n_episodes = constants['n_episodes']
    batch_size = constants['batch_size']
    max_episode_length = constants['max_episode_length']
    n_pretrain_steps = constants['n_pretrain_steps']
    update_freq = constants['update_freq']

    rewards, _ = train(env, n_hidden, start_epsilon, end_epsilon, annealing_steps, tau, gamma, learning_rate,
                       save_path, load_model, n_episodes, batch_size, max_episode_length, n_pretrain_steps, update_freq)

    # Check network learning

    # mean reward over time
    reward_mat = np.resize(np.array(rewards), [len(rewards) // 100, 100])
    mean_reward = np.average(reward_mat, 1)
    plt.plot(mean_reward)
    plt.show()
Exemple #5
0
def run(constant_overwrites):
    tokens_train, tags_train, tokens_val, tags_val, tokens_test, tags_test = load_twitter_entities_dataset()
    special_tokens = ['<UNK>', '<PAD>']
    special_tags = ['O']
    tok2idx, idx2tok = build_dict(tokens_train + tokens_val, special_tokens)
    tag2idx, idx2tag = build_dict(tags_train, special_tags)

    constants = merge_dict(get_constants(), constant_overwrites)
    vocab_size = len(tok2idx.keys())
    n_tags = len(tag2idx.keys())
    pad_idx = tok2idx[PAD_TOKEN]

    print('vocab_size:', vocab_size)
    print('n_tags:', n_tags)
    print('pad_idx:', pad_idx)

    tf.reset_default_graph()
    model = BiLSTMModel(vocab_size, n_tags, constants['embedding_dim'], constants['n_hidden'], pad_idx)

    sess = tf.Session()
    train(model, sess, tokens_train, tags_train, tokens_val, tags_val, tok2idx, tag2idx, idx2tok, idx2tag, constants)

    print('-' * 20 + ' Train set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_train, tags_train, tok2idx, tag2idx, idx2tok, idx2tag)

    print('-' * 20 + ' Validation set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_val, tags_val, tok2idx, tag2idx, idx2tok, idx2tag)

    print('-' * 20 + ' Test set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_test, tags_test, tok2idx, tag2idx, idx2tok, idx2tag)
Exemple #6
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    config = namedtuple('Config', constants.keys())(*constants.values())

    with tf.device('/cpu:0'), tf.Session() as sess:
        try:
            task = importlib.import_module('tasks.%s' % config.task)
        except ImportError:
            print("task '%s' does not have implementation" % config.task)
            raise

        if config.is_train:
            cell, ntm = create_ntm(config, sess)
            task.train(ntm, config, sess)
        else:
            cell, ntm = create_ntm(config, sess, forward_only=True)

        ntm.load(config.checkpoint_dir, config.task)

        if config.task == 'copy':
            task.run(ntm, int(config.test_max_length * 1 / 3), sess)
            print()
            task.run(ntm, int(config.test_max_length * 2 / 3), sess)
            print()
            task.run(ntm, int(config.test_max_length * 3 / 3), sess)
        else:
            task.run(ntm, int(config.test_max_length), sess)
Exemple #7
0
def run(constant_overwrites):
    x_train, y_train, x_val, y_val, x_test, y_test = load_mnist_dataset(
        flatten=True)

    plt.figure(figsize=[6, 6])
    for i in range(4):
        plt.subplot(2, 2, i + 1)
        plt.title('Label: %i' % y_train[i])
        plt.imshow(x_train[i].reshape([28, 28]), cmap='gray')

    plt.pause(1)  # block to show sample image

    constants = merge_dict(get_constants(), constant_overwrites)

    # using estimator does not require labels to be one-hot encoded first
    data = {
        'X_train': x_train,
        'y_train': y_train,
        'X_val': x_val,
        'y_val': y_val,
        'X_test': x_test,
        'y_test': y_test
    }

    metrics = train_using_estimator(data, model_builder, constants)
    print('')
    print('Test accuracy:', metrics['accuracy'])
Exemple #8
0
def run(constant_overwrites):
    allowed_operators = ['+', '-']
    dataset_size = 100000
    data = generate_equations(allowed_operators,
                              dataset_size,
                              min_value=0,
                              max_value=9999)
    train_set, test_set = train_test_split(data,
                                           test_size=0.2,
                                           random_state=42)
    word2id, id2word = get_symbol_to_id_mappings()

    # Special symbols
    start_symbol = '^'  # indicate the beginning of the decoding procedure
    end_symbol = '$'  # indicate the end of a string, both for input and output sequences
    # padding_symbol = '#'  # a padding character to make lengths of all strings equal within one training batch

    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    model = Seq2SeqModel(vocab_size=len(word2id),
                         embeddings_size=constants['embeddings_size'],
                         hidden_size=constants['n_hidden'],
                         max_iter=constants['max_iter'],
                         start_symbol_id=word2id[start_symbol],
                         end_symbol_id=word2id[end_symbol])

    sess = tf.Session()
    all_ground_truth, all_model_predictions, invalid_number_prediction_counts = \
        train(sess, model, train_set, test_set, word2id, id2word, constants)

    evaluate_results(all_ground_truth, all_model_predictions,
                     invalid_number_prediction_counts)
Exemple #9
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    data_generator = ImageDataGenerator(rescale=1/255, rotation_range=90, width_shift_range=0.2,
                                        height_shift_range=0.2, horizontal_flip=True)
    print('Load', constants['module_spec'])
    module_spec = hub.load_module_spec(constants['module_spec'])
    image_size, _ = hub.get_expected_image_size(module_spec)
    # n_channels = hub.get_num_image_channels(module_spec)
    # project_dir = 'tmp/semihard_full_' + 'time:' + str(int(time()))[-3:] +\
    #               '/top:lambda:' + str(constants['lambda_reg']) +\
    #               'margin:' + str(constants['tl_margin'])
    project_dir = '/Users/d777710/src/DeepLearning/vision'
    print('Project dir:', project_dir)
    _, _, bottleneck_config = get_bottleneck_config(os.path.join(project_dir, constants['bottleneck_dir']),
                                                    os.path.join(project_dir, constants['splits_dir']))
    bottleneck_flow_gen = ImageFlowGenerator(bottleneck_config, mode='bottleneck')
    constants.update({
        'train_dir': os.path.join(project_dir, constants['train_subdir']),
        'top_model_dir': os.path.join(project_dir, constants['top_model_subdir']),
        'val_dir': os.path.join(project_dir, constants['val_subdir']),
        'top_model_val_dir': os.path.join(project_dir, constants['top_model_val_subdir']),
        'data_flow_gen': bottleneck_flow_gen,
        'eval_every_n_steps': 5,
        'generator': data_generator,
        'image_size': image_size
    })
    model = SemiHardModel(constants, train_top_only=True)
    run_training(model, constants)
Exemple #10
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    learning_rate = constants['learning_rate']
    n_episodes = constants['n_episodes']
    epsilon = constants['epsilon']

    tf.reset_default_graph()

    bandit = ContextualBandit()
    agent = Agent(learning_rate=learning_rate,
                  state_dim=bandit.n_bandits,
                  n_actions=bandit.n_actions)
    w = tf.trainable_variables()[0]

    sess = tf.Session()

    _, w1 = train(bandit,
                  agent,
                  w,
                  sess,
                  n_episodes=n_episodes,
                  epsilon=epsilon)

    for i in range(bandit.n_bandits):
        print(
            'The agent thinks action %s for bandit %i is the most promising' %
            (str(np.argmax(w1[i]) + 1), i + 1))

        if np.argmax(w1[i]) == np.argmin(bandit.bandits[i]):
            print('and it is right!')
        else:
            print('and it is wrong!')

        print('')
Exemple #11
0
def run(constant_overwrites):
    x_train, y_train, x_val, y_val, x_test, y_test = load_mnist_dataset(
        flatten=True)

    plt.figure(figsize=[6, 6])
    for i in range(4):
        plt.subplot(2, 2, i + 1)
        plt.title('Label: %i' % y_train[i])
        plt.imshow(x_train[i].reshape([28, 28]), cmap='gray')

    constants = merge_dict(get_constants(), constant_overwrites)
    network = network_builder(x_train, constants)

    train_log = []
    val_log = []
    for epoch in range(constants['n_epochs']):
        for x_batch, y_batch in iterate_minibatches(
                x_train, y_train, batch_size=constants['batch_size'],
                shuffle=True):
            train(network, x_batch, y_batch)

        train_log.append(np.mean(predict(network, x_train) == y_train))
        val_log.append(np.mean(predict(network, x_val) == y_val))

        clear_output()
        print('Epoch', epoch)
        print('Train accuracy:', train_log[-1])
        print('Val accuracy:', val_log[-1])
        if len(train_log) > 1:
            plt.figure()
            plt.plot(train_log, label='train accuracy')
            plt.plot(val_log, label='val accuracy')
            plt.legend(loc='best')
            plt.grid()
            plt.show()
Exemple #12
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    x_train, y_train, x_val, y_val, classes = load_datasets()
    model = LogisticRegressionModel(x_train, y_train)

    def print_prediction(sample_idx):
        utterance = x_val[sample_idx]
        print('Utterance:', utterance)
        print('Actual:', classes[y_val[sample_idx]])
        print('Predicted:', classes[model.predict(utterance)])

    print('\nSample predictions:')
    samples = np.random.choice(x_val.index, size=constants['n_samples'])
    for i in samples:
        print_prediction(i)
        print()

    y_pred = [model.predict(utterance) for utterance in x_val]

    train_df = pd.DataFrame({'utterance': x_train, 'label': y_train})
    counts_by_label = train_df.groupby('label').utterance.count()

    stats = perf_by_label(y_val, y_pred, classes, counts_by_label)
    print('\nBest / Worst classes:')
    print_best_worst(stats,
                     rounded=2,
                     sort_column='f1_score',
                     top_n=5,
                     max_name_len=40)
Exemple #13
0
def image_retrieval(constant_overwrites):
    img_shape, attr, x_train, x_test = load_faces_dataset()
    constants = merge_dict(get_constants(), constant_overwrites)
    constants['img_shape'] = img_shape
    encoder_filename = constants['encoder_filename']
    decoder_filename = constants['decoder_filename']
    reset_tf_session()
    autoencoder, encoder, decoder = model_builder(network_builder, constants)
    if os.path.exists(encoder_filename) and not constants['retrain']:
        encoder.load_weights(encoder_filename)
    else:
        data = {'X_train': x_train, 'X_test': x_test}
        train(autoencoder, data, constants)
        encoder.save_weights(encoder_filename)
        decoder.save_weights(decoder_filename)

    images = x_train
    codes = encoder.predict(images)
    assert len(codes) == len(images)
    nei_clf = NearestNeighbors(metric="euclidean")
    nei_clf.fit(codes)

    # Cherry-picked examples:

    # smiles
    show_similar(x_test[247], nei_clf, encoder, images)

    # ethnicity
    show_similar(x_test[56], nei_clf, encoder, images)

    # glasses
    show_similar(x_test[63], nei_clf, encoder, images)
Exemple #14
0
def run(constant_overwrites):
    x_train, y_train, x_test, y_test = load_cifar10_dataset()
    # n_classes = 10
    classes = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]

    print('')
    print('original:')
    print('X_train shape:', x_train.shape)
    print('y_train shape:', y_train.shape)
    print(y_train[:3])

    # not required when using `tfms_from_model` below
    # x_train = x_train.reshape(x_train.shape[0], -1)
    x_train = x_train.astype(
        np.float32)  # WTF? float/float64 (default) raises error
    y_train = np.where(y_train == 1)[1]
    y_train = y_train.astype(np.int)  # uint8 is causing error
    # x_test = x_test.reshape(x_test.shape[0], -1)
    x_test = x_test.astype(np.float32)  # as above
    y_test = np.where(y_test == 1)[1]
    y_test = y_test.astype(np.int)  # as above

    # sample to test on CPU
    x_train = x_train[:800]
    y_train = y_train[:800]
    x_test = x_test[:200]
    y_test = y_test[:200]

    print('')
    print('reshaped:')
    print('X_train shape:', x_train.shape)
    print('y_train shape:', y_train.shape)
    print(y_train[:3])
    print('X_train dtype:', x_train.dtype)
    print('y_train dtype:', y_train.dtype)

    constants = merge_dict(get_constants(), constant_overwrites)
    constants['img_shape'] = x_train.shape[1:]

    arch = resnet34
    data = ImageClassifierData.from_arrays(OUTPUT_DIR,
                                           trn=(x_train, y_train),
                                           val=(x_test, y_test),
                                           classes=classes,
                                           tfms=tfms_from_model(
                                               arch, constants['image_size']))
    learn = ConvLearner.pretrained(arch, data, precompute=True)
    # lrf = learn.lr_find()
    learn.fit(constants['learning_rate'], constants['n_epochs'])

    learn.sched.plot_loss()

    log_preds = learn.predict()
    preds = np.argmax(log_preds, axis=1)

    print('Finished')
Exemple #15
0
def run(constant_overwrites):
    constants = merge_dict(get_constants(), constant_overwrites)
    tar_filename = constants['tar_filename']
    img_size = constants['img_size']

    train_files, test_files, train_labels, test_labels, n_classes = \
        load_flowers(os.path.dirname(os.path.abspath(__file__)))
    data = {
        'train_files': train_files,
        'train_labels': train_labels,
        'test_files': test_files,
        'test_labels': test_labels,
        'n_classes': n_classes
    }

    # test cropping
    raw_bytes = read_raw_from_tar(tar_filename, 'jpg/image_00001.jpg')
    img = decode_image_from_raw_bytes(raw_bytes)

    print('')
    print('original image shape:', img.shape)
    print('')
    plt.imshow(img)
    plt.show()

    img = prepare_raw_bytes_for_model(raw_bytes, img_size, normalize_for_model=False)
    print('')
    print('cropped image shape:', img.shape)
    print('')
    plt.imshow(img)
    plt.show()

    # remember to clear session if you start building graph from scratch!
    # don't call K.set_learning_phase() !!! (otherwise will enable dropout
    # in train/test simultaneously)
    _ = reset_tf_session()  # returns session

    model = model_builder(n_classes, constants)

    print('')
    print(model.summary())
    print('')

    compile_model(model, constants)

    # model_file_exists = any(f.startswith('flowers') for f in os.listdir('.') if os.path.isfile(f))
    last_finished_epoch = constants['last_finished_epoch']
    if last_finished_epoch:
        model = load_model(constants['model_filename'].format(last_finished_epoch))

    train(model, data, constants)

    # Accuracy on test set
    test_accuracy = model.evaluate_generator(
        train_generator(tar_filename, test_files, test_labels, n_classes, constants),
        len(test_files) // constants['batch_size'] // 2
    )[1]

    print('\nTest accuracy: %.5f' % test_accuracy)
Exemple #16
0
def run(constant_overwrites):
    constants = merge_dict(get_constants(), constant_overwrites)
    data, all_tags = load_tagged_sentences()

    draw(data[11])
    draw(data[10])
    draw(data[7])

    all_words, word_counts = get_word_counts(data)

    # let's measure what fraction of data words are in the dictionary
    coverage = float(sum(word_counts[w]
                         for w in all_words)) / sum(word_counts.values())
    print('Coverage = %.5f' % coverage)

    # Build a mapping from tokens to integer ids. Our model operates on a word level,
    # processing one word per RNN step. This means we'll have to deal with far larger
    # vocabulary.
    # Luckily for us, we only receive those words as input, i.e. we don't have to
    # predict them. This means we can have a large vocabulary for free by using word
    # embeddings.
    word_to_id = defaultdict(lambda: 1,
                             {word: i
                              for i, word in enumerate(all_words)})
    tag_to_id = {tag: i for i, tag in enumerate(all_tags)}

    batch_words, batch_tags = zip(*[zip(*sentence) for sentence in data[-3:]])

    print('word ids:')
    print(to_matrix(batch_words, word_to_id))
    print('tag ids:')
    print(to_matrix(batch_tags, tag_to_id))

    train_data, test_data = train_test_split(data,
                                             test_size=0.25,
                                             random_state=42)

    if constants['bidirectional']:
        model = bidirectional_model_builder(all_words, all_tags, constants)
    else:
        model = model_builder(all_words, all_tags, constants)

    print('')
    print(model.summary())
    print('')

    train(model, train_data, test_data, all_tags, word_to_id, tag_to_id,
          constants)

    # Measure final accuracy on the whole test set.
    acc = compute_test_accuracy(model, test_data, word_to_id, tag_to_id)
    print('\n\nFinal accuracy: %.5f' % acc)

    assert acc > 0.94, 'Accuracy should be better than that'
Exemple #17
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), '../src',
                               'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    build_dataset_splits(images_dir=constants['images_dir'],
                         splits_dir=constants['splits_dir'],
                         external_dir=constants['images_dir'],
                         min_test=constants['min_test'],
                         val_ratio=constants['val_ratio'],
                         test_ratio=constants['test_ratio'],
                         rebuild=constants['rebuild'])
Exemple #18
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    env = gym.make('Taxi-v2')
    n_actions = env.action_space.n
    agent = QLearningAgent(alpha=constants['alpha'],
                           epsilon=constants['epsilon'],
                           discount=constants['discount'],
                           get_legal_actions=lambda s: range(n_actions))

    train(env, agent, constants['n_epochs'])
Exemple #19
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    train_df, val_df, test_df, classes = load_data(
        dirname=constants['data_dir'])
    train_df = remove_classes_with_too_few_examples(clean_data(train_df))
    val_df = remove_classes_with_too_few_examples(clean_data(val_df))
    n_classes = len(classes)
    batch_size = constants['batch_size']
    allow_soft_placement = constants['allow_soft_placement']
    log_device_placement = constants['log_device_placement']
    if constants['test']:
        print('\nTesting...')
        x_raw = val_df.utterance.values
        checkpoint_dir = constants['checkpoint_dir']
        vocab_path = os.path.join(checkpoint_dir, '..', 'vocab')
        vocab_processor = learn.preprocessing.VocabularyProcessor.restore(
            vocab_path)
        x_test = np.array(list(vocab_processor.transform(x_raw)))
        # y_test = one_hot_encode(val_df.label.values, n_classes)
        y_test = val_df.label.values
        preds = test(x_test, batch_size, checkpoint_dir, allow_soft_placement,
                     log_device_placement, y_test)
        save_eval_to_csv(x_raw, preds, checkpoint_dir)
    else:
        print('\nTraining...')
        x_train, y_train, x_val, y_val, vocab_processor = preprocess(
            train_df, val_df, n_classes)
        # model = TextCNN(seq_len=x_train.shape[1], n_classes=y_train.shape[1],
        #                 vocab_size=len(vocab_processor.vocabulary_),
        #                 embed_size=constants['embed_size'],
        #                 filter_sizes=constants['filter_sizes'],
        #                 n_filters=constants['n_filters'],
        #                 l2_reg_lambda=constants['l2_reg_lambda'])
        train(x_train,
              y_train,
              x_val,
              y_val,
              vocab_processor,
              model=None,
              learning_rate=constants['learning_rate'],
              n_checkpoints=constants['n_checkpoints'],
              keep_prob=constants['keep_prob'],
              batch_size=batch_size,
              n_epochs=constants['n_epochs'],
              evaluate_every=constants['evaluate_every'],
              checkpoint_every=constants['checkpoint_every'],
              allow_soft_placement=allow_soft_placement,
              log_device_placement=log_device_placement,
              constants=constants)
Exemple #20
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    train_df, val_df, test_df, classes = load_data()
    train_df = remove_classes_with_too_few_examples(clean_data(train_df))
    val_df = remove_classes_with_too_few_examples(clean_data(val_df))
    features, labels, tfidf, _ = generate_tfidf_features(
        train_df,
        val_df,
        cutoff=constants['cutoff'],
        ngram_range=constants['ngram_range'])
    print('Number Utterances: {}, Features: {}'.format(*features.shape))
    show_relevant_terms(features, labels, tfidf, classes, every=20)
Exemple #21
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    if constants['play']:
        if 'folder' in constants:
            folder = constants['folder']
            with open(folder + '/hyperparams.json') as f:
                constants = merge_dict(constants, json.load(f))

            # check for slash at end
            experiment = folder if folder[-1] == '/' else folder + '/'
            constants['experiment'] = experiment

            if 'traffic_folder' in constants:
                if constants['traffic'] == 'dir:':
                    constants['traffic'] += constants['traffic_folder']

            play(constants, is_training=False)
        else:
            print('Folder must be specific to play')
    else:
        constants['experiment'] = setup_experiment()
        play(constants, is_training=True)
Exemple #22
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    env = WithSnapshots(gym.make('CartPole-v0'))
    root_observation = env.reset()
    root_snapshot = env.get_snapshot()
    n_actions = env.action_space.n
    root = Root(env, n_actions, root_snapshot, root_observation)

    plan_mcts(root, n_iters=constants['n_iters'])

    test_env = loads(root_snapshot)

    train(root, test_env, show=False)
Exemple #23
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    env = make_env()
    obs_shape = env.observation_space.shape
    n_actions = env.action_space.n

    print('\nPrint game info:')
    print('observation shape:', obs_shape)
    print('n_actions:', n_actions)
    print('action names:', env.env.env.get_action_meanings())

    # Print game images:
    s = env.reset()
    for _ in range(100):
        s, _, _, _ = env.step(env.action_space.sample())

    plt.title('Game image')
    plt.imshow(env.render('rgb_array'))
    plt.show()

    plt.title('Agent observation (4-frame buffer')
    plt.imshow(s.transpose([0, 2, 1]).reshape([42, -1]))
    plt.show()

    tf.reset_default_graph()
    sess = tf.Session()
    agent = Agent('agent', obs_shape, n_actions)
    sess.run(tf.global_variables_initializer())

    env_monitor = Monitor(env, directory='videos', force=True)
    game_rewards = evaluate(agent, env, sess, n_games=constants['n_sessions'])
    env_monitor.close()
    print('Game rewards:', game_rewards)

    # Train on parallel games - test
    env_batch = EnvBatch(10)
    batch_states = env_batch.reset()
    batch_actions = sample_actions(agent.step(sess, batch_states))
    batch_next_states, batch_rewards, batch_done, _ = env_batch.step(
        batch_actions)

    print('State shape:', batch_states.shape)
    print('Actions:', batch_actions[:3])
    print('Rewards:', batch_rewards[:3])
    print('Done:', batch_done[:3])

    # Train for real
    model = ActorCritic(obs_shape, n_actions, agent)
    train(agent, model, env, sess)
Exemple #24
0
def run(constant_overwrites):
    # if using a GPU
    # os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    # # os.environ['CUDA_VISIBLE_DEVICES'] = ''
    # os.environ['CUDA_VISIBLE_DEVICES'] = '1'

    constants = merge_dict(get_constants(), constant_overwrites)
    file_path = load_quickdraw_dataset('apple', DATA_DIR)
    data = np.load(
        file_path
    )  # 28x28 grayscale bitmap in numpy format, images are centered

    print('')
    print('data.shape:', data.shape)

    # normalize and reshape
    data = data / 255
    data = np.reshape(data, (data.shape[0], 28, 28, 1))
    img_w, img_h = data.shape[1:3]

    # Sample image
    random_idx = np.random.randint(data.shape[0])
    plt.imshow(data[random_idx, :, :, 0], cmap='Greys')
    plt.show()

    generator = generator_builder(constants)
    discriminator = get_discriminator(img_w, img_h, constants)
    adversarial_model = adversarial_builder(generator, discriminator,
                                            constants)

    a_metrics_complete, d_metrics_complete = train(generator, discriminator,
                                                   adversarial_model, data,
                                                   constants)

    df0 = pd.DataFrame({
        'Generative Loss': [metric[0] for metric in a_metrics_complete],
        'Discriminative Loss': [metric[0] for metric in d_metrics_complete],
    })
    ax0 = df0.plot(title='Training Loss', logy=True)
    ax0.set_xlabel('Epochs')
    ax0.set_ylabel('Loss')

    df1 = pd.DataFrame({
        'Generative Loss': [metric[1] for metric in a_metrics_complete],
        'Discriminative Loss': [metric[1] for metric in d_metrics_complete],
    })
    ax1 = df1.plot(title='Training Accuracy')
    ax1.set_xlabel('Epochs')
    ax1.set_ylabel('Loss')
Exemple #25
0
def run(constant_overwrites):
    # nltk.download('stopwords')
    constants = merge_dict(get_constants(), constant_overwrites)
    x_train, y_train, x_val, y_val, x_test = load_stack_overflow_dataset()
    x_train = [clean_text(text) for text in x_train]
    x_val = [clean_text(text) for text in x_val]
    x_test = [clean_text(text) for text in x_test]
    dict_size = constants['dict_size']
    train_and_test(x_train,
                   y_train,
                   x_val,
                   y_val,
                   x_test,
                   dict_size,
                   print_metrics=True)
Exemple #26
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    env = gym.make('CartPole-v0')
    env.reset()
    n_actions = env.action_space.n
    state_dim = env.observation_space.shape

    sess = tf.Session()
    agent = PolicyEstimator(state_dim, n_actions)
    train(env, agent, n_actions, sess, constants['n_epochs'],
          constants['n_iters'])

    record_sessions(env.spec.id, agent, n_actions, sess)

    env.close()
Exemple #27
0
def run(constant_overwrites):
    constants = merge_dict(get_constants(), constant_overwrites)
    dataset = constants['dataset']
    if dataset == 'twitter':
        print('Loading twitter dataset')
        tokens_train, tags_train, tokens_val, tags_val, tokens_test, tags_test = load_twitter_entities_dataset()
    elif dataset == 'nvd':
        print('Loading NVD dataset')
        tokens_train, tags_train, tokens_val, tags_val, tokens_test, tags_test, _, _, _ = load_nvd_corpus()
    else:
        print('ERR - invalid dataset')
        return

    special_tokens = ['<UNK>', '<PAD>']
    special_tags = ['O']
    tok2idx, idx2tok = build_dict(tokens_train + tokens_val, special_tokens)
    tag2idx, idx2tag = build_dict(tags_train, special_tags)

    vocab_size = len(tok2idx.keys())
    n_tags = len(tag2idx.keys())
    embedding_dim = constants['embedding_dim']
    n_hidden = constants['n_hidden']

    print('')
    print('Hyperparameters')
    print('---------------')
    print('vocab_size:', vocab_size)
    print('n_tags:', n_tags)
    print('embedding_dim:', embedding_dim)
    print('n_hidden:', n_hidden)

    tf.reset_default_graph()
    model = BiLSTMCRFModel(vocab_size, n_tags, embedding_dim, n_hidden)

    sess = tf.Session()
    train(model, sess, tokens_train, tags_train, tokens_val, tags_val, tok2idx, tag2idx, idx2tok, idx2tag, constants)

    print('-' * 20 + ' Train set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_train, tags_train, tok2idx, tag2idx, idx2tok, idx2tag)

    print('-' * 20 + ' Validation set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_val, tags_val, tok2idx, tag2idx, idx2tok, idx2tag)

    print('-' * 20 + ' Test set quality: ' + '-' * 20)
    eval_conll(model, sess, tokens_test, tags_test, tok2idx, tag2idx, idx2tok, idx2tag)
Exemple #28
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)

    env = gym.make('FrozenLake-v0')

    # initialize table with all zeros
    q_table = np.zeros([env.observation_space.n, env.action_space.n])

    learning_rate = constants['learning_rate']
    gamma = constants['gamma']
    n_episodes = constants['n_episodes']

    rewards = train(env, q_table, n_episodes, learning_rate, gamma)

    print('Score over time:', str(sum(rewards) / n_episodes))
    print('Final Q-Table values:')
    print(q_table)
Exemple #29
0
def run(constant_overwrites):
    config_path = os.path.join(os.path.dirname(__file__), 'hyperparams.yml')
    constants = merge_dict(load_hyperparams(config_path), constant_overwrites)
    if constants['train']:
        logging.info('Training the model...')
        x, y, vocab, vocab_inv, df, labels = load_crime_dataset()
        train(x, y, vocab, vocab_inv, labels, constants)
    else:
        logging.info('Making predictions...')
        trained_dir = constants['trained_dir']
        if not trained_dir.endswith('/'):
            trained_dir += '/'

        params, words_index, labels, embedding_mat = load_trained_params(
            trained_dir)
        x, y, df = load_crime_test_dataset(labels)
        predict(x, y, df, params, words_index, labels, embedding_mat,
                trained_dir)
Exemple #30
0
def run(constant_overwrites):
    tokens_train, tags_train, tokens_val, tags_val, tokens_test, tags_test = load_twitter_entities_dataset()
    sents_train = build_sentences(tokens_train, tags_train)
    sents_val = build_sentences(tokens_val, tags_val)
    sents_test = build_sentences(tokens_test, tags_test)
    x_train = [sentence_features(sent) for sent in sents_train]
    y_train = [sentence_labels(sent) for sent in sents_train]
    x_val = [sentence_features(sent) for sent in sents_val]
    y_val = [sentence_labels(sent) for sent in sents_val]
    x_test = [sentence_features(sent) for sent in sents_test]
    y_test = [sentence_labels(sent) for sent in sents_test]
    constants = merge_dict(get_constants(), constant_overwrites)
    algorithm = constants['algorithm']
    c1 = constants['c1']
    c2 = constants['c2']
    max_iterations = constants['max_iterations']
    all_possible_transitions = constants['all_possible_transitions']

    print('')
    print('Hyperparameters')
    print('---------------')
    print('algorithm:', algorithm)
    print('c1:', c1)
    print('c2:', c2)
    print('max_iterations:', max_iterations)
    print('all_possible_transitions:', all_possible_transitions)

    # Build the CRF Model
    model = CRF(algorithm=algorithm,
                c1=c1,
                c2=c2,
                max_iterations=max_iterations,
                all_possible_transitions=all_possible_transitions)

    model.fit(x_train, y_train)

    print('-' * 20 + ' Train set quality: ' + '-' * 20)
    evaluate(model, x_train, y_train, short_report=False)

    print('-' * 20 + ' Validation set quality: ' + '-' * 20)
    evaluate(model, x_val, y_val, short_report=False)

    print('-' * 20 + ' Test set quality: ' + '-' * 20)
    evaluate(model, x_test, y_test, short_report=False)