Exemple #1
0
    def __init__(self, data_loader, num_epochs, chckpnt_dir, weights_path,
                 summary_write_freq, model_save_freq):
        self.data_loader = data_loader
        self.num_epochs = num_epochs
        self.chckpnt_dir = chckpnt_dir
        self.weights_path = weights_path
        self.summary_write_freq = summary_write_freq
        self.model_save_freq = model_save_freq
        self.num_train = data_loader.get_num_train()

        self.input, self.label, self.loss, self.outputs = model.build_graph()
        self.train_op = tf.train.AdamOptimizer(1e-3).minimize(self.loss)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # This is needed otherwise, tensorflow assigns the entire GPU memory for one process

        self.saver = tf.train.Saver()

        self.sess = tf.InteractiveSession(config=config)
        self.summary_writer = tf.summary.FileWriter(self.chckpnt_dir,
                                                    self.sess.graph)

        self.sess.run(tf.global_variables_initializer())

        self.load_weights(self.weights_path)  # Load weights from VGG model
        ckpt = tf.train.get_checkpoint_state(
            self.chckpnt_dir)  # Continue from previous checkpoint
        if (ckpt and ckpt.model_checkpoint_path):
            saver.restore(sess.ckpt.model_checkpoint_path)
Exemple #2
0
def train():
    with tf.Session() as sess:
        # initialization
        graph = build_graph()
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())

        # multi thread
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        # log writer
        train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
        test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/val')

        # restore model
        if FLAGS.restore:
            ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
            if ckpt:
                saver.restore(sess, ckpt)
                print("restore from the checkpoint {}".format(ckpt))

        # training begins
        try:
            while not coord.should_stop():
                for step, (x_batch, y_batch) in enumerate(train_data_iterator()):
                    start_time = time.time()
                    feed_dict = {graph['images']: x_batch,
                                 graph['labels']: y_batch,
                                 graph['keep_prob']: 0.8,
                                 graph['is_training']: True}
                    train_opts = [graph['train_op'], graph['loss'], graph['merged_summary_op']]
                    _, loss_val, train_summary = sess.run(train_opts, feed_dict=feed_dict)

                    train_writer.add_summary(train_summary, step)
                    end_time = time.time()
                    print("the step {0} takes {1} loss {2}".format(step, end_time - start_time, loss_val))

                    # eval stage
                    if step % FLAGS.eval_steps == 0:
                        x_batch_test, y_batch_test = test_data_helper(128)
                        feed_dict = {graph['images']: x_batch_test,
                                     graph['labels']: y_batch_test,
                                     graph['keep_prob']: 1.0,
                                     graph['is_training']: False}
                        test_opts = [graph['accuracy'], graph['merged_summary_op']]
                        accuracy_test, test_summary = sess.run(test_opts, feed_dict=feed_dict)
                        test_writer.add_summary(test_summary, step)
                        print('===============Eval a batch=======================')
                        print('the step {0} test accuracy: {1}'.format(step, accuracy_test))
                        print('===============Eval a batch=======================')
                    # save stage
                    if step % FLAGS.save_steps == 0 and step > FLAGS.min_save_steps:
                        saver.save(sess, os.path.join(FLAGS.checkpoint_dir, FLAGS.model_name), global_step=step)
        except tf.errors.OutOfRangeError:
            print('==================Train Finished================')
            saver.save(sess, os.path.join(FLAGS.checkpoint_dir, FLAGS.model_name), global_step=step)
        finally:
            coord.request_stop()
        coord.join(threads)
Exemple #3
0
def main():
    images, labels = dataset.load_test_images()
    num_scatter = len(images)
    _images, _, label_id = dataset.sample_labeled_data(images, labels,
                                                       num_scatter)
    with tf.device(config.device):
        t = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
        z, _x = sess.run([t.z_r, t.x_r], feed_dict={t.x: _images})

        plot.scatter_labeled_z(z, label_id, dir=config.ckpt_dir)
        plot.tile_images(_x[:100], dir=config.ckpt_dir)
        plot.plot_loss_tendency(config.ckpt_dir)

    hist_value, hist_head = plot.load_pickle_to_data(config.ckpt_dir)
    for loss_name in ['reconstruction']:
        plot.plot_loss_trace(hist_value[loss_name], loss_name, config.ckpt_dir)

    plot.plot_adversarial_trace(hist_value['discriminator'],
                                hist_value['generator'], 'z', config.ckpt_dir)
    plot.plot_adversarial_trace(hist_value['discriminator_z'],
                                hist_value['generator_z'], 'z',
                                config.ckpt_dir)
    plot.plot_adversarial_trace(hist_value['discriminator_img'],
                                hist_value['generator_img'], 'z',
                                config.ckpt_dir)
Exemple #4
0
def main():
    # load MNIST images
    images, labels = dataset.load_test_images()

    # Settings
    num_scatter = len(images)
    _images, _, label_id = dataset.sample_labeled_data(images, labels,
                                                       num_scatter)

    with tf.device(config.device):
        t = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
        representation, x_reconstruction = sess.run([t.yz, t.x_r],
                                                    feed_dict={t.x: _images})
        plot.scatter_labeled_z(representation, label_id, dir=config.ckpt_dir)
        plot.tile_images(x_reconstruction[:100], dir=config.ckpt_dir)

        # z distributed plot
        num_segments = 20
        limit = (-5, 5)
        x_values = np.linspace(limit[0], limit[1], num_segments)
        y_values = np.linspace(limit[0], limit[1], num_segments)
        vacant = np.zeros((28 * num_segments, 28 * num_segments))
        for i, x_element in enumerate(x_values):
            for j, y_element in enumerate(y_values):
                x_reconstruction = sess.run(
                    t.x_r,
                    feed_dict={
                        t.yz: np.reshape([x_element, y_element], [1, 2])
                    })
                vacant[(num_segments - 1 - i) * 28:(num_segments - i) * 28,
                       j * 28:(j + 1) * 28] = x_reconstruction.reshape(28, 28)

        vacant = (vacant + 1) / 2
        pylab.figure(figsize=(10, 10), dpi=400, facecolor='white')
        pylab.imshow(vacant, cmap='gray', origin='upper')
        pylab.tight_layout()
        pylab.axis('off')
        pylab.savefig("{}/clusters.png".format(config.ckpt_dir))

        # loss part
        hist_value, hist_head = plot.load_pickle_to_data(config.ckpt_dir)
        for loss_name in ['reconstruction', 'supervised']:
            plot.plot_loss_trace(hist_value[loss_name], loss_name,
                                 config.ckpt_dir)

        plot.plot_adversarial_trace(hist_value['discriminator_y'],
                                    hist_value['generator_y'], 'y',
                                    config.ckpt_dir)
        plot.plot_adversarial_trace(hist_value['discriminator_z'],
                                    hist_value['generator_z'], 'z',
                                    config.ckpt_dir)
        plot.plot_adversarial_trace(hist_value['validation_accuracy'],
                                    hist_value['transform'],
                                    'validation_accuracy', config.ckpt_dir)
Exemple #5
0
def build_train_model(hparams, scope="train"):
    """Builds a training Seq2Seq model
        Args:
            hparams: a HParams object
            scope: scope of train model

        Returns:
            model: a NTModel tuple, representing a handle to the model
    """
    src_lang = hparams.src_lang
    src_vocab_file_name = hparams.src_vocab_file_name
    tgt_lang = hparams.tgt_lang
    tgt_vocab_file_name = hparams.tgt_vocab_file_name

    tf.reset_default_graph()

    train_graph = tf.Graph()
    with train_graph.as_default() as g:
        with tf.container(scope):
            src_vocab, tgt_vocab = load_vocabs(src_lang, src_vocab_file_name,
                                               tgt_lang, tgt_vocab_file_name)
            src_dataset_file_name = tf.placeholder(
                tf.string, name="src_dataset_file_name")
            tgt_dataset_file_name = tf.placeholder(
                tf.string, name="tgt_dataset_file_name")

            src_dataset = tf.data.TextLineDataset(src_dataset_file_name)
            tgt_dataset = tf.data.TextLineDataset(tgt_dataset_file_name)

            batch_size = tf.placeholder(tf.int64, name="batch_size")

            # maximum sequence length for training example
            max_len = tf.placeholder(tf.int64, name="max_len")

            iterator = Iterator(src_dataset,
                                src_vocab,
                                tgt_dataset,
                                tgt_vocab,
                                batch_size=batch_size,
                                max_len=max_len)

            # actual TensorFlow Dataset Iterator
            iterator_tf = iterator.create_iterator()

            model_class = _get_model_from_str_type(hparams.model_name)

            model = model_class(hparams, src_vocab, tgt_vocab)

            model_graph = model.build_graph(iterator_tf,
                                            tf.contrib.learn.ModeKeys.TRAIN,
                                            batch_size, g)

            return NTModel(src_vocab=src_vocab,
                           tgt_vocab=tgt_vocab,
                           iterator_tf=iterator_tf,
                           model_graph=model_graph,
                           model=model,
                           hparams=hparams,
                           mode=tf.contrib.learn.ModeKeys.TRAIN)
def test():
    """ Testing a pretrained network """

    # First create our Tensorflow graph.
    # Start by setting some of our hyperparamters.
    num_dimensions = 300

    # Load in data structures
    wordsList = data.wordsList  # encode words as UTF-8 (necessary?)
    wordVectors = data.wordVectors

    model.build_graph()

    saver = tf.train().Saver()
    with tf.InteractiveSession as sess:
        saver.restore(sess, tf.train().latest_checkpoint('models'))

        max_length = util.MAX_SEQ_LENGTH
        print('Write a movie review to analyse. Enter to exit. Max length is ', max_length)
        while True:
            line = _get_user_input()
            if len(line) > 0 and line[-1] == '\n':
                continue
            if line == '':
                break

            """
                Before we input our own text, let's first define a couple of functions.
                The first is a function to make sure the sentence is in the proper format,
                and the second is a function that obtains the word vectors for each of the words
                in a given sentence.
            """

            # Get token-ids for the input sentence
            input_matrix = get_sentence_matrix(line)
            if len(input_matrix) > max_length:
                print('Max length I can handle is:', max_length)
                line = _get_user_input()
                continue

            predicted_sentiment = sess.run(model.prediction, {model.input_data: input_matrix})

            if predicted_sentiment[0] > predicted_sentiment[1]:
                print("Positive statement")
            else:
                print("Negative statement")
Exemple #7
0
def main():
    # load MNIST images
    images, labels = dataset.load_test_images()

    # Settings
    num_anologies = 10
    pylab.gray()

    # generate style vector z
    x = dataset.sample_unlabeled_data(images, num_anologies)
    x = (x + 1) / 2

    with tf.device(config.device):
        x_input, img_y, img_z, reconstruction = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
        z = sess.run(img_z, feed_dict={x_input: x})

        for m in range(num_anologies):
            pylab.subplot(num_anologies, config.ndim_y + 2, m * 12 + 1)
            pylab.imshow(x[m].reshape((28, 28)), interpolation='none')
            pylab.axis('off')

        all_y = np.identity(config.ndim_y, dtype=np.float32)
        for m in range(num_anologies):
            fixed_z = np.repeat(z[m].reshape(1, -1), config.ndim_y, axis=0)
            gen_x = sess.run(reconstruction,
                             feed_dict={
                                 img_z: fixed_z,
                                 img_y: all_y
                             })
            gen_x = (gen_x + 1) / 2

            for n in range(config.ndim_y):
                pylab.subplot(num_anologies, config.ndim_y + 2, m * 12 + 3 + n)
                pylab.imshow(gen_x[n].reshape((28, 28)), interpolation='none')
                pylab.axis('off')

        fig = pylab.gcf()
        fig.set_size_inches(num_anologies, config.ndim_y)
        pylab.savefig('{}/analogy.png'.format(config.ckpt_dir))

        hist_value, hist_head = plot.load_pickle_to_data(config.ckpt_dir)
        for loss_name in [
                'reconstruction', 'validation_accuracy', 'supervised'
        ]:
            plot.plot_loss_trace(hist_value[loss_name], loss_name,
                                 config.ckpt_dir)

        plot.plot_adversarial_trace(hist_value['discriminator_y'],
                                    hist_value['generator_y'], 'y',
                                    config.ckpt_dir)
        plot.plot_adversarial_trace(hist_value['discriminator_z'],
                                    hist_value['generator_z'], 'z',
                                    config.ckpt_dir)
Exemple #8
0
def train():
    trainset = sys.argv[1]
    inputs = melt.read_sparse.inputs
    X, y = inputs(trainset,
                  decode=decode,
                  batch_size=FLAGS.batch_size,
                  num_epochs=FLAGS.num_epochs,
                  num_preprocess_threads=FLAGS.num_preprocess_threads,
                  batch_join=FLAGS.batch_join,
                  shuffle=FLAGS.shuffle)

    train_with_validation = len(
        sys.argv) > 2 and not sys.argv[2].startswith('-')
    print('train_with_validation', train_with_validation)
    if train_with_validation:
        validset = sys.argv[2]
        eval_X, eval_y = inputs(
            validset,
            decode=decode,
            batch_size=FLAGS.batch_size * 10,
            num_preprocess_threads=FLAGS.num_preprocess_threads,
            batch_join=FLAGS.batch_join,
            shuffle=FLAGS.shuffle)

    loss, accuracy = model.build_graph(X, y)
    train_op = melt.gen_train_op(loss, FLAGS.learning_rate)
    if train_with_validation:
        tf.get_variable_scope().reuse_variables()
        eval_loss, eval_accuracy = model.build_graph(eval_X, eval_y)
        tf.scalar_summary('loss_eval', eval_loss)
        eval_ops = [eval_loss, eval_accuracy]
    else:
        eval_ops = None

    train_flow(
        [train_op, loss, accuracy],
        deal_results=melt.show_precision_at_k,
        #deal_results=None,
        eval_ops=eval_ops,
        deal_eval_results=lambda x: melt.print_results(x,
                                                       names=['precision@1']),
        print_avg_loss=True,
        eval_interval_steps=FLAGS.eval_interval_steps)
Exemple #9
0
def train():
  assert FLAGS.num_classes > 0 and FLAGS.num_features > 0, 'you must pass num_classes and num_features according to your data'
  print('num_features:', FLAGS.num_features, 'num_classes:', FLAGS.num_classes)
  model.set_input_info(num_features=FLAGS.num_features, num_classes=FLAGS.num_classes)

  trainset = sys.argv[1]
  inputs = melt.shuffle_then_decode.inputs
  X, y = inputs(
    trainset, 
    decode=decode,
    batch_size=FLAGS.batch_size,
    num_epochs=FLAGS.num_epochs, 
    num_threads=FLAGS.num_preprocess_threads,
    batch_join=FLAGS.batch_join,
    shuffle=FLAGS.shuffle)
  
  train_with_validation = len(sys.argv) > 2
  if train_with_validation:
    validset = sys.argv[2]
    eval_X, eval_y = inputs(
      validset, 
      decode=decode,
      batch_size=FLAGS.batch_size * 10,
      num_threads=FLAGS.num_preprocess_threads,
      batch_join=FLAGS.batch_join,
      shuffle=FLAGS.shuffle)
  
  with tf.variable_scope('main') as scope:
    loss, accuracy = model.build_graph(X, y)
    scope.reuse_variables()
    if train_with_validation:
      eval_loss, eval_accuracy = model.build_graph(eval_X, eval_y)
      eval_ops = [eval_loss, eval_accuracy]
    else:
      eval_ops = None

  melt.apps.train_flow(
             [loss, accuracy], 
             deal_results=melt.show_precision_at_k,
             eval_ops=eval_ops,
             deal_eval_results= lambda x: melt.print_results(x, names=['precision@1']),
             model_dir=FLAGS.model_dir
             )
Exemple #10
0
def main():
    with tf.device(config.device):
        t = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:

        logger.info(config.ckpt_path)
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_path))
        logger.info("Loading model completely")
        z_latent = sampler_switch(config)
        d_q = sess.run(t.p_o,
                       feed_dict={
                           t.z_e: dp.test.e,
                           t.x_c: dp.test.c,
                           t.z_l: z_latent,
                           t.p_in: dp.test.rd,
                       })
        r_p = sess.run(t.p_i,
                       feed_dict={
                           t.x_c: dp.test.c,
                           t.z_l: z_latent,
                           t.z_e: dp.test.e,
                           t.p_in: dp.test.rd
                       })

        # inverse the scaled output
        qm, qr, rdm, rdr = dp.out.qm, dp.out.qr, dp.out.rdm, dp.out.rdr
        actual_Q = anti_norm(dp.test.q, qm, qr)
        result_Q = anti_norm(d_q, qm, qr)
        actual_r = anti_norm(dp.test.rd, rdm, rdr)
        result_r = anti_norm(r_p, rdm, rdr)

        # save the result
        ensemble = {
            'actual_Q': actual_Q,
            'result_Q': result_Q,
            'actual_r': actual_r,
            'result_r': result_r
        }

        path = os.path.join(config.logs_path, config.description + '-test.pkl')
        pickle_save(ensemble, 'test_result', path)
        copy_file(path, config.history_test_path)

        # visualize the process
        vis.cplot(actual_Q[:, 0], result_Q[:, 0], ['Q1', 'origin', 'modify'],
                  config.t_p)
        vis.cplot(actual_Q[:, 1], result_Q[:, 1], ['Q2', 'origin', 'modify'],
                  config.t_p)
        for num in range(6):
            vis.cplot(actual_r[:, num], result_r[:, num],
                      ['R{}'.format(num + 1), 'origin', 'modify'], config.t_p)
Exemple #11
0
def build_eval_model(hparams, scope="eval"):

    src_lang = hparams.src_lang
    src_vocab_file_name = hparams.src_vocab_file_name
    tgt_lang = hparams.tgt_lang
    tgt_vocab_file_name = hparams.tgt_vocab_file_name

    tf.reset_default_graph()

    eval_graph = tf.Graph()
    with eval_graph.as_default() as g:
        with tf.container(scope):
            src_vocab, tgt_vocab = load_vocabs(src_lang, src_vocab_file_name,
                                               tgt_lang, tgt_vocab_file_name)
            src_dataset_file_name = tf.placeholder(
                tf.string, name="src_dataset_file_name")
            tgt_dataset_file_name = tf.placeholder(
                tf.string, name="tgt_dataset_file_name")

            src_dataset = tf.data.TextLineDataset(src_dataset_file_name)
            tgt_dataset = tf.data.TextLineDataset(tgt_dataset_file_name)

            batch_size = tf.placeholder(tf.int64, name="batch_size")

            # maximum sequence length for training example
            max_len = tf.placeholder(tf.int64, name="max_len")

            iterator = Iterator(src_dataset,
                                src_vocab,
                                tgt_dataset,
                                tgt_vocab,
                                batch_size=batch_size,
                                max_len=max_len)

            # actual TensorFlow Dataset Iterator
            iterator_tf = iterator.create_iterator(shuffle=False)

            model_class = _get_model_from_str_type(hparams.model_name)

            model = model_class(hparams, src_vocab, tgt_vocab)
            model_graph = model.build_graph(iterator_tf,
                                            tf.contrib.learn.ModeKeys.EVAL,
                                            batch_size, g)

            return NTModel(src_vocab=src_vocab,
                           tgt_vocab=tgt_vocab,
                           iterator_tf=iterator_tf,
                           model_graph=model_graph,
                           model=model,
                           hparams=hparams,
                           mode=tf.contrib.learn.ModeKeys.EVAL)
def train():
    trainset = sys.argv[1]
    inputs = melt.shuffle_then_decode.inputs
    X, y = inputs(trainset,
                  decode=decode,
                  batch_size=FLAGS.batch_size,
                  num_epochs=FLAGS.num_epochs,
                  num_threads=FLAGS.num_preprocess_threads,
                  batch_join=FLAGS.batch_join,
                  shuffle=FLAGS.shuffle)

    train_with_validation = len(sys.argv) > 2
    if train_with_validation:
        validset = sys.argv[2]
        eval_X, eval_y = inputs(validset,
                                decode=decode,
                                batch_size=FLAGS.batch_size * 10,
                                num_threads=FLAGS.num_preprocess_threads,
                                batch_join=FLAGS.batch_join,
                                shuffle=FLAGS.shuffle)

    loss, accuracy = model.build_graph(X, y)
    if train_with_validation:
        tf.get_variable_scope().reuse_variables()
        eval_loss, eval_accuracy = model.build_graph(eval_X, eval_y)
        eval_ops = [eval_loss, eval_accuracy]
    else:
        eval_ops = None

    melt.apps.train_flow(
        [loss, accuracy],
        deal_results=melt.show_precision_at_k,
        eval_ops=eval_ops,
        deal_eval_results=lambda x: melt.print_results(x,
                                                       names=['precision@1']),
    )
Exemple #13
0
def main():

    hparams = hyperparameters.hparams

    print("************************************")
    print("*********** Begin Train ************")
    print("************************************")

    print("Model: %s" % hparams.model_name)
    print("Optimizer: %s" % hparams.optimizer_name)
    print("Data directory: %s" % hparams.data_dir)
    print("Log directory: %s" % hparams.log_dir)

    inputs, loss, train_op = model.build_graph(hparams)
    train.run_train(hparams, inputs, loss, train_op)
Exemple #14
0
def main():
    with tf.device(config.device):
        t = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_path))

        q_errors = []
        r_adjs = []
        z_adjs = []

        z_true = sess.run(t.z_img, feed_dict={t.x: dp.test.rd})

        for time in range(test_times):
            z_latent = sampler_switch(config)
            q_error = sess.run(t.dq,
                               feed_dict={
                                   t.z_e: dp.test.e,
                                   t.x_c: dp.test.c,
                                   t.z_l: z_latent,
                                   t.p_in: dp.test.rd,
                                   t.p_t: dp.test.q,
                               })
            r_adj = sess.run(t.x_lat,
                             feed_dict={
                                 t.x_c: dp.test.c,
                                 t.z_l: z_latent,
                                 t.z_e: dp.test.e,
                             })
            z_adj = sess.run(t.z_img, feed_dict={t.x: r_adj})

            q_errors.append(q_error)
            r_adjs.append(r_adj)
            z_adjs.append(z_adj)

        q_errors = (np.array(q_errors) - np.expand_dims(dp.test.e, axis=0))**2
        r_adjs = np.array(r_adjs).reshape(-1, config.ndim_x)
        z_adjs = np.array(z_adjs).reshape(-1, config.ndim_z)

        pickle_save([q_errors, r_adjs, z_adjs, z_true],
                    ["productions", "adjustment", "latent_variables"],
                    '{}/{}-metric_plus.pkl'.format(config.logs_path,
                                                   config.description))
Exemple #15
0
def main():
    images, labels = dataset.load_test_images()
    num_scatter = len(images)
    _images, _, label_id = dataset.sample_labeled_data(images, labels,
                                                       num_scatter)
    with tf.device(config.device):
        x, z_respresentation, x_construction = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
        z, _x = sess.run([z_respresentation, x_construction],
                         feed_dict={x: _images})

        scatter_labeled_z(z, label_id, dir=config.ckpt_dir)
        tile_images(_x[:100], dir=config.ckpt_dir)
        plot_loss_tendency(config.ckpt_dir)
Exemple #16
0
def target():
    """
    Label the data using a particular model and save the softmax values.
    Generates one softmax values per file
    """
    flags = parse_flags()
    hparams = parse_hparams(flags.hparams)
    num_classes = 41
    df = pd.read_csv(flags.infer_csv_file)
    file_names = df.iloc[:, 0].values

    count = 0

    sr = 32000
    df = pd.read_csv(flags.infer_csv_file)
    x, _ = utils_tf._load_dataset(cfg.to_dataset('training'))
    generator = utils.fit_scaler(x)
    file_names = df.iloc[:, 0].values
    with tf.Graph().as_default() as graph:
        mel_filt = librosa.filters.mel(sr=32000, n_fft=1024, n_mels=64).T
        mel_filt = tf.convert_to_tensor(mel_filt, dtype=tf.float32)
        pcm = tf.placeholder(tf.float32, shape=[None], name='input_audio')
        model = CleverHansModel(flags.save_model_dir + '.meta', sr, generator,
                                mel_filt)
        saver = model.build_graph(pcm)

    with tf.Session(graph=graph) as sess:
        saver.restore(sess, flags.save_model_dir)
        print(len(file_names))
        for i in range(100):
            data, _ = utils_tf._preprocess_data(flags.infer_audio_dir,
                                                file_names[i])
            l = sess.run([model.get_probs()], feed_dict={pcm: data})
            l = np.squeeze(l)
            if (l.ndim != 1):
                l = np.mean(l, axis=0)

            lab = utils_tf._convert_label_name_to_label(df.iloc[i, 1])
            if (lab == np.argmax(l)):
                count += 1
                print(lab, np.argmax(l))

            print(count / 100)
Exemple #17
0
def test():
    X, y = inputs(sys.argv[1],
                  decode=decode,
                  batch_size=FLAGS.batch_size,
                  num_epochs=1,
                  num_preprocess_threads=FLAGS.num_preprocess_threads,
                  batch_join=FLAGS.batch_join,
                  shuffle=FLAGS.shuffle)

    loss, accuracy = model.build_graph(X, y)

    eval_names = ['loss', 'precision@1']
    print('eval_names:', eval_names)

    test_flow([loss, accuracy],
              names=eval_names,
              model_dir=FLAGS.model_path,
              num_steps=FLAGS.num_steps,
              eval_times=FLAGS.eval_times)
Exemple #18
0
def test():
    X, y = inputs(
        sys.argv[1],
        decode=decode,
        batch_size=FLAGS.batch_size,
        num_epochs=
        0,  #for test we will set num_epochs as 1! but now has epoch variable model loading problem, so set 0..
        num_preprocess_threads=FLAGS.num_preprocess_threads,
        batch_join=FLAGS.batch_join,
        shuffle=FLAGS.shuffle)

    loss, accuracy = model.build_graph(X, y)

    eval_names = ['loss', 'precision@1']
    print('eval_names:', eval_names)

    test_flow([loss, accuracy],
              names=eval_names,
              model_dir=FLAGS.model_path,
              num_steps=FLAGS.num_steps,
              eval_times=FLAGS.eval_times)
def main():
    # load MNIST images
    images, labels = dataset.load_test_images()

    # Settings
    num_scatter = len(images)
    _images, _, label_id = dataset.sample_labeled_data(images, labels,
                                                       num_scatter)

    with tf.device(config.device):
        t = build_graph(is_test=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
        # z distributed plot
        num_segments = 20
        limit = (-5, 5)
        x_values = np.linspace(limit[0], limit[1], num_segments)
        y_values = np.linspace(limit[0], limit[1], num_segments)
        vacant = np.zeros((28 * num_segments, 28 * num_segments))
        for i, x_element in enumerate(x_values):
            for j, y_element in enumerate(y_values):
                x_reconstruction = sess.run(
                    t.x_r,
                    feed_dict={
                        t.yz: np.reshape([x_element, y_element], [1, 2])
                    })
                vacant[(num_segments - 1 - i) * 28:(num_segments - i) * 28,
                       j * 28:(j + 1) * 28] = x_reconstruction.reshape(28, 28)

        vacant = (vacant + 1) / 2
        pylab.figure(figsize=(10, 10), dpi=400, facecolor='white')
        pylab.imshow(vacant, cmap='gray', origin='upper')
        pylab.tight_layout()
        pylab.axis('off')
        pylab.savefig("{}/clusters.png".format(config.ckpt_dir))
Exemple #20
0
def train():
    BATCH_SIZE = 128
    y1, y2, y3, ai, bi, ci, train_op, loss = build_graph()
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # saver.restore(sess, "./tmp/model.ckpt")
        for i in range(5000):
            _, s1, s2, s3, s4, s5, s6, s7 = sess.run(
                [train_op, y1, y2, y3, ai, bi, ci, loss])
            if i % 100 == 0:
                accuracy = get_accuracy(s1, s2, s3, s4, s5, s6, BATCH_SIZE)
                print('Step %s: Accuracy of this batch is %s%%. Loss: %s ' %
                      (i, accuracy * 100, s7))
                print('The first number: ', np.round(s1[:10, 0]))
                print('The second number:', np.round(s2[:10, 0]))
                print('The third number: ', np.round(s3[:10, 0]))
            # if i > 1000:
            # 	accuracy = get_accuracy(s1, s2, s3, s4, s5, s6, BATCH_SIZE)
            # 	if(accuracy == 1 and s7<0.06):
            # 			break
        checkpoint_path = os.path.join('./tmp', 'model.ckpt')
        saver.save(sess, checkpoint_path)
    if True:


        partial_trees,node_type_size,level_width,method_indexes,parse_tree_depth = load_data()

    	print 'num of tress',len(partial_trees)
    	print 'node type size:',node_type_size
    	print 'max width of every level:',level_width
#    	print num_sub_trees

#        '''

	previous_accuracy = 0
    	not_increase = 0

    	g = model.build_graph(batch_size=batch_size,state_size=state_size,learning_rate=learning_rate,node_type_size = node_type_size,vector_size=vector_size,level_max_width = level_width,parse_tree_depth=parse_tree_depth,num_classes=num_classes)
	sess = tf.Session()
	# initialize
        sess.run(tf.global_variables_initializer())
#        g['saver'].restore(sess,'weight_average_tmp/GCD/weight')

    	count = 0
    	while True:
	    count += 1
	    print 'EPOCH',count


	    if not os.path.exists('weight_average/GCD'):
	        os.makedirs('weight_average/GCD')
	    if not os.path.exists('weight_average_tmp/GCD'):
	        os.makedirs('weight_average_tmp/GCD')
Exemple #22
0
import tensorflow as tf

from model import build_graph
from config import FLAGS
from preprocess import get_X, int2text


def predict(img):
    size = (FLAGS.image_size, FLAGS.image_size)
    image = get_X(img, size)
    predict_opts = [graph['predicted_val_top_k'], graph['predicted_index_top_k']]
    feed_dict = {graph['images']: [image],
                 graph['keep_prob']: 1.0,
                 graph['is_training']: False}
    predict_val, predict_index = sess.run(predict_opts, feed_dict=feed_dict)
    predict_index = predict_index.flatten()
    text = int2text(predict_index[0], FLAGS.wordset)
    return text



# when import from other modules, load the model
sess = tf.Session()
graph = build_graph(top_k=1)
saver = tf.train.Saver()
ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
saver.restore(sess, ckpt)
Exemple #23
0
import os, sys
import tensorflow as tf
import numpy as np
import pylab

sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "../../")))
from model import build_graph, config

with tf.device(config.device):
    t = build_graph(is_test=True)

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=True)) as sess:
    saver = tf.train.Saver()
    saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
    # z distributed plot
    num_segments = 20
    xlimit = (-6, 2)
    ylimit = (-6, 2)
    x_values = np.linspace(xlimit[0], xlimit[1], num_segments)
    y_values = np.linspace(ylimit[0], ylimit[1], num_segments)
    vacant = np.zeros((28 * num_segments, 28 * num_segments))
    z_batch = []
    for x_element in x_values:
        for y_element in y_values:
            z_batch.append([x_element, y_element])

    z_batch = np.array(z_batch).reshape(-1, 2)

    x_reconstruction = sess.run(t.x_r, feed_dict={t.z_r: z_batch})
    x_reconstruction = (x_reconstruction + 1) / 2
Exemple #24
0
# Validation
val_imgs_path = './cac_numpy_data/val_imgs.npy'
val_labs_path = './cac_numpy_data/val_labs.npy'

train_imgs , train_labs, val_imgs , val_labs , test_imgs , test_labs = \
    map(np.load , [train_imgs_path , train_labs_path , val_imgs_path , val_labs_path , test_imgs_path , test_labs_path])
test_imgs = test_imgs / 255.
val_imgs = val_imgs / 255.

x_, y_, lr_, is_training, global_step = define_inputs(
    shape=[None, 540, 540, 3], n_classes=3)  # 1~9 / 10~100 / 100~inf

logits = build_graph(x_=x_,
                     y_=y_,
                     is_training=is_training,
                     aug_flag=True,
                     actmap_flag=True,
                     model='vgg_11',
                     random_crop_resize=540,
                     bn=True)
sess, saver, summary_writer = sess_start(logs_path='./logs/vgg_11')
pred, pred_cls, cost_op, cost_mean, train_op, correct_pred, accuracy_op = algorithm(
    logits, y_, lr_, 'sgd', 'mse')

for i in range(100000):
    batch_xs, batch_ys = batch_selector(train_imgs, train_labs, 5, 5, 5, 5)
    #print np.shape(batch_xs) , np.shape(batch_ys)
    batch_xs = batch_xs / 255.
    batch_ys = batch_ys.reshape([-1, 1])
    train_fetches = [train_op, accuracy_op, cost_mean, logits]
    train_feedDict = {x_: batch_xs, y_: batch_ys, lr_: lr, is_training: True}
    _, train_acc, train_loss, train_preds = sess.run(fetches=train_fetches,
Exemple #25
0
def begin_eval(para, repo, train_index, val_index):

    methods = np.load('./data/comment_%s_data/comment_method_vector.npy' %
                      repo)
    print 'shape of method vectors', methods.shape

    comment_array = np.load('./data/comment_%s_data/comment_array.npy' % repo)
    print 'shape of comment array', comment_array.shape

    vector_size = len(methods[0])
    print 'vector size', vector_size

    bodies_array = np.load('./data/body/body_%s/bodies_array.npy' % repo)
    print 'shape of bodies array', bodies_array.shape
    body_length = len(bodies_array[0])

    bodyWordToIndex, bodyIndexToWord = reader._read_comments_word(
        './data/body_words/bodyWordMap.txt')
    body_vocab_size = len(bodyWordToIndex)

    wordToIndex, indexToWord = reader._read_comments_word(
        './data/comment_words/commentWordMap.txt')
    print 'length', len(wordToIndex), len(indexToWord)
    num_classes = len(wordToIndex)

    #random_index = np.load('./comment_%s_data/random_index_array.npy'%repo)

    if para == '3':
        state_size = 1
    else:
        state_size = 512

    g = model.build_graph(batch_size, state_size, learning_rate, num_steps,
                          vector_size, num_classes, body_length,
                          body_vocab_size)

    gpu_options = tf.GPUOptions(allow_growth=True)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            intra_op_parallelism_threads=8))
    if para == 1:
        g['saver'].restore(sess, 'CR_temp/weight')
    else:
        #g['saver'].restore(sess,'CR/weight')
        g['saver'].restore(sess, 'CR_%s_tab/weight' % repo)

    system_strings, model_strings = eval(sess, val_index - train_index,
                                         batch_size, num_steps, num_classes,
                                         methods[train_index:val_index],
                                         comment_array[train_index:val_index],
                                         bodies_array[train_index:val_index],
                                         g, indexToWord, wordToIndex, para)

    sess.close()

    r.system_filename_pattern = 'a.(\d+).txt'
    r.model_filename_pattern = 'a.[A-Z].#ID#.txt'

    output = r.convert_and_evaluate()
    #    print(output)

    index = output.find('ROUGE-2 Average_F: ')

    value = float(output[index + 19:index + 26])

    #output_dict = r.output_to_dict(output)

    return value, output
Exemple #26
0
def train(batch_size, lr, epochs, keep_prob, chkp_dir, output_pb):
    click.echo(
        click.style(
            "lr: {}, keep_prob: {}, output pbfile: {}".format(
                lr, keep_prob, output_pb),
            fg="cyan",
            bold=True,
        ))
    cifar10_train = cifar.CIFAR10("./cifar10_data", download=True, train=True)
    cifar10_test = cifar.CIFAR10("./cifar10_data", download=True, train=False)
    mean = ((cifar10_train.train_data.astype("float32") /
             255.0).mean(axis=(0, 1, 2)).tolist())
    std = ((cifar10_train.train_data.astype("float32") /
            255.0).std(axis=(1, 2)).mean(axis=0).tolist())
    cifar10_train.transform = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.RandomAffine(degrees=0, translate=(0.1, 0.1)),
        transforms.ToTensor(),
        transforms.Normalize(mean, std),
    ])
    cifar10_test.transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize(mean, std)])
    train_loader = torch.utils.data.DataLoader(cifar10_train,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               num_workers=2)
    eval_loader = torch.utils.data.DataLoader(cifar10_test,
                                              batch_size=len(cifar10_test),
                                              shuffle=False,
                                              num_workers=2)
    graph = tf.Graph()
    with graph.as_default():
        tf_image_batch = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
        tf_labels = tf.placeholder(tf.float32, shape=[None, 10])
        tf_keep_prob = tf.placeholder(tf.float32, name="keep_prob")
        tf_pred, train_op, tf_total_loss, saver = build_graph(tf_image_batch,
                                                              tf_labels,
                                                              tf_keep_prob,
                                                              lr=lr)
    best_acc = 0.0
    chkp_cnt = 0
    with tf.Session(graph=graph) as sess:
        tf.global_variables_initializer().run()
        for epoch in range(1, epochs + 1):
            for i, (img_batch, label_batch) in enumerate(train_loader, 1):
                np_img_batch = img_batch.numpy().transpose((0, 2, 3, 1))
                np_label_batch = label_batch.numpy()
                _ = sess.run(
                    train_op,
                    feed_dict={
                        tf_image_batch: np_img_batch,
                        tf_labels: one_hot(np_label_batch),
                        tf_keep_prob: keep_prob,
                    },
                )
                if (i % 100) == 0:
                    img_batch, label_batch = next(iter(eval_loader))
                    np_img_batch = img_batch.numpy().transpose((0, 2, 3, 1))
                    np_label_batch = label_batch.numpy()
                    pred_label = sess.run(
                        tf_pred,
                        feed_dict={
                            tf_image_batch: np_img_batch,
                            tf_keep_prob: 1.0
                        },
                    )
                    acc = (pred_label
                           == np_label_batch).sum() / np_label_batch.shape[0]
                    click.echo(
                        click.style(
                            "[epoch {}: {}], accuracy {:0.2f}%".format(
                                epoch, i, acc * 100),
                            fg="yellow",
                            bold=True,
                        ))
                    if acc >= best_acc:
                        best_acc = acc
                        chkp_cnt += 1
                        click.echo(
                            click.style(
                                "[epoch {}: {}] saving checkpoint, {} with acc {:0.2f}%"
                                .format(epoch, i, chkp_cnt, best_acc * 100),
                                fg="white",
                                bold=True,
                            ))
                        best_chkp = saver.save(sess,
                                               chkp_dir,
                                               global_step=chkp_cnt)
    best_graph_def = prepare_meta_graph("{}.meta".format(best_chkp),
                                        output_nodes=[tf_pred.op.name])

    with open(output_pb, "wb") as fid:
        fid.write(best_graph_def.SerializeToString())
        click.echo(
            click.style("{} saved".format(output_pb), fg="blue", bold=True))
Exemple #27
0
def main(run_load_from_file=False):
    # load MNIST images
    images, labels = dataset.load_test_images()

    # config
    opt = Operation()
    opt.check_dir(config.ckpt_dir, is_restart=False)
    opt.check_dir(config.log_dir, is_restart=True)

    max_epoch = 510
    num_trains_per_epoch = 500
    batch_size_u = 100

    # training
    with tf.device(config.device):
        h = build_graph()

    sess_config = tf.ConfigProto(allow_soft_placement=True,
                                 log_device_placement=True)
    sess_config.gpu_options.allow_growth = True
    sess_config.gpu_options.per_process_gpu_memory_fraction = 0.9
    saver = tf.train.Saver(max_to_keep=2)

    with tf.Session(config=sess_config) as sess:
        '''
         Load from checkpoint or start a new session

        '''
        if run_load_from_file:
            saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
            training_epoch_loss, _ = pickle.load(
                open(config.ckpt_dir + '/pickle.pkl', 'rb'))
        else:
            sess.run(tf.global_variables_initializer())
            training_epoch_loss = []

        # Recording loss per epoch
        process = Process()
        for epoch in range(max_epoch):
            process.start_epoch(epoch, max_epoch)
            '''
            Learning rate generator

            '''
            learning_rate = 0.0001

            # Recording loss per iteration
            sum_loss_reconstruction = 0
            sum_loss_discrminator_z = 0
            sum_loss_discrminator_img = 0
            sum_loss_generator_z = 0
            sum_loss_generator_img = 0
            process_iteration = Process()
            for i in range(num_trains_per_epoch):
                process_iteration.start_epoch(i, num_trains_per_epoch)
                # Inputs
                '''
                _l -> labeled
                _u -> unlabeled

                '''
                images_u = dataset.sample_unlabeled_data(images, batch_size_u)
                if config.distribution_sampler == 'swiss_roll':
                    z_true_u = sampler.swiss_roll(batch_size_u, config.ndim_z,
                                                  config.num_types_of_label)
                elif config.distribution_sampler == 'gaussian_mixture':
                    z_true_u = sampler.gaussian_mixture(
                        batch_size_u, config.ndim_z, config.num_types_of_label)
                elif config.distribution_sampler == 'uniform_desk':
                    z_true_u = sampler.uniform_desk(batch_size_u,
                                                    config.ndim_z,
                                                    radius=2)
                elif config.distribution_sampler == 'gaussian':
                    z_true_u = sampler.gaussian(batch_size_u,
                                                config.ndim_z,
                                                var=1)
                elif config.distribution_sampler == 'uniform':
                    z_true_u = sampler.uniform(batch_size_u,
                                               config.ndim_z,
                                               minv=-1,
                                               maxv=1)

                # reconstruction_phase
                _, loss_reconstruction = sess.run([h.opt_r, h.loss_r],
                                                  feed_dict={
                                                      h.x: images_u,
                                                      h.lr: learning_rate
                                                  })

                # adversarial phase for discriminator_z
                images_u_s = dataset.sample_unlabeled_data(
                    images, batch_size_u)
                _, loss_discriminator_z = sess.run([h.opt_dz, h.loss_dz],
                                                   feed_dict={
                                                       h.x: images_u,
                                                       h.z: z_true_u,
                                                       h.lr: learning_rate
                                                   })

                _, loss_discriminator_img = sess.run([h.opt_dimg, h.loss_dimg],
                                                     feed_dict={
                                                         h.x: images_u,
                                                         h.x_s: images_u_s,
                                                         h.lr: learning_rate
                                                     })

                # adversarial phase for generator
                _, loss_generator_z = sess.run([h.opt_e, h.loss_e],
                                               feed_dict={
                                                   h.x: images_u,
                                                   h.lr: learning_rate
                                               })

                _, loss_generator_img = sess.run([h.opt_d, h.loss_d],
                                                 feed_dict={
                                                     h.x: images_u,
                                                     h.lr: learning_rate
                                                 })

                sum_loss_reconstruction += loss_reconstruction
                sum_loss_discrminator_z += loss_discriminator_z
                sum_loss_discrminator_img += loss_discriminator_img
                sum_loss_generator_z += loss_generator_z
                sum_loss_generator_img += loss_generator_img

                if i % 1000 == 0:
                    process_iteration.show_table_2d(
                        i, num_trains_per_epoch, {
                            'reconstruction':
                            sum_loss_reconstruction / (i + 1),
                            'discriminator_z':
                            sum_loss_discrminator_z / (i + 1),
                            'discriminator_img':
                            sum_loss_discrminator_img / (i + 1),
                            'generator_z':
                            sum_loss_generator_z / (i + 1),
                            'generator_img':
                            sum_loss_generator_img / (i + 1),
                        })

            average_loss_per_epoch = [
                sum_loss_reconstruction / num_trains_per_epoch,
                sum_loss_discrminator_z / num_trains_per_epoch,
                sum_loss_discrminator_img / num_trains_per_epoch,
                sum_loss_generator_z / num_trains_per_epoch,
                sum_loss_generator_img / num_trains_per_epoch,
                (sum_loss_discrminator_z + sum_loss_discrminator_img) /
                num_trains_per_epoch,
                (sum_loss_generator_z + sum_loss_generator_img) /
                num_trains_per_epoch
            ]
            training_epoch_loss.append(average_loss_per_epoch)
            training_loss_name = [
                'reconstruction', 'discriminator_z', 'discriminator_img',
                'generator_z', 'generator_img', 'discriminator', 'generator'
            ]

            if epoch % 1 == 0:
                process.show_bar(
                    epoch, max_epoch, {
                        'loss_r': average_loss_per_epoch[0],
                        'loss_d': average_loss_per_epoch[5],
                        'loss_g': average_loss_per_epoch[6]
                    })

                plt.scatter_labeled_z(
                    sess.run(h.z_r, feed_dict={h.x: images[:1000]}),
                    [int(var) for var in labels[:1000]],
                    dir=config.log_dir,
                    filename='z_representation-{}'.format(epoch))

            if epoch % 10 == 0:
                saver.save(sess,
                           os.path.join(config.ckpt_dir, 'model_ckptpoint'),
                           global_step=epoch)
                pickle.dump((training_epoch_loss, training_loss_name),
                            open(config.ckpt_dir + '/pickle.pkl', 'wb'))
Exemple #28
0
def main(run_load_from_file=False):
    # load MNIST images
    images, labels = dataset.load_train_images()

    # config
    opt = Operation()
    opt.check_dir(config.ckpt_dir, is_restart=False)
    opt.check_dir(config.log_dir, is_restart=True)

    # setting
    max_epoch = 510
    num_trains_per_epoch = 500
    batch_size_u = 100

    # training
    with tf.device(config.device):
        h = build_graph()

    sess_config = tf.ConfigProto(allow_soft_placement=True,
                                 log_device_placement=True)
    sess_config.gpu_options.allow_growth = True
    sess_config.gpu_options.per_process_gpu_memory_fraction = 0.9
    saver = tf.train.Saver(max_to_keep=2)

    with tf.Session(config=sess_config) as sess:
        '''
         Load from checkpoint or start a new session

        '''
        if run_load_from_file:
            saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir))
            training_epoch_loss, _ = pickle.load(
                open(config.ckpt_dir + '/pickle.pkl', 'rb'))
        else:
            sess.run(tf.global_variables_initializer())
            training_epoch_loss = []

        # Recording loss per epoch
        process = Process()
        for epoch in range(max_epoch):
            process.start_epoch(epoch, max_epoch)
            '''
            Learning rate generator

            '''
            learning_rate = opt.ladder_learning_rate(epoch +
                                                     len(training_epoch_loss))

            # Recording loss per iteration
            training_loss_set = []
            sum_loss_reconstruction = 0
            sum_loss_supervised = 0
            sum_loss_discrminator = 0
            sum_loss_generator = 0

            process_iteration = Process()
            for i in range(num_trains_per_epoch):
                process_iteration.start_epoch(i, num_trains_per_epoch)

                # sample from data distribution
                images_u = dataset.sample_unlabeled_data(images, batch_size_u)

                # reconstruction_phase
                _, loss_reconstruction = sess.run([h.opt_r, h.loss_r],
                                                  feed_dict={
                                                      h.x: images_u,
                                                      h.lr: learning_rate
                                                  })

                z_true_u = sampler.gaussian(batch_size_u,
                                            config.ndim_z,
                                            mean=0,
                                            var=1)
                y_true_u = sampler.onehot_categorical(batch_size_u,
                                                      config.ndim_y)
                # adversarial phase for discriminator
                _, loss_discriminator_y = sess.run([h.opt_dy, h.loss_dy],
                                                   feed_dict={
                                                       h.x: images_u,
                                                       h.y: y_true_u,
                                                       h.lr: learning_rate
                                                   })

                _, loss_discriminator_z = sess.run([h.opt_dz, h.loss_dz],
                                                   feed_dict={
                                                       h.x: images_u,
                                                       h.z: z_true_u,
                                                       h.lr: learning_rate
                                                   })

                loss_discriminator = loss_discriminator_y + loss_discriminator_z

                # adversarial phase for generator
                _, loss_generator_y, loss_generator_z = sess.run(
                    [h.opt_e, h.loss_gy, h.loss_gz],
                    feed_dict={
                        h.x: images_u,
                        h.lr: learning_rate
                    })

                loss_generator = loss_generator_y + loss_generator_z

                training_loss_set.append([
                    loss_reconstruction,
                    loss_discriminator,
                    loss_discriminator_y,
                    loss_discriminator_z,
                    loss_generator,
                    loss_generator_z,
                    loss_generator_y,
                ])

                sum_loss_reconstruction += loss_reconstruction
                sum_loss_discrminator += loss_discriminator
                sum_loss_generator += loss_generator

                if i % 1000 == 0:
                    process_iteration.show_table_2d(
                        i, num_trains_per_epoch, {
                            'reconstruction': sum_loss_reconstruction /
                            (i + 1),
                            'discriminator': sum_loss_discrminator / (i + 1),
                            'generator': sum_loss_generator / (i + 1),
                        })
            # In end of epoch, summary the loss
            average_training_loss_per_epoch = np.mean(
                np.array(training_loss_set), axis=0)

            # append validation accuracy to the training loss
            training_epoch_loss.append(average_training_loss_per_epoch)
            loss_name_per_epoch = [
                'reconstruction',
                'discriminator',
                'discriminator_y',
                'discriminator_z',
                'generator',
                'generator_z',
                'generator_y',
            ]

            if epoch % 1 == 0:
                process.show_bar(
                    epoch, max_epoch, {
                        'loss_r': average_training_loss_per_epoch[0],
                        'loss_d': average_training_loss_per_epoch[1],
                        'loss_g': average_training_loss_per_epoch[4],
                    })

                plt.tile_images(sess.run(h.x_, feed_dict={h.x: images_u}),
                                dir=config.log_dir,
                                filename='x_rec_epoch_{}'.format(
                                    str(epoch).zfill(3)))

            if epoch % 10 == 0:
                saver.save(sess,
                           os.path.join(config.ckpt_dir, 'model_ckptpoint'),
                           global_step=epoch)
                pickle.dump((training_epoch_loss, loss_name_per_epoch),
                            open(config.ckpt_dir + '/pickle.pkl', 'wb'))
Exemple #29
0
eval_graph = tf.Graph()

with train_graph.as_default():
    pitch = tf.placeholder(tf.int32, (None, None))
    velocity = tf.placeholder(tf.int32, (None, None))
    dt = tf.placeholder(tf.int32, (None, None))
    duration = tf.placeholder(tf.int32, (None, None))

    noteseq = {
        "pitch": pitch,
        "velocity": velocity,
        "dt": dt,
        "duration": duration
    }

    rnn = build_graph(noteseq, config, True, False)

    global_step = tf.train.create_global_step()
    lr = tf.train.exponential_decay(config.learning_rate, global_step, 8000,
                                    0.989)

    opt = tf.train.AdamOptimizer(learning_rate=lr)
    grads, var = zip(*opt.compute_gradients(rnn["loss"]))
    grads, _ = tf.clip_by_global_norm(grads, 5.0)
    train_op = opt.apply_gradients(zip(grads, var), global_step=global_step)
    init = tf.global_variables_initializer()

    train_saver = tf.train.Saver()

    with tf.variable_scope("feature_loss"):
        summaries = [
Exemple #30
0
if not os.path.exists(result_path): os.makedirs(result_path)
log_path = os.path.join(result_path, "log.txt")
paths['log_path'] = log_path
utils.get_logger(log_path).info(str(args))

## training model
if args.mode == 'train':
    # 引入第二步建立的模型
    model = model.BiLSTM_CRF(args,
                             embeddings,
                             data.tag2label,
                             word2id,
                             paths,
                             config=config)
    # 创建节点,无返回值
    model.build_graph()

    ## hyperparameters-tuning, split train/dev
    # dev_data = train_data[:5000]; dev_size = len(dev_data)
    # train_data = train_data[5000:]; train_size = len(train_data)
    # print("train data: {0}\ndev data: {1}".format(train_size, dev_size))
    # model.train(train=train_data, dev=dev_data)

    ## train model on the whole training data
    print("train data: {}".format(len(train_data)))
    # 训练
    model.train(train=train_data, dev=test_data
                )  # use test_data as the dev_data to see overfitting phenomena

## testing model
elif args.mode == 'test':