コード例 #1
0
ファイル: unsupervised.py プロジェクト: rmkatti/surf_2012
    def run(self):
        imgs, labels = read_mnist(path=self.data_path, training=True)
        imgs = binarize_mnist_images(imgs)
        self.fit(imgs, labels)

        imgs, labels = read_mnist(path=self.data_path, training=False)
        imgs = binarize_mnist_images(imgs)
        self.coding_cost = -self.score(imgs, labels)
コード例 #2
0
ファイル: supervised.py プロジェクト: rmkatti/surf_2012
    def run(self):
        # Fit training data.
        imgs, labels = read_mnist(path=self.data_path, training=True)
        imgs = binarize_mnist_images(imgs)
        machines = self.fit(imgs, labels)

        # Compute error rate on test set.
        imgs, labels = read_mnist(path=self.data_path, training=False)
        idx = np.in1d(labels, machines.keys())
        imgs = binarize_mnist_images(imgs[idx])
        labels = labels[idx]
        predicted = self.predict(imgs)
        self.error_rate = np.sum(predicted != labels) / float(len(imgs))
コード例 #3
0
ファイル: specificity.py プロジェクト: rmkatti/surf_2012
    def run(self):
        # Validate settings.
        if len(self.digits) < 2:
            raise ValueError("Must use at least 2 digit classes")

        # Train the machine.
        imgs, labels = read_mnist(path=self.data_path, training=True)
        idx = np.in1d(labels, self.digits)
        imgs = binarize_mnist_images(imgs[idx])
        self.machine = machine = self.create_machine()
        self.train(machine, imgs)

        # Score the machine.
        imgs, labels = read_mnist(path=self.data_path, training=False)
        imgs = binarize_mnist_images(imgs)
        self.score(imgs, labels)
コード例 #4
0
ファイル: grid_search.py プロジェクト: rmkatti/surf_2012
    def run(self):
        imgs, labels = read_mnist(path=self.data_path, training=True)
        imgs = binarize_mnist_images(imgs)

        gs = GridSearchCV(self.base_runner, self.param_grid,
                          n_jobs = self.jobs, pre_dispatch = '2*n_jobs',
                          cv = self.cv, verbose = self.verbose)
        gs.fit(imgs, labels)

        self.param_scores = gs.grid_scores_
        self.best_params = gs.best_params_
        self.best_score = gs.best_score_
コード例 #5
0
def prepare_mnist(n_train):
    download_mnist()

    X, Y = read_mnist()
    X = image_to_vec(X)
    X = X / 255
    X = X.T
    Y = Y.T

    n_samples = X.shape[1]
    shuffle_idx = np.random.permutation(n_samples)
    X_shuffled = X[:, shuffle_idx]
    Y_shuffled = Y[:, shuffle_idx]

    X_train = X_shuffled[:, :n_train]
    Y_train = Y_shuffled[:, :n_train]
    X_test = X_shuffled[:, n_train:]
    Y_test = Y_shuffled[:, n_train:]

    Y_train = one_hot_encode(Y_train)

    return X_train, Y_train, X_test, Y_test
コード例 #6
0
from mnist import read_mnist
from sklearn.linear_model import LogisticRegression
import logging
logging.basicConfig(
    level=logging.DEBUG,
    format=
    '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a, %d %b %Y %H:%M:%S')

if __name__ == '__main__':
    train_image, train_label, test_image, test_label = read_mnist()
    clf = LogisticRegression()
    logging.info("Fiting")
    clf.fit(train_image, train_label)
    logging.info("Scoring")
    logging.info("Score: %lf\n", clf.score(test_image, test_label))
コード例 #7
0
def train():
    """Train CIFAR-10 for a number of steps."""
    data_sets = mnist.read_mnist(FLAGS.data_dir, FLAGS.fake_data)
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        images_placeholder, labels_placeholder = placeholder_inputs(
            FLAGS.batch_size)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = partnet.inference(images_placeholder, is_training=True)

        # Calculate loss.
        loss = partnet.loss(logits, labels_placeholder, sparse_weight=0.01)

        # Add the Op to compare the logits to the labels during evaluation.
        eval_correct = partnet.evaluation(logits, labels_placeholder)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = partnet.train(loss, global_step)

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables())

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        # Build an initialization operation to run below.
        init = tf.initialize_all_variables()

        # Start running operations on the Graph.
        sess = tf.Session()
        sess.run(init)

        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)

        for step in xrange(FLAGS.max_steps):
            start_time = time.time()

            feed_dict = fill_feed_dict(data_sets.train, images_placeholder,
                                       labels_placeholder)
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

            duration = time.time() - start_time

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step % 100 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                format_str = (
                    '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                    'sec/batch)')
                print(format_str % (datetime.now(), step, loss_value,
                                    examples_per_sec, sec_per_batch))

                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)
                summary_writer.flush()

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
                # Evaluate against the training set.
                print('Training Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.train)
                # Evaluate against the validation set.
                print('Validation Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.validation)
                # Evaluate against the test set.
                print('Test Data Eval:')
                do_eval(sess, eval_correct, images_placeholder,
                        labels_placeholder, data_sets.test)
        sess.close()  # free resource
コード例 #8
0
ファイル: gen_feat.py プロジェクト: dxmtb/ml-ocr
import numpy as np
from mnist import read_mnist
from sklearn.linear_model import LogisticRegression
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')


if __name__ == '__main__':
    w = np.load('theta.npy')
    train_image, train_label, test_image, test_label = read_mnist()
    clf = LogisticRegression()
    logging.info("Fiting")
    clf.fit(np.dot(train_image, w), train_label)
    logging.info("Scoring")
    logging.info("Score: %lf\n", clf.score(np.dot(test_image, w), test_label))
コード例 #9
0
ファイル: data_prep.py プロジェクト: AlessandroFumi/NNetSim
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 14 13:53:51 2017

@author: admin-congo
"""
import numpy as np
import mnist
import NNvars

full_dataset = [i for i in mnist.read_mnist("training")]

np.random.shuffle(full_dataset)
training_set = full_dataset[:NNvars.numSamples]
verification_set = full_dataset[NNvars.numSamples:NNvars.numVerification +
                                NNvars.numSamples]

test_set = [i for i in mnist.read_mnist("testing")]