Exemplo n.º 1
0
import tensorflow as tf
import net
import numpy as np

net.build()
vars = tf.all_variables()

saver = tf.train.Saver()
sess = tf.Session()

print([var.name for var in vars])

mat = list(filter(lambda x: x.name == 'linear_1/weights:0', vars))[0]
saver.restore(sess, './log/checkpoint-40000')

W = sess.run(mat)
print(W.shape)
np.savez('mat.npz', mat=W)
Exemplo n.º 2
0
def run_training(extra_opts={}):
    start = datetime.datetime.now()
    start_str = start.strftime('%d-%m-%Y_%H_%M')
    train, validation = input_data.read_data_sets(FLAGS.data_dir)
    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        net.build(extra_opts)
        #Precision summaries
        precision_train = tf.Variable(0.0,
                                      trainable=False,
                                      name='precision_train')
        precision_validation = tf.Variable(0.0,
                                           trainable=False,
                                           name='precision_validation')

        precision_train_summary = tf.summary.scalar('precision/train',
                                                    precision_train)

        precision_validation_summary = tf.summary.scalar(
            'precision/validation', precision_validation)
        graph = tf.get_default_graph()
        loss = graph.get_tensor_by_name('loss:0')
        train_op = graph.get_tensor_by_name('train_op:0')
        correct_count = graph.get_tensor_by_name('correct_count:0')
        #Create summary stuff
        regular_summaries_names = ['loss', 'learning_rate']
        regular_summaries_list = []
        for name in regular_summaries_names:
            summary = graph.get_tensor_by_name('summary/' + name + ':0')
            regular_summaries_list.append(summary)
        regular_summaries = tf.summary.merge(regular_summaries_list,
                                             name='summary/regular_summaries')
        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver(tf.global_variables())
        # Run the Op to initialize the variables.
        init = tf.global_variables_initializer()

        # Create a session for running Ops on the Graph.
        sess = tf.Session(graph=graph,
                          config=tf.ConfigProto(
                              intra_op_parallelism_threads=3,
                              inter_op_parallelism_threads=3))
        sess.run(init)
        # Instantiate a SummaryWriter to output summaries and the Graph.
        summary_writer = tf.summary.FileWriter(FLAGS.log_dir,
                                               graph=tf.get_default_graph())
        # And then after everything is built, start the training loop.
        for step in xrange(1, FLAGS.max_steps + 1):
            start_time = time.time()
            # Fill a feed dictionary with the actual set of images and labels
            # for this particular training step.
            feed_dict = fill_feed_dict(train.next_batch(FLAGS.batch_size))
            # Run one step of the model.  The return values are the activations
            # from the `train_op` (which is discarded) and the `loss` Op.  To
            # inspect the values of your Ops or variables, you may include them
            # in the list passed to sess.run() and the value tensors will be
            # returned in the tuple from the call.

            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
            duration = time.time() - start_time
            # Write the summaries and print an overview fairly often.
            if step % FLAGS.overview_steps == 0:
                # Print status to stdout.
                data_per_sec = FLAGS.batch_size / duration
                print('Step %d: loss = %.2f (%.3f sec) [%.2f data/s]' %
                      (step, loss_value, duration, data_per_sec))
                # Update the events file.
                summary_str = sess.run(regular_summaries, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)

            # Save a checkpoint and evaluate the model periodically.
            if (step) % FLAGS.evaluation_steps == 0 or step == FLAGS.max_steps:
                saver.save(sess,
                           FLAGS.log_dir + '/checkpoint',
                           global_step=step)
                # Evaluate against the training set.
                print('Training Data Eval:')
                precision_t, obj_t = do_eval(sess, correct_count, loss, train)
                sess.run(precision_train.assign(precision_t))

                # Evaluate against the validation set.
                print('Validation Data Eval:')
                precision_v, obj_v = do_eval(sess, correct_count, loss,
                                             validation)
                sess.run(precision_validation.assign(precision_v))

                summary_str_0, summary_str_1 = sess.run(
                    [precision_train_summary, precision_validation_summary])
                summary_writer.add_summary(summary_str_0, step)
                summary_writer.add_summary(summary_str_1, step)

                os.makedirs('./results', exist_ok=True)
                res_file = open('./results/res_' + str(start_str), 'w')

                res_file.write('Iterations: ' + str(step) + '\n')
                now = datetime.datetime.now()
                delta = now - start
                res_file.write('Learning time: {0:.2f} minutes\n'.format(
                    delta.total_seconds() / 60.0))
                res_file.write(
                    'Train precision: {0:.5f}\n'.format(precision_t))
                res_file.write('Train loss: {0:.5f}\n'.format(obj_t))
                res_file.write(
                    'Validation precision: {0:.5f}\n'.format(precision_v))
                res_file.write('Validation loss: {0:.5f}\n'.format(obj_v))
                res_file.write('Extra opts: ' + str(extra_opts) + '\n')
                res_file.write('Code:\n')
                net_file = open('./net.py', 'r')
                shutil.copyfileobj(net_file, res_file)
                net_file.close()
                res_file.close()
Exemplo n.º 3
0
def run_training(extra_opts={}):
    start = datetime.datetime.now()
    start_str = start.strftime('%d-%m-%Y_%H_%M')
    train, validation = input_data.read_data_sets(FLAGS.data_dir)
    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        net.build(extra_opts)
        #Precision summaries
        precision_train = tf.Variable(0.0, trainable=False, name='precision_train')
        precision_validation = tf.Variable(0.0, trainable=False, name='precision_validation')

        precision_train_summary = tf.summary.scalar('precision/train',
                                                    precision_train)

        precision_validation_summary = tf.summary.scalar('precision/validation',
                                                         precision_validation)
        graph = tf.get_default_graph()
        loss = graph.get_tensor_by_name('loss:0')
        train_op = graph.get_tensor_by_name('train_op:0')
        correct_count = graph.get_tensor_by_name('correct_count:0')
        #Create summary stuff
        regular_summaries_names = ['loss', 'learning_rate']
        regular_summaries_list = []
        for name in regular_summaries_names:
            summary = graph.get_tensor_by_name('summary/' + name + ':0')
            regular_summaries_list.append(summary)
        regular_summaries = tf.summary.merge(regular_summaries_list, name='summary/regular_summaries')
        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver(tf.global_variables())
        # Run the Op to initialize the variables.
        init = tf.global_variables_initializer()

        # Create a session for running Ops on the Graph.
        sess = tf.Session(graph=graph,
                          config=tf.ConfigProto(intra_op_parallelism_threads=3, inter_op_parallelism_threads = 3))
        sess.run(init)
        # Instantiate a SummaryWriter to output summaries and the Graph.
        summary_writer = tf.summary.FileWriter(FLAGS.log_dir,
                                               graph=tf.get_default_graph())

        # And then after everything is built, start the training loop.
        for step in xrange(1, FLAGS.max_steps + 1):
            start_time = time.time()
            # Fill a feed dictionary with the actual set of images and labels
            # for this particular training step.
            feed_dict = fill_feed_dict(train.next_batch(FLAGS.batch_size))
            # Run one step of the model.  The return values are the activations
            # from the `train_op` (which is discarded) and the `loss` Op.  To
            # inspect the values of your Ops or variables, you may include them
            # in the list passed to sess.run() and the value tensors will be
            # returned in the tuple from the call.


            _, loss_value = sess.run([train_op, loss],
                                     feed_dict=feed_dict)
            duration = time.time() - start_time
            # Write the summaries and print an overview fairly often.
            if step % FLAGS.overview_steps == 0:
                # Print status to stdout.
                data_per_sec = FLAGS.batch_size / duration
                print('Step %d: loss = %.2f (%.3f sec) [%.2f data/s]' %
                      (step, loss_value, duration, data_per_sec))
                # Update the events file.
                summary_str = sess.run(regular_summaries, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)

            # Save a checkpoint and evaluate the model periodically.
            if (step) % FLAGS.evaluation_steps == 0 or step == FLAGS.max_steps:
                saver.save(sess, FLAGS.log_dir +'/checkpoint', global_step=step)
                # Evaluate against the training set.
                print('Training Data Eval:')
                precision_t, obj_t = do_eval(sess,
                                             correct_count,
                                             loss,
                                             train)
                sess.run(precision_train.assign(precision_t))

                # Evaluate against the validation set.
                print('Validation Data Eval:')
                precision_v, obj_v = do_eval(sess,
                                             correct_count,
                                             loss,
                                             validation)
                sess.run(precision_validation.assign(precision_v))

                summary_str_0, summary_str_1 = sess.run([precision_train_summary, precision_validation_summary])
                summary_writer.add_summary(summary_str_0, step)
                summary_writer.add_summary(summary_str_1, step)

                os.makedirs('./results', exist_ok=True)
                res_file = open('./results/res_' + str(start_str), 'w')

                res_file.write('Iterations: ' + str(step) + '\n')
                now = datetime.datetime.now()
                delta = now - start
                res_file.write('Learning time: {0:.2f} minutes\n'.format(delta.total_seconds() / 60.0))
                res_file.write('Train precision: {0:.5f}\n'.format(precision_t))
                res_file.write('Train loss: {0:.5f}\n'.format(obj_t))
                res_file.write('Validation precision: {0:.5f}\n'.format(precision_v))
                res_file.write('Validation loss: {0:.5f}\n'.format(obj_v))
                res_file.write('Extra opts: ' + str(extra_opts) + '\n')
                res_file.write('Code:\n')
                net_file = open('./net.py', 'r')
                shutil.copyfileobj(net_file, res_file)
                net_file.close()
                res_file.close()