示例#1
0
    def __init__(self, mat, cmap=None, pixelspervalue=20, minvalue=None, maxvalue=None, show=True, block=False):
        """ Make a colormap image of a matrix or sequence of Matrix/Connection objects

        :key mat: the matrix to be used for the colormap.
        :key cmap: the matplotlib colormap (color scale) to use ('hot', 'hot_r', 'gray', 'gray_r', 'hsv', 'prism', pylab.cm.hot, etc)
        """
        self.colormaps = []
        if isinstance(mat, basestring):
            try:
                #nn = NetworkReader(mat, newfile=False)
                mat = NetworkReader(mat, newfile=False).readFrom(mat)
            except:
                pass

        try:  # if isinstance(mat, Trainer):
            mat = mat.module
        except:
            pass

        if isinstance(mat, Network):
            # connections is a dict with key: value pairs of Layer: Connection (ParameterContainer)
            mat = [connection for connection in mat.connections.values() if connection]
        
            # connections = mat.module.connections.values()
            # mat = []
            # for conlist in connections:
            #     mat += conlist

        try:
            mat = [v for (k, v) in mat.iteritems()]
            if not any(isinstance(m, (ParameterContainer, Connection)) for m in mat):
                raise ValueError("Don't know how to display ColorMaps for a sequence of type {} containing key, values of type {}: {}".format(
                                 type(mat), *[type(m) for m in mat.iteritems().next()]))
        except AttributeError:
            pass
            # from traceback import print_exc
            # print_exc()
        if isinstance(mat, list):
            for m in mat:
                if isinstance(m, list):
                    if len(m) == 1:
                        m = m[0]
                    else:
                        raise ValueError("Don't know how to display a ColorMap for a list containing more than one matrix: {}".format([type(m) for m in mat]))
                try:
                    self.colormaps = [ColorMap(m, cmap=cmap, pixelspervalue=pixelspervalue, minvalue=minvalue, maxvalue=maxvalue) ]
                except ValueError:
                    self.colormaps = [ColorMap(m[0], cmap=cmap, pixelspervalue=pixelspervalue, minvalue=minvalue, maxvalue=maxvalue) ]
        else:
            self.colormaps = [ColorMap(mat)]
            # raise ValueError("Don't know how to display ColorMaps for a sequence of type {}".format(type(mat)))
        if show:
            self.show(block=block)
示例#2
0
文件: util.py 项目: nvaller/pug-ann
def weight_matrices(nn):
    """ Extract list of weight matrices from a Network, Layer (module), Trainer, Connection or other pybrain object"""

    if isinstance(nn, ndarray):
        return nn

    try:
        return weight_matrices(nn.connections)
    except:
        pass

    try:
        return weight_matrices(nn.module)
    except:
        pass

    # Network objects are ParameterContainer's too, but won't reshape into a single matrix,
    # so this must come after try nn.connections
    if isinstance(nn, (ParameterContainer, Connection)):
        return reshape(nn.params, (nn.outdim, nn.indim))

    if isinstance(nn, basestring):
        try:
            fn = nn
            nn = NetworkReader(fn, newfile=False)
            return weight_matrices(nn.readFrom(fn))
        except:
            pass
    # FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here

    try:
        return [weight_matrices(v) for (k, v) in nn.iteritems()]
    except:
        try:
            connections = nn.module.connections.values()
            nn = []
            for conlist in connections:
                nn += conlist
            return weight_matrices(nn)
        except:
            return [weight_matrices(v) for v in nn]
示例#3
0
def weight_matrices(nn):
    """ Extract list of weight matrices from a Network, Layer (module), Trainer, Connection or other pybrain object"""

    if isinstance(nn, ndarray):
        return nn

    try:
        return weight_matrices(nn.connections)
    except:
        pass

    try:
        return weight_matrices(nn.module)
    except:
        pass

    # Network objects are ParameterContainer's too, but won't reshape into a single matrix,
    # so this must come after try nn.connections
    if isinstance(nn, (ParameterContainer, Connection)):
        return reshape(nn.params, (nn.outdim, nn.indim))

    if isinstance(nn, basestring):
        try:
            fn = nn
            nn = NetworkReader(fn, newfile=False)
            return weight_matrices(nn.readFrom(fn))
        except:
            pass
    # FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here

    try:
        return [weight_matrices(v) for (k, v) in nn.iteritems()]
    except:
        try:
            connections = nn.module.connections.values()
            nn = []
            for conlist in connections:
                nn += conlist
            return weight_matrices(nn)
        except:
            return [weight_matrices(v) for v in nn]