brainstorm/tools.py
def extract_and_save(network, iter, buffer_names, file_name):
    """Save the desired buffer values of a network to an HDF5 file.

    In particular, this tool can be used to save the predictions of a
    network on a dataset.
    In general, any number of internal, input or output buffers of the network
    can be extracted.

    Examples:
        >>> getter = Minibatches(100, default=x_test)
        >>> extract_and_save(network,
        ...                  getter,
        ...                  ['Output.outputs.predictions',
        ...                   'Hid1.internals.H'],
        ...                  'network_features.hdf5')
    Args:
        network (brainstorm.structure.Network):
            Network using which the features should be generated.
        iter (brainstorm.DataIterator):
            A data iterator which produces the data on which the features are
            computed.
        buffer_names (list[unicode]):
            Name of the buffer views to be saved (in dotted notation).
            See example.
        file_name (unicode):
            Name of the hdf5 file (including extension) in which the features
            should be saved.
    """
    iterator = iter(handler=network.handler)
    if isinstance(buffer_names, six.string_types):
        buffer_names = [buffer_names]
    nr_items = 0
    ds = []

    with h5py.File(file_name, 'w') as f:
        f.attrs.create('info', get_brainstorm_info())
        f.attrs.create('format', b'Buffers file v1.0')

        for _ in run_network(network, iterator, all_inputs=False):
            network.forward_pass()
            first_pass = False if len(ds) > 0 else True
            for num, buffer_name in enumerate(buffer_names):
                data = network.get(buffer_name)
                if num == 0:
                    nr_items += data.shape[1]
                if first_pass:
                    ds.append(f.create_dataset(
                        buffer_name, data.shape, data.dtype, chunks=data.shape,
                        maxshape=(data.shape[0], None) + data.shape[2:]))
                    ds[-1][:] = data
                else:
                    ds[num].resize(size=nr_items, axis=1)
                    ds[num][:, nr_items - data.shape[1]:nr_items, ...] = data
Save the desired buffer values of a network to an HDF5 file.

    In particular, this tool can be used to save the predictions of a
    network on a dataset.
    In general, any number of internal, input or output buffers of the network
    can be extracted.

    Examples:
        >>> getter = Minibatches(100, default=x_test)
        >>> extract_and_save(network,
        ...                  getter,
        ...                  ['Output.outputs.predictions',
        ...                   'Hid1.internals.H'],
        ...                  'network_features.hdf5')
    Args:
        network (brainstorm.structure.Network):
            Network using which the features should be generated.
        iter (brainstorm.DataIterator):
            A data iterator which produces the data on which the features are
            computed.
        buffer_names (list[unicode]):
            Name of the buffer views to be saved (in dotted notation).
            See example.
        file_name (unicode):
            Name of the hdf5 file (including extension) in which the features
            should be saved.
    
Save the desired buffer values of a network to an HDF5 file.
In particular, this tool can be used to save the predictions of a
network on a dataset.
In general, any number of internal, input or output buffers of the network
can be extracted.
Save the desired buffer values of a network to an HDF5 file . In particular this tool can be used to save the predictions of a network on a dataset . In general any number of internal input or output buffers of the network can be extracted .
def extract_and_save(network, iter, buffer_names, file_name):
    iterator = iter(handler=network.handler)
    if isinstance(buffer_names, six.string_types):
        buffer_names = [buffer_names]
    nr_items = 0
    ds = []
    with h5py.File(file_name, 'w') as f:
        f.attrs.create('info', get_brainstorm_info())
        f.attrs.create('format', b'Buffers file v1.0')
        for _ in run_network(network, iterator, all_inputs=False):
            network.forward_pass()
            first_pass = False if len(ds) > 0 else True
            for num, buffer_name in enumerate(buffer_names):
                data = network.get(buffer_name)
                if num == 0:
                    nr_items += data.shape[1]
                if first_pass:
                    ds.append(f.create_dataset(
                        buffer_name, data.shape, data.dtype, chunks=data.shape,
                        maxshape=(data.shape[0], None) + data.shape[2:]))
                    ds[-1][:] = data
                else:
                    ds[num].resize(size=nr_items, axis=1)
                    ds[num][:, nr_items - data.shape[1]:nr_items, ...] = data
def extract_and_save ( network , iter , buffer_names , file_name ) : iterator = iter ( handler = network . handler ) if isinstance ( buffer_names , six . string_types ) : buffer_names = [ buffer_names ] nr_items = 0 ds = [ ] with h5py . File ( file_name , 'w' ) as f : f . attrs . create ( 'info' , get_brainstorm_info ( ) ) f . attrs . create ( 'format' , b'Buffers file v1.0' ) for _ in run_network ( network , iterator , all_inputs = False ) : network . forward_pass ( ) first_pass = False if len ( ds ) > 0 else True for num , buffer_name in enumerate ( buffer_names ) : data = network . get ( buffer_name ) if num == 0 : nr_items += data . shape [ 1 ] if first_pass : ds . append ( f . create_dataset ( buffer_name , data . shape , data . dtype , chunks = data . shape , maxshape = ( data . shape [ 0 ] , None ) + data . shape [ 2 : ] ) ) ds [ - 1 ] [ : ] = data else : ds [ num ] . resize ( size = nr_items , axis = 1 ) ds [ num ] [ : , nr_items - data . shape [ 1 ] : nr_items , ... ] = data
Save the desired buffer values of a network to an HDF5 file.
Save the desired buffer values of a network to an HDF5 file .
"""Save the desired buffer values of a network to an HDF5 file.

    In particular, this tool can be used to save the predictions of a
    network on a dataset.
    In general, any number of internal, input or output buffers of the network
    can be extracted.

    Examples:
        >>> getter = Minibatches(100, default=x_test)
        >>> extract_and_save(network,
        ...                  getter,
        ...                  ['Output.outputs.predictions',
        ...                   'Hid1.internals.H'],
        ...                  'network_features.hdf5')
    Args:
        network (brainstorm.structure.Network):
            Network using which the features should be generated.
        iter (brainstorm.DataIterator):
            A data iterator which produces the data on which the features are
            computed.
        buffer_names (list[unicode]):
            Name of the buffer views to be saved (in dotted notation).
            See example.
        file_name (unicode):
            Name of the hdf5 file (including extension) in which the features
            should be saved.
    """
network
Network using which the features should be generated.
Network using which the features should be generated .
iter
A data iterator which produces the data on which the features are
computed.
A data iterator which produces the data on which the features are computed .
buffer_names
Name of the buffer views to be saved (in dotted notation).
See example.
Name of the buffer views to be saved ( in dotted notation ) . See example .
file_name
Name of the hdf5 file (including extension) in which the features
should be saved.
Name of the hdf5 file ( including extension ) in which the features should be saved .
examples
>>> getter = Minibatches(100, default=x_test)
>>> extract_and_save(network,
getter,
['Output.outputs.predictions',
'Hid1.internals.H'],
'network_features.hdf5')
>>> getter = Minibatches ( 100 default = x_test ) >>> extract_and_save ( network getter [ ' Output . outputs . predictions ' ' Hid1 . internals . H ' ] ' network_features . hdf5 ' )