예제 #1
0
파일: train.py 프로젝트: se-p-93/MDR
def load_data():
    """Loads data"""
    x_train = preprocessing.get_x("data/train")
    y_train = preprocessing.get_y1("data/train")
    x_test = preprocessing.get_x("data/test")
    y_test = preprocessing.get_y1("data/test")
    return x_train, y_train, x_test, y_test
def accuracy_calculate(x, y, L):
    x = preprocessing.get_x("data/train")
    y = preprocessing.get_y1("data/train")
    x = x[y[:, 0] == L]
    y = y[y[:, 0] == L]
    indices = np.random.choice(len(x), 2000, replace=False)
    examples = x[indices]
    answers = y[indices]

    model = Model()

    with tf.Session(graph=model.graph) as session:
        # Restore model with 82% accuracy on test data
        model.saver.restore(session, "./try2.ckpt")
        log_1, log_2, log_3, log_4, log_5 = session.run([
            model.logits_1, model.logits_2, model.logits_3, model.logits_4,
            model.logits_5
        ],
                                                        feed_dict={
                                                            model.x: examples,
                                                            model.keep_prob:
                                                            4.0
                                                        })
    # Make predictions
    predictions = predictions(log_1, log_2, log_3, log_4, log_5)

    acc = 0.0
    r1 = 0.0
    r2 = 0.0
    for i, el in enumerate(indices):
        number = predictions[i][predictions[i] < 10]
        ans = answers[i][answers[i] < 10]

        if len(ans[1:]) != len(number):
            r1 += 1.0
            continue
        if not (ans[1:] - number).any():
            acc += 1.0
        else:
            r2 += 1.0
            try:
                ans = int(''.join(ans[1:]))
                number = int(''.join(number))
                if ans == number:
                    acc += 1.0
            except:
                pass
    fac = len(x) / 100.0
    print('acc:%f' % (acc / fac))
    print('r1:%f' % (r1 / fac))
    print('r2:%f' % (r2 / fac))
예제 #3
0
파일: examples.py 프로젝트: se-p-93/MDR
"""Try to predict 7 random examples from test data"""

import scipy.misc
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

import preprocessing
from model import Model
from train import predictions

x = preprocessing.get_x("data/test")
indices = np.random.choice(len(x), 7)
examples = x[indices]
model = Model()

with tf.Session(graph=model.graph) as session:

    # Restore model with 82% accuracy on test data
    model.saver.restore(session, "./try2.ckpt")

    log_1, log_2, log_3, log_4, log_5 = session.run([
        model.logits_1, model.logits_2, model.logits_3, model.logits_4,
        model.logits_5
    ],
                                                    feed_dict={
                                                        model.x: examples,
                                                        model.keep_prob: 1.0
                                                    })

# Make predictions on 7 random examples from test data
def ckpt_accuracy(L, ckpt, cut=True):
    x = preprocessing.get_x("data/test")
    y = preprocessing.get_y1("data/test")
    x = x[y[:, 0] == L]
    y = y[y[:, 0] == L]

    if len(x) > 2000:
        indices = np.random.choice(len(x), 2000, replace=False)
        examples = x[indices]
        answers = y[indices]

    else:
        examples = x
        answers = y

    model = Model()

    with tf.Session(graph=model.graph) as session:
        # Restore model with filename as ckpt
        model.saver.restore(session, "./" + ckpt)
        log_1, log_2, log_3, log_4, log_5 = session.run([
            model.logits_1, model.logits_2, model.logits_3, model.logits_4,
            model.logits_5
        ],
                                                        feed_dict={
                                                            model.x: examples,
                                                            model.keep_prob:
                                                            4.0
                                                        })
    # Make predictions
    preds = predictions(log_1, log_2, log_3, log_4, log_5)

    #cut the digit longer than L
    if cut:
        preds = preds[:, :L]

    #r1 is first type error that predict wrong length
    #r2 is second type error that predict wrong number
    acc = 0.0
    r1 = 0.0
    r2 = 0.0
    for i, el in enumerate(indices):
        number = preds[i][preds[i] < 10]
        ans = answers[i][answers[i] < 10]

        if len(ans[1:]) != len(number):
            r1 += 1.0
            continue
        if not (ans[1:] - number).any():
            acc += 1.0
        else:
            r2 += 1.0
            try:
                ans = int(''.join(ans[1:]))
                number = int(''.join(number))
                if ans == number:
                    acc += 1.0
            except:
                pass
    fac = len(examples) / 100.0
    print('ckpt_acc:%f' % (acc / fac))
    print('r1:%f' % (r1 / fac))
    print('r2:%f' % (r2 / fac))
    return acc, r1, r2