コード例 #1
0
ファイル: prepare_rgb.py プロジェクト: kdung/sense_making
def train_minibatch():

    generator = MiniBatchGenerator(861, x_mapper, y_mapper)
    generator.split_train_test()
    """
    print('load train mini-batch')
    while True:
        X, y = generator.load_next_train_batch(10)
        if X is None:
            break
        print(y)
        X_to_train, y_to_train = preprocess(X, y)
        print(X_to_train.shape)
        print(y_to_train.shape)
        break
        
        sess, y_predict, x_train, y_train = models.train_nn(NUM_CATEGORIES,
                                                        X_to_train, y_to_train,
                                                        layers=(256, 256),
                                                        iterations=1000)

    print('load test mini-batch')
    while True:
        X, y = generator.load_next_test_batch(10)
        if X is None:
            break
        X_to_test, y_to_test = preprocess(X, y)
        correct_prediction = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y_train, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        print(sess.run(accuracy, feed_dict={x_train: X_to_test, y_train: y_to_test}))
        
    correct_prediction = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y_train, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    #print(sess.run(accuracy, feed_dict={x_train: X_to_train, y_train: y_to_train}))
    print(sess.run(accuracy, feed_dict={x_train: X_to_test, y_train: y_to_test}))
    """

    model = MLP(NUM_CATEGORIES,
                min_pc,
                layers=(256, 256, 256),
                learning_rate=0.0005)
    model.start_session()

    X_test, y_test = generator.load_next_test_batch(1000)
    X_test, y_test = preprocess(X_test, y_test)
    #sess = None
    for iteration in range(1000):
        generator.reset()
        while True:
            X_train, y_train = generator.load_next_train_batch(10)
            if X_train is None:
                break
            X_train, y_train = preprocess(X_train, y_train)
            model.train(X_train, y_train)
        if iteration % 100 == 0:
            print('loss = ', model.calculate_loss())
            # print('train acc = ', model.calculate_accuracy(X_train, y_train))
            print('test acc = ', model.calculate_accuracy(X_test, y_test))
            print("\n---\n")