コード例 #1
0
    def train(self):
        mnist = logistic_regression.load_data_mnist()

        # negative loss likelihood
        cross_entropy = -tf.reduce_sum(self.y_ * tf.log(self.y_conv))
        # 最適化関数にコストを渡す
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

        # 正しいかどうか
        correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        # 全変数の初期化
        self.sess.run(tf.initialize_all_variables())

        for i in range(1000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
                # 100回ごとにテスト
                train_accuracy = accuracy.eval(feed_dict={self.x: batch[0], self.y_: batch[1], self.keep_prob: 1.0})
                print "setp %d, training accuracy %g" % (i, train_accuracy)
            train_step.run(feed_dict={self.x: batch[0], self.y_: batch[1], self.keep_prob: 1.0})
        print "test accuracy %g" % accuracy.eval(
            feed_dict={self.x: mnist.test.images, self.y_: mnist.test.labels, self.keep_prob: 1.0}
        )
コード例 #2
0
def test():
    print("load settings:")
    print(FLAGS.learning_rate)

    print("load dataset:")
    mnist = logistic_regression.load_data_mnist()

    print("place holder")
    images_placeholder, labels_placeholder = ph.placeholder_inputs(FLAGS.batch_size, model.IMAGE_PIXELS, model.NUM_CLASSES)
    
    print("fill feed dict")
    fd.fill_feed_dict(mnist.train, images_placeholder, labels_placeholder, FLAGS.batch_size, FLAGS.fake_data)    
コード例 #3
0
def test():
    print("load settings:")
    print(FLAGS.learning_rate)

    print("load dataset:")
    mnist = logistic_regression.load_data_mnist()

    print("place holder")
    images_placeholder, labels_placeholder = ph.placeholder_inputs(
        FLAGS.batch_size, model.IMAGE_PIXELS, model.NUM_CLASSES)

    print("fill feed dict")
    fd.fill_feed_dict(mnist.train, images_placeholder, labels_placeholder,
                      FLAGS.batch_size, FLAGS.fake_data)
コード例 #4
0
    def train(self):
        mnist = logistic_regression.load_data_mnist()

        # negative loss likelihood
        cross_entropy = -tf.reduce_sum(self.y_ * tf.log(self.y_conv))
        # 最適化関数にコストを渡す
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

        # 正しいかどうか
        correct_prediction = tf.equal(tf.argmax(self.y_conv, 1),
                                      tf.argmax(self.y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        # 全変数の初期化
        self.sess.run(tf.initialize_all_variables())

        for i in range(1000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
                # 100回ごとにテスト
                train_accuracy = accuracy.eval(feed_dict={
                    self.x: batch[0],
                    self.y_: batch[1],
                    self.keep_prob: 1.0
                })
                print "setp %d, training accuracy %g" % (i, train_accuracy)
            # input data, target data, placeholder
            train_step.run(feed_dict={
                self.x: batch[0],
                self.y_: batch[1],
                self.keep_prob: 1.0
            })
        print "test accuracy %g" % accuracy.eval(
            feed_dict={
                self.x: mnist.test.images,
                self.y_: mnist.test.labels,
                self.keep_prob: 1.0
            })
コード例 #5
0
def run_training():
    data_sets = logistic_regression.load_data_mnist(one_hot=False)

    hiddens = [FLAGS.hidden1, FLAGS.hidden2]

    # Graph実行用のSession
    with tf.Graph().as_default():
        # placeholderの生成
        images_placeholder, labels_placeholder = ph.placeholder_inputs(FLAGS.batch_size, model.IMAGE_PIXELS, model.NUM_CLASSES)

        # logitsの生成
        logits = model.inference(images_placeholder, hiddens)

        # lossの生成
        loss = model.loss(logits, labels_placeholder)

        # trainオペレーションの生成
        train_op = model.train(loss, FLAGS.learning_rate)

        # evaluationオペレーション
        eval_correct = model.evaluation(logits, labels_placeholder)

        # 可視化用サマリ(グラフのビルド時に初期化)
        summary_op = tf.merge_all_summaries()
        
        # modelの保存用オブジェクト
        saver = tf.train.Saver()

        # sessionオブジェクト
        sess = tf.Session()

        # 変数の初期化
        init = tf.initialize_all_variables()
        sess.run(init)

        # 可視化用の変数初期化
        summary_writer = tf.train.SummaryWriter(FLAGS.model_dir, graph_def=sess.graph_def)
        
        # 学習ステップの実行
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()

            # 現在のステップ用のデータを取得
            feed_dict = fd.fill_feed_dict(data_sets.train, images_placeholder, labels_placeholder, FLAGS.batch_size, FLAGS.fake_data)

            # train_opを実行
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

            duration = time.time() - start_time

            # 100ステップごとにサマリーを出力
            if step%FLAGS.summary_step == 0:
                # stdout
                print('Step %d: loss = %.2f , %.3f sec' % (step, loss_value, duration))
                
                # 可視化用の変数アップデート
                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)

            # 1000ステップごとにcheckpointを保存(最大ステップに達した際も保存)
            if (step+1)%FLAGS.checkpoint_step == 0 or (step+1) == FLAGS.max_steps:
                saver.save(sess, FLAGS.model_dir, global_step =step)
                
                # Evaluate model
                print('CheckPoint Train Evalation:')
                do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.train) 
                print('CheckPoint Valid Evalation:')
                do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.validation)
                print('CheckPoint Test Evalation:')
                do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.test)
コード例 #6
0
def run_training():
    data_sets = logistic_regression.load_data_mnist(one_hot=False)

    hiddens = [FLAGS.hidden1, FLAGS.hidden2]

    # Graph実行用のSession
    with tf.Graph().as_default():
        # placeholderの生成
        images_placeholder, labels_placeholder = ph.placeholder_inputs(
            FLAGS.batch_size, model.IMAGE_PIXELS, model.NUM_CLASSES)

        # logitsの生成
        logits = model.inference(images_placeholder, hiddens)

        # lossの生成
        loss = model.loss(logits, labels_placeholder)

        # trainオペレーションの生成
        train_op = model.train(loss, FLAGS.learning_rate)

        # evaluationオペレーション
        eval_correct = model.evaluation(logits, labels_placeholder)

        # 可視化用サマリ(グラフのビルド時に初期化)
        summary_op = tf.merge_all_summaries()

        # modelの保存用オブジェクト
        saver = tf.train.Saver()

        # sessionオブジェクト
        sess = tf.Session()

        # 変数の初期化
        init = tf.initialize_all_variables()
        sess.run(init)

        # 可視化用の変数初期化
        summary_writer = tf.train.SummaryWriter(FLAGS.model_dir,
                                                graph_def=sess.graph_def)

        # 学習ステップの実行
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()

            # 現在のステップ用のデータを取得
            feed_dict = fd.fill_feed_dict(data_sets.train, images_placeholder,
                                          labels_placeholder, FLAGS.batch_size,
                                          FLAGS.fake_data)

            # train_opを実行
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

            duration = time.time() - start_time

            # 100ステップごとにサマリーを出力
            if step % FLAGS.summary_step == 0:
                # stdout
                print('Step %d: loss = %.2f , %.3f sec' %
                      (step, loss_value, duration))

                # 可視化用の変数アップデート
                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)

            # 1000ステップごとにcheckpointを保存(最大ステップに達した際も保存)
            if (step + 1) % FLAGS.checkpoint_step == 0 or (
                    step + 1) == FLAGS.max_steps:
                saver.save(sess, FLAGS.model_dir, global_step=step)

                # Evaluate model
                print('CheckPoint Train Evalation:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.train)
                print('CheckPoint Valid Evalation:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.validation)
                print('CheckPoint Test Evalation:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.test)