def loadWeights(self, filename, mode='pickle'):
        """
        Loads weights from a file in pickle, plain, or tlearn mode.
        """
        # modes: pickle, plain/conx, tlearn
        if mode == 'pickle':
            import pickle
            fp = open(filename, "r")
            mylist = pickle.load(fp)
            fp.close()
            self.unArrayify(mylist)
        elif mode in ['plain', 'conx']:
            arr = []
            fp = open(filename, "r")
            lines = fp.readlines()
            for line in lines:
                line = line.strip()
                if line == '' or line[0] == '#':
                    pass
                else:
                    data = list(map( float, line.split()))
                    arr.extend( data )
            self.unArrayify( arr )
            fp.close()
        elif mode == 'tlearn':
            fp = open(filename, "r")
            fp.readline() # NETWORK CONFIGURED BY
            fp.readline() # # weights after %d sweeps
            fp.readline() # # WEIGHTS
            cnt = 1
            for lto in self.layers:
                if lto.type != 'Input':
                    for j in range(lto.size):
                        fp.readline() # TO NODE %d
                        lto.weight[j] = float(fp.readline())
                        for lfrom in self.layers:
                            try:
                                connection = self.getConnection(lfrom.name, lto.name)
                                for i in range(connection.fromLayer.size):
                                    connection.weight[i][j] = float( fp.readline() )
                            except NetworkError:
                                for i in range(lfrom.size):
                                    # 0.0
                                    fp.readline()
                        cnt += 1
            fp.close()            
        elif mode == 'nbench':
            # reads weights and constructs network
            fp = open(filename, "r")
            line = fp.readline()
            while line[:8] != "Ninputs:":
                line = fp.readline()
            itxt, inputs, ttxt, total, otxt, outputs = line.split()
            inputs, total, outputs = int(inputs), int(total), int(outputs)
            # cascor's total value is the bias + inputs + hiddens
            hiddens = total - inputs - 1
            # create the layers:
            self.addLayer("input", inputs)
            for i in range(hiddens):
                self.addLayer("hidden%d" % i, 1)
            self.addLayer("output", outputs)
            # connect input to all hiddens, output:
            for i in range(hiddens):
                self.connect("input", "hidden%d" % i)
            self.connect("input", "output")
            # connect all hiddens to all later hiddens:
            for i in range(hiddens - 1):
                for j in range(i + 1, hiddens):
                    if j != i:
                        self.connect("hidden%d" % i, "hidden%d" % j )
            # connect all hiddens to outputs:
            for i in range(hiddens):
                self.connect("hidden%d" % i, "output")
            # now, let's set the weights:
            for outcount in range(outputs):
                while line[:9] != "# Output:":
                    line = fp.readline()
                line = fp.readline() # $
                line = fp.readline() # bias, input to output, hidden to output?
                data = ""
                while line and line[0] != "#":
                    data += " " + line.strip()
                    line = fp.readline()
                weights = list(map(float, data.split()))
                self["output"].weight[outcount] = weights[0] # bias
                next = 1
                for i in range(self["input"].size):
                    self["input", "output"].weight[i][outcount] = weights[next]
                    next += 1
                for h in range(hiddens):
                    for i in range(self["hidden%d" % h].size): # normally just 1
                        self["hidden%d" % h, "output"].weight[i][outcount] = weights[next]
                        next += 1
            # now, for each hidden "layer":
            while line and line[0] != "$":
                line = fp.readline()
            line = fp.readline()
            for hidcount in range(hiddens):
                weights = []
                while line and line[0] != "$" and line[0] != "#": # next line is a weight line
                    weights.extend( list(map(float, line.split()))) # bias, input to hidden, hidden to hidden?
                    line = fp.readline() 
                self[("hidden%d" % hidcount)].weight[0] = weights[0] # bias first
                next = 1
                for i in range(self["input"].size):
                    for j in range(self["hidden%d" % hidcount].size): # normally just 1
                        self["input", ("hidden%d" % hidcount)].weight[i][j] = weights[next]
                        next += 1
                for h in range(hidcount): # all those hids leading up to this one
                    for i in range(self["hidden%d" % h].size): # normally just 1
                        for j in range(self["hidden%d" % hidcount].size): # normally just 1
                            self[("hidden%d" % h), ("hidden%d" % hidcount)].weight[i][j] = weights[next]
                            next += 1
                line = fp.readline() # $
                line = fp.readline() # beginning of weights
        else:
            raise ValueError('Unknown mode in loadWeights()', mode)
def loadWeights ( self , filename , mode = 'pickle' ) : if mode == 'pickle' : import pickle fp = open ( filename , "r" ) mylist = pickle . load ( fp ) fp . close ( ) self . unArrayify ( mylist ) elif mode in [ 'plain' , 'conx' ] : arr = [ ] fp = open ( filename , "r" ) lines = fp . readlines ( ) for line in lines : line = line . strip ( ) if line == '' or line [ 0 ] == '#' : pass else : data = list ( map ( float , line . split ( ) ) ) arr . extend ( data ) self . unArrayify ( arr ) fp . close ( ) elif mode == 'tlearn' : fp = open ( filename , "r" ) fp . readline ( ) fp . readline ( ) fp . readline ( ) cnt = 1 for lto in self . layers : if lto . type != 'Input' : for j in range ( lto . size ) : fp . readline ( ) lto . weight [ j ] = float ( fp . readline ( ) ) for lfrom in self . layers : try : connection = self . getConnection ( lfrom . name , lto . name ) for i in range ( connection . fromLayer . size ) : connection . weight [ i ] [ j ] = float ( fp . readline ( ) ) except NetworkError : for i in range ( lfrom . size ) : fp . readline ( ) cnt += 1 fp . close ( ) elif mode == 'nbench' : fp = open ( filename , "r" ) line = fp . readline ( ) while line [ : 8 ] != "Ninputs:" : line = fp . readline ( ) itxt , inputs , ttxt , total , otxt , outputs = line . split ( ) inputs , total , outputs = int ( inputs ) , int ( total ) , int ( outputs ) hiddens = total - inputs - 1 self . addLayer ( "input" , inputs ) for i in range ( hiddens ) : self . addLayer ( "hidden%d" % i , 1 ) self . addLayer ( "output" , outputs ) for i in range ( hiddens ) : self . connect ( "input" , "hidden%d" % i ) self . connect ( "input" , "output" ) for i in range ( hiddens - 1 ) : for j in range ( i + 1 , hiddens ) : if j != i : self . connect ( "hidden%d" % i , "hidden%d" % j ) for i in range ( hiddens ) : self . connect ( "hidden%d" % i , "output" ) for outcount in range ( outputs ) : while line [ : 9 ] != "# Output:" : line = fp . readline ( ) line = fp . readline ( ) line = fp . readline ( ) data = "" while line and line [ 0 ] != "#" : data += " " + line . strip ( ) line = fp . readline ( ) weights = list ( map ( float , data . split ( ) ) ) self [ "output" ] . weight [ outcount ] = weights [ 0 ] next = 1 for i in range ( self [ "input" ] . size ) : self [ "input" , "output" ] . weight [ i ] [ outcount ] = weights [ next ] next += 1 for h in range ( hiddens ) : for i in range ( self [ "hidden%d" % h ] . size ) : self [ "hidden%d" % h , "output" ] . weight [ i ] [ outcount ] = weights [ next ] next += 1 while line and line [ 0 ] != "$" : line = fp . readline ( ) line = fp . readline ( ) for hidcount in range ( hiddens ) : weights = [ ] while line and line [ 0 ] != "$" and line [ 0 ] != "#" : weights . extend ( list ( map ( float , line . split ( ) ) ) ) line = fp . readline ( ) self [ ( "hidden%d" % hidcount ) ] . weight [ 0 ] = weights [ 0 ] next = 1 for i in range ( self [ "input" ] . size ) : for j in range ( self [ "hidden%d" % hidcount ] . size ) : self [ "input" , ( "hidden%d" % hidcount ) ] . weight [ i ] [ j ] = weights [ next ] next += 1 for h in range ( hidcount ) : for i in range ( self [ "hidden%d" % h ] . size ) : for j in range ( self [ "hidden%d" % hidcount ] . size ) : self [ ( "hidden%d" % h ) , ( "hidden%d" % hidcount ) ] . weight [ i ] [ j ] = weights [ next ] next += 1 line = fp . readline ( ) line = fp . readline ( ) else : raise ValueError ( 'Unknown mode in loadWeights()' , mode )
Loads weights from a file in pickle, plain, or tlearn mode.