Пример #1
0
    def apply_minibatches_function(self, f, X, Z):
        """Apply a function to batches of the input.

        The convolutional neural networks class needs the input to be the same
        size as the batch size. This function slices the input so that it
        can be processed correctly.
        If the batch size is not a divisor of the input size, an exception is
        raised.

        Parameters
        ----------

        f : Callable
            Function to use for all the batches.

        X : Numpy array
            Input of the function

        Z : numpy array
            Target of the function

        Returns
        -------

        exprs : The average of the results of the function over all the batches.
        """
        data = [minibatches(i, self.batch_size, d)
                for i, d in zip([X, Z], self.sample_dim)]
        if theano.config.device == 'gpu':
            total = [f(*element).asndarray()
                     for element in zip(data[0], data[1])]
        else:
            total = [f(*element) for element in zip(data[0], data[1])]
        return sum(total)/float(len(total))
Пример #2
0
def test_minibatches():
    """Test if minibatches are correctly generated if given a size."""
    D = np.random.random((13, 5))
    batches = minibatches(D, batch_size=5)
    assert batches[0].shape[0] == 5
    assert batches[1].shape[0] == 5
    assert batches[2].shape[0] == 3
Пример #3
0
def test_minibatches():
    """Test if minibatches are correctly generated if given a size."""
    D = np.random.random((13, 5))
    batches = minibatches(D, batch_size=5)
    assert batches[0].shape[0] == 5
    assert batches[1].shape[0] == 5
    assert batches[2].shape[0] == 3
Пример #4
0
Файл: cnn.py Проект: osdf/breze
    def apply_minibatches_function(self, f, X, Z):
        """Apply a function to batches of the input.

        The convolutional neural networks class needs the input to be the same
        size as the batch size. This function slices the input so that it can be
        processed correctly.
        If the batch size is not a divisor of the input size, an exception is
        raised.

        :param f: theano function
            Function to use for all the batches.

        :param X: numpy array
            Input of the function

        :param Z: numpy array
            Target of the function

        :returns: The average of the results of the function over all the batches.
        """
        data = [
            minibatches(i, self.batch_size, d)
            for i, d in zip([X, Z], self.sample_dim)
        ]
        total = [f(*element) for element in zip(data[0], data[1])]
        return sum(total) / float(len(total))
Пример #5
0
Файл: cnn.py Проект: osdf/breze
    def predict(self, X):
        """Override the predict function.

        :param X: numpy array
            Input
        :returns: The predictions of the network.
        """
        data = minibatches(X, self.batch_size, 0)
        if theano.config.device == 'gpu':
            raise NotImplementedError(
                'prediction not possible on gpu with conv net yet, please implement :)')
        total = np.concatenate([super(Cnn, self).predict(element) for element in data], axis=0)
        return total
Пример #6
0
def visualize_tsne(args):
    model_dir = os.path.abspath(args['<model>'])
    data_dir = os.path.abspath(args['<data>'])
    os.chdir(model_dir)
    cps = contrib.find_checkpoints('.')

    if cps:
        with gzip.open(cps[-1], 'rb') as fp:
                trainer = cPickle.load(fp)
                trainer.model.parameters.data[...] = trainer.best_pars
                data = h5.File(data_dir,'r')
                TX = data['test_set/test_set'][:5000]
                TZ = data['test_labels/real_test_labels'][:5000]
                TZ = one_hot(TZ,13)
                print 'data loaded.'

                if args['<mode>'] == 'cnn':
                    f_transformed = trainer.model.function(['inpt'],'mlp-layer-2-inpt')
                    print 'transform-function generated.'
                    data = minibatches(TX, trainer.model.batch_size, 0)
                    trans_TX = np.concatenate([f_transformed(element) for element in data], axis=0)
                else:
                    f_transformed = trainer.model.function(['inpt'],'layer-2-inpt')
                    print 'transform-function generated.'
                    trans_TX = f_transformed(TX)

                trans_TX = np.array(trans_TX, dtype=np.float32)
                print 'data transformed'
                trans_n_input = trans_TX.shape[1]
                trans_tsne = Tsne(trans_n_input, 2, perplexity=5)
                print 'TSNE initialized.'
                trans_TX_r = trans_tsne.fit_transform(trans_TX)
                print 'data TSNEd'

                fig = plt.figure(figsize=(16, 16))
                ax = fig.add_subplot(111)
                TZ_am = TZ.argmax(axis=1)
                ax.scatter(trans_TX_r[TZ_am==0, 0], trans_TX_r[TZ_am==0, 1], c='g', lw=0, alpha=1, s=100, marker='o')
                ax.scatter(trans_TX_r[TZ_am==1, 0], trans_TX_r[TZ_am==1, 1], c='b', lw=0, alpha=1, s=100, marker='v')
                ax.scatter(trans_TX_r[TZ_am==2, 0], trans_TX_r[TZ_am==2, 1], c='yellow', lw=0, alpha=1, s=100, marker='^')
                ax.scatter(trans_TX_r[TZ_am==3, 0], trans_TX_r[TZ_am==3, 1], c='r', lw=0, alpha=1, s=100, marker='<')
                ax.scatter(trans_TX_r[TZ_am==4, 0], trans_TX_r[TZ_am==4, 1], c='g', lw=0, alpha=1, s=100, marker='>')
                ax.scatter(trans_TX_r[TZ_am==5, 0], trans_TX_r[TZ_am==5, 1], c='m', lw=0, alpha=1, s=100, marker='8')
                ax.scatter(trans_TX_r[TZ_am==6, 0], trans_TX_r[TZ_am==6, 1], c='crimson', lw=0, alpha=1, s=100, marker='s')
                ax.scatter(trans_TX_r[TZ_am==7, 0], trans_TX_r[TZ_am==7, 1], c='lawngreen', lw=0, alpha=1, s=100, marker='p')
                ax.scatter(trans_TX_r[TZ_am==8, 0], trans_TX_r[TZ_am==8, 1], c='gold', lw=0, alpha=1, s=100, marker='*')
                ax.scatter(trans_TX_r[TZ_am==9, 0], trans_TX_r[TZ_am==9, 1], c='darkorange', lw=0, alpha=1, s=100, marker='h')
                ax.scatter(trans_TX_r[TZ_am==10, 0], trans_TX_r[TZ_am==10, 1], c='k', lw=0, alpha=1, s=100, marker='H')
                ax.scatter(trans_TX_r[TZ_am==11, 0], trans_TX_r[TZ_am==11, 1], c='magenta', lw=0, alpha=1, s=100, marker='d')
                ax.scatter(trans_TX_r[TZ_am==12, 0], trans_TX_r[TZ_am==12, 1], c='turquoise', lw=0, alpha=1, s=100, marker='D')
                plt.savefig(os.path.join('/nthome/maugust/thesis',args['<output>']))
Пример #7
0
Файл: cnn.py Проект: osdf/breze
    def predict(self, X):
        """Override the predict function.

        :param X: numpy array
            Input
        :returns: The predictions of the network.
        """
        data = minibatches(X, self.batch_size, 0)
        if theano.config.device == 'gpu':
            raise NotImplementedError(
                'prediction not possible on gpu with conv net yet, please implement :)'
            )
        total = np.concatenate(
            [super(Cnn, self).predict(element) for element in data], axis=0)
        return total
 def iter_minibatches(self, lst, batch_size, dims, n_cycles=False, random_state=None):
     batches = [minibatches(i, batch_size, d) for i, d in zip(lst, dims)]
     if len(batches) > 1:
         if any(len(i) != len(batches[0]) for i in batches[1:]):
             raise ValueError("containers to be batched have different lengths")
         counter = itertools.count()
         if random_state is not None:
             random.seed(random_state.normal())
         while True:
             indices = [i for i, _ in enumerate(batches[0])]
             while True:
                 random.shuffle(indices)
                 for i in indices:
                     yield (self.transformedData(batches[0][i]), batches[1][i])
                     count = counter.next()
                 if n_cycles and count >= n_cycles:
                     raise StopIteration()
Пример #9
0
    def predict(self, X):
        """Override the predict function.

        Parameters
        ----------

        X : Numpy array
            Input of the function

        Returns
        -------

        exprs : The predictions of the network
        """
        data = minibatches(X, self.batch_size, 0)
        total = np.concatenate([super(Cnn, self).predict(element)
                                for element in data], axis=0)
        return total
Пример #10
0
Файл: cnn.py Проект: osdf/breze
    def apply_minibatches_function(self, f, X, Z):
        """Apply a function to batches of the input.

        The convolutional neural networks class needs the input to be the same
        size as the batch size. This function slices the input so that it can be
        processed correctly.
        If the batch size is not a divisor of the input size, an exception is
        raised.

        :param f: theano function
            Function to use for all the batches.

        :param X: numpy array
            Input of the function

        :param Z: numpy array
            Target of the function

        :returns: The average of the results of the function over all the batches.
        """
        data = [minibatches(i, self.batch_size, d) for i, d in zip([X, Z], self.sample_dim)]
        total = [f(*element) for element in zip(data[0], data[1])]
        return sum(total) / float(len(total))
Пример #11
0
 def iter_minibatches(self,
                      lst,
                      batch_size,
                      dims,
                      n_cycles=False,
                      random_state=None):
     batches = [minibatches(i, batch_size, d) for i, d in zip(lst, dims)]
     if len(batches) > 1:
         if any(len(i) != len(batches[0]) for i in batches[1:]):
             raise ValueError(
                 "containers to be batched have different lengths")
         counter = itertools.count()
         if random_state is not None:
             random.seed(random_state.normal())
         while True:
             indices = [i for i, _ in enumerate(batches[0])]
             while True:
                 random.shuffle(indices)
                 for i in indices:
                     yield (self.transformedData(batches[0][i]),
                            batches[1][i])
                     count = counter.next()
                 if n_cycles and count >= n_cycles:
                     raise StopIteration()