Пример #1
0
def view(fname,which_pics=None):
    import pylab
    import math
    try:
        var=zpickle.load(fname)
    except TypeError:
        var=fname
        
    
    gray=pylab.cm.gray

    total_num_pics=len(var['im'])
    
    if which_pics:
        var['im']=[var['im'][i] for i in which_pics]
    else:
        which_pics=range(len(var['im']))
    
    num_pics=len(var['im'])
    c=math.ceil(math.sqrt(num_pics))
    r=math.ceil(num_pics/c)
    
    for i in range(num_pics):
        pylab.subplot(r,c,i+1)
        pylab.pcolor(var['im'][i],cmap=pylab.cm.gray)
        pylab.hold(False)
        pylab.axis('equal')
#        pylab.imshow(var['im'][i],cmap=gray,aspect='preserve')
        pylab.axis('off')
        if i==0:
            pylab.title('%dx%dx%d' % (var['im'][0].shape[0],var['im'][0].shape[1],total_num_pics))
        else:
            pylab.title('%d' % which_pics[i])
        
    pylab.show()
Пример #2
0
    def Save_Simulation_As(self,event=None):
        
        self.canvas.Show(False)
        dlg = FileDialog(self, "Save Simulation As...",default_dir=os.getcwd()+"/sims/",
                        wildcard='DAT Files|*.dat|All Files|*.*',save=1)
        result = dlg.ShowModal()
        if result == 'ok':
            filename = dlg.GetPaths()[0]
        else:
            filename=None
            
        dlg.Destroy()

            
        if filename:
            d=zpickle.load(self.tmpfile)
            self.params['save_sim_file']=filename
            d['params']=self.params
            zpickle.save(d,filename)
            
            self.modified=False
            
            self.ResetTitle()
        
        self.canvas.Show(True)
            
        return filename
Пример #3
0
    def __load_sim__(self, lfname):

        sim = zpickle.load(lfname)

        params = sim['params']

        params['save_sim_file'] = self.params['save_sim_file']
        params['load_sim_file'] = ''
        params['continue'] = False

        try:
            params['initial_weights'] = sim['weights']
            params['initial_moments'] = sim['moments']
        except KeyError:
            self.params = params
            return 1

        params['load_sim_file'] = self.tmpfile
        params['continue'] = True
        sim['params'] = params
        self.params = params

        zpickle.save(sim, self.tmpfile)

        return 0
Пример #4
0
    def Save_Simulation_As(self, event=None):

        self.canvas.Show(False)
        dlg = FileDialog(self,
                         "Save Simulation As...",
                         default_dir=os.getcwd() + "/sims/",
                         wildcard='DAT Files|*.dat|All Files|*.*',
                         save=1)
        result = dlg.ShowModal()
        if result == 'ok':
            filename = dlg.GetPaths()[0]
        else:
            filename = None

        dlg.Destroy()

        if filename:
            d = zpickle.load(self.tmpfile)
            self.params['save_sim_file'] = filename
            d['params'] = self.params
            zpickle.save(d, filename)

            self.modified = False

            self.ResetTitle()

        self.canvas.Show(True)

        return filename
Пример #5
0
    def Body(self):

        self.CreateMenu()
        self.CenterOnScreen()

        self.ResetTitle()

        fname = self.base_dir + "/images/plasticity_small_icon.ico"
        self.SetIcon(fname)

        self.fig = Figure(figsize=(7, 5), dpi=100)
        self.canvas = FigureCanvas(self, -1, self.fig)
        self.figmgr = FigureManager(self.canvas, 1, self)

        self.axes = [
            self.fig.add_subplot(221),
            self.fig.add_subplot(222),
            self.fig.add_subplot(223),
            self.fig.add_subplot(224)
        ]

        if self.plot_first:
            sim = zpickle.load(self.tmpfile)
            sim['params']['display'] = True
            self.Plot(sim)
Пример #6
0
    def Load_Simulation(self,event=None):

        self.canvas.Show(False)
        if self.modified:
            (root,sfname)=os.path.split(self.params['save_sim_file'])
            dlg=MessageDialog(self, 
                          text="Do you want to save the changes you made to %s?" % sfname,
                          title="Load Simulation", ok=0, yes_no=1,cancel=1)
            result=dlg.ShowModal()
            dlg.Destroy()
            
            if result == 'cancel':
                pass
            elif result == 'yes':
                self.Save_Simulation()
                
        lfname=''    
        dlg = FileDialog(self, "Load Simulation",default_dir=os.getcwd()+"/sims",
                        wildcard='DAT Files|*.dat|All Files|*.*')
        result = dlg.ShowModal()
        if result == 'ok':
            lfname = dlg.GetPaths()[0]
        dlg.Destroy()

        if not lfname:
            self.canvas.Show(True)
            return
        
        self.__load_sim__(lfname)
            
        sim=zpickle.load(self.tmpfile)
        self.Plot(sim)
        self.canvas.Show(True)
Пример #7
0
    def __load_sim__(self,lfname):
        
        sim=zpickle.load(lfname)

        params=sim['params']
        
        params['save_sim_file']=self.params['save_sim_file']
        params['load_sim_file']=''
        params['continue']=False
        
        try:
            params['initial_weights']=sim['weights']
            params['initial_moments']=sim['moments']
        except KeyError:
            self.params=params
            return 1

        params['load_sim_file']=self.tmpfile
        params['continue']=True
        sim['params']=params
        self.params=params
        
        zpickle.save(sim,self.tmpfile)
        
        return 0
Пример #8
0
def load_params(fname):
    
    d=zpickle.load(fname)
    
    if 'epoch_number' in d:
        return d
    else:
        return d['params']
Пример #9
0
def load_params(fname):

    d = zpickle.load(fname)

    if 'epoch_number' in d:
        return d
    else:
        return d['params']
Пример #10
0
    def Click(self, event):

        file = event.GetString()

        if file:

            self.SetCursor('arrowwait')
            self.plot_frame.SetStatusText('Plotting %s...' % file)
            self.UpdateWindowUI()
            self.Refresh()
            sim = zpickle.load(file)
            self.plot_frame.params = sim['params']
            self.plot_frame.Plot(sim)
            self.SetCursor('default')
            self.plot_frame.SetStatusText('Plotting %s...done.' % file)
Пример #11
0
 def Click(self,event):
 
     
     file=event.GetString()
     
     if file:
         
         self.SetCursor('arrowwait')
         self.plot_frame.SetStatusText('Plotting %s...' % file)
         self.UpdateWindowUI()
         self.Refresh()
         sim=zpickle.load(file)
         self.plot_frame.params=sim['params']
         self.plot_frame.Plot(sim)
         self.SetCursor('default')
         self.plot_frame.SetStatusText('Plotting %s...done.' % file)
Пример #12
0
    def Display(self,event=None):

        self.canvas.Show(False)
        dlg = FileDialog(self, "Choose Display Module",default_dir=os.getcwd()+"/",
                        wildcard='Python Plot Files|plot*.py|All Files|*.*')
        result = dlg.ShowModal()
        dlg.Destroy()

        if result == 'ok':
            lfname = dlg.GetPaths()[0]
            modulename=os.path.splitext(os.path.split(lfname)[-1])[0]

            self.params['display_module']=modulename
            
            if os.path.exists(self.tmpfile):
                sim=zpickle.load(self.tmpfile)
                self.Plot(sim)
                
            
        self.canvas.Show(True)
Пример #13
0
    def Save_Simulation(self, event=None):

        if not self.modified:
            return

        sfname = self.params['save_sim_file']
        def_sfname = default_params()['save_sim_file']
        if sfname == def_sfname:
            filename = self.Save_Simulation_As()

        else:
            filename = sfname
            d = zpickle.load(self.tmpfile)
            d['params'] = self.params
            zpickle.save(d, sfname)

            self.modified = False

            self.ResetTitle()

        return filename
Пример #14
0
 def Body(self):
     
     self.CreateMenu()
     self.CenterOnScreen()
     
     self.ResetTitle()
     
     fname=self.base_dir+"/images/plasticity_small_icon.ico"
     self.SetIcon(fname)
     
     self.fig = Figure(figsize=(7,5),dpi=100)
     self.axes = [self.fig.add_subplot(221),
                 self.fig.add_subplot(222),
                 self.fig.add_subplot(223),
                 self.fig.add_subplot(224)]
                 
     self.canvas = FigureCanvas(self, -1, self.fig)
     
     if self.plot_first:
         sim=zpickle.load(self.tmpfile)
         self.Plot(sim)
Пример #15
0
    def Display(self, event=None):

        self.canvas.Show(False)
        dlg = FileDialog(self,
                         "Choose Display Module",
                         default_dir=os.getcwd() + "/",
                         wildcard='Python Plot Files|plot*.py|All Files|*.*')
        result = dlg.ShowModal()
        dlg.Destroy()

        if result == 'ok':
            lfname = dlg.GetPaths()[0]
            modulename = os.path.splitext(os.path.split(lfname)[-1])[0]

            self.params['display_module'] = modulename

            if os.path.exists(self.tmpfile):
                sim = zpickle.load(self.tmpfile)
                self.Plot(sim)

        self.canvas.Show(True)
Пример #16
0
    def Load_Simulation(self, event=None):

        self.canvas.Show(False)
        if self.modified:
            (root, sfname) = os.path.split(self.params['save_sim_file'])
            dlg = MessageDialog(
                self,
                text="Do you want to save the changes you made to %s?" %
                sfname,
                title="Load Simulation",
                ok=0,
                yes_no=1,
                cancel=1)
            result = dlg.ShowModal()
            dlg.Destroy()

            if result == 'cancel':
                pass
            elif result == 'yes':
                self.Save_Simulation()

        lfname = ''
        dlg = FileDialog(self,
                         "Load Simulation",
                         default_dir=os.getcwd() + "/sims",
                         wildcard='DAT Files|*.dat|All Files|*.*')
        result = dlg.ShowModal()
        if result == 'ok':
            lfname = dlg.GetPaths()[0]
        dlg.Destroy()

        if not lfname:
            self.canvas.Show(True)
            return

        self.__load_sim__(lfname)

        sim = zpickle.load(self.tmpfile)
        self.Plot(sim)
        self.canvas.Show(True)
Пример #17
0
    def Save_Simulation(self,event=None):
        
        if not self.modified:
            return
        
        sfname=self.params['save_sim_file']
        def_sfname=default_params()['save_sim_file']
        if sfname==def_sfname:
            filename=self.Save_Simulation_As()
            
            
        else:
            filename=sfname
            d=zpickle.load(self.tmpfile)
            d['params']=self.params
            zpickle.save(d,sfname)
            
            self.modified=False
            
            self.ResetTitle()

        return filename
Пример #18
0
def view(fname, which_pics=None):
    import pylab
    import math
    try:
        var = zpickle.load(fname)
    except TypeError:
        var = fname

    pylab.figure()
    pylab.show()
    gray = pylab.cm.gray

    total_num_pics = len(var['im'])

    if which_pics:
        var['im'] = [var['im'][i] for i in which_pics]
    else:
        which_pics = range(len(var['im']))

    num_pics = len(var['im'])
    c = math.ceil(math.sqrt(num_pics))
    r = math.ceil(num_pics / c)

    for i in range(num_pics):
        pylab.subplot(r, c, i + 1)
        pylab.imshow(var['im'][i], cmap=pylab.cm.gray)
        #pylab.pcolor(var['im'][i],cmap=pylab.cm.gray)
        pylab.hold(False)
        pylab.axis('equal')
        #        pylab.imshow(var['im'][i],cmap=gray,aspect='preserve')
        pylab.axis('off')
        if i == 0:
            pylab.title(
                '%dx%dx%d' %
                (var['im'][0].shape[0], var['im'][0].shape[1], total_num_pics))
        else:
            pylab.title('%d' % which_pics[i])
        pylab.draw()
    pylab.show()
Пример #19
0
    print "Writing ", fname
    zpickle.save(var, fname)
    fname = hdf5_fname(fname)
    print "Writing ", fname
    hdf5_save_images(var, fname)


if __name__ == "__main__":

    # cat stuff
    imfname = 'pics/catim081604.pics'
    if not os.path.exists(imfname):
        var_raw = make_raw('cat', show=False)
        save(var_raw, imfname)

    var_raw = zpickle.load('pics/catim081604.pics')
    #    var_raw['im']=[var_raw['im'][0]]  # for debugging: use 1 image

    imfname = 'pics/catim081604_norm.pics'
    if not os.path.exists(imfname):
        var = make_norm(var_raw)
        set_resolution(var, 'uint16')
        save(var, imfname)

    imfname = 'pics/catim081604_dog.pics'
    if not os.path.exists(imfname):
        var = make_dog(var_raw)
        set_resolution(var, 'uint16')
        save(var, imfname)

    imfname = 'pics/catim081604_white.pics'
Пример #20
0
def load_sim(fname):
    
    d=zpickle.load(fname)
    
    return d    
Пример #21
0
def load_sim(fname):

    d = zpickle.load(fname)

    return d
Пример #22
0
def run_sim(params=None,parent=None):
    
    if not params:
        params=default_params()
    elif isinstance(params,basestring):
        fname=params
        d=zpickle.load(fname)
    
        params=d['params']
        
    
    if parent:
        save_sim_file=params['tmpfile']
        params['display']=1
    else:
        save_sim_file=params['save_sim_file']
        if params['load_sim_file']==params['tmpfile']:
            params['load_sim_file']=''
            
    if not save_sim_file:
        save_sim_file='sims/untitled.dat'

        
        
    # Deal the random number seed here
    
    if params['random_seed']=='clock':
        numpy.random.seed(None)
    else:
        numpy.random.seed(params['random_seed'])
        
    params['actual_random_seed']=numpy.random.get_state()
    params['random_seed2']=int(round(numpy.random.rand()*1e6))
    
    start_time=time.time()
    end_time=None
    sim_time=None
    
    num_channels=len(params['pattern_input'])
    
    num_moments=get_num_moments(params)
    
    
    layer=0
    test_stimulus=params['test_stimulus'][layer]['type']
    
    total_num_inputs=0
    for p in params['pattern_input']:
        total_num_inputs=total_num_inputs+p['num_inputs']
        

    num_neurons=prod(params['num_neurons'])
    
    if params['load_sim_file']:
        
        d=zpickle.load(params['load_sim_file'])
        load_sim_params=d['params']
        
        weights=d['weights']
        moments=d['moments']
        initial_weights=d['weights']
        initial_moments=d['moments']
        t_mat=None
        
        if params['continue']:
            t_mat=d['t_mat']
            moments_mat=d['moments_mat']
            response_mat=d['response_mat']
            weights_mat=d['weights_mat']
            
            if not t_mat:
                moments_mat=None
                response_mat=None
        else:
            params['initial_weights']=weights
            params['initial_moments']=moments
    else:
        params['continue']=0
        
        full_mask=[]
        for p in params['pattern_input']:
            full_mask.append(get_circle_mask(p['num_inputs']))
            
        full_mask=numpy.array(full_mask).ravel()
        
        if not params['initial_weights']:
            params['initial_weights']=[numpy.asarray(numpy.random.rand(num_neurons,total_num_inputs)*(params['initial_weight_range'][1]-params['initial_weight_range'][0])+params['initial_weight_range'][0],numpy.float64)]
            params['initial_weights'][layer]=params['initial_weights'][layer]*(full_mask.repeat(num_neurons))

            
        initial_weights=params['initial_weights']
        
        if not params['initial_moments']:
            params['initial_moments']=[numpy.asarray(numpy.random.rand(num_moments,num_neurons)*(params['initial_moment_range'][1]-params['initial_moment_range'][0])+params['initial_moment_range'][0],numpy.float64)]

        initial_moments=params['initial_moments']
        
        moments_mat=numpy.dstack( (initial_moments[0],) )
        
        t_mat=None
    
    
    if not t_mat:  # not loaded from file
        
        t_mat=[0]
        start_epoch=1
        moments_mat=numpy.dstack( (initial_moments[0],) )
        
        if params['keep_every_epoch']:
            weights_mat=[initial_weights]
        else:
            weights_mat=[]
            

        response_mat=[]
        response_var=[]
        
        
        test_stimulus_type=params['test_stimulus'][layer]['type']
        if test_stimulus_type==1: # test OR single
 
            response_var=test_OR_single(params,initial_weights)
            if not response_var:
                params['test_stimulus'][layer]['type']=0
            else:
                for r in response_var:
                    response_mat.append([r[0]])
        
        
    else:
        start_epoch=len(t_mat)
        test_stimulus_type=params['test_stimulus'][layer]['type']
        response_var=[]
        if test_stimulus_type==1: # test OR single
            
            response_var=test_OR_single(params,initial_weights)
            if not response_var:
                params['test_stimulus'][layer]['type']=0
            else:
                for r,m in zip(response_var,response_mat):
                    m.append(r[0])
    

        
    weights=deepcopy(initial_weights)
    moments=deepcopy(initial_moments)
    
    
    response_var_list=[response_var]

    extra_mat=[]
    
    sim={'params':params,
            'weights':weights,
            'moments':moments,
            'moments_mat':moments_mat,
            'weights_mat':weights_mat,
            'response_mat':response_mat,
            'response_var_list':response_var_list,
            'initial_weights':weights,
            'initial_moments':moments,
            't_mat':t_mat,
            'start_time':start_time,
            'end_time':end_time,
            'sim_time':sim_time,
            'extra_mat':extra_mat}
        
    params_with_images=deepcopy(params)
    
    # make these point to the same thing
    params_with_images['saved_input_vectors']=params['saved_input_vectors']
    
    fix_images_directories(params_with_images)
    
    
    status('Preemtive Save: %s' % save_sim_file,parent)
    zpickle.save(sim,save_sim_file)
    
    
    w=weights[layer]
    m=moments[layer]
    
    
    t0=time.time()
    for epoch in range(start_epoch,start_epoch+params['epoch_number']):
        t1=time.time()
        
        extra=train(epoch-start_epoch,params_with_images,w,m)
    
        if extra:
            extra_mat.append(extra)
            
        
        # copy over mask stuff
        if epoch==start_epoch:
            for i,p in enumerate(params['pattern_input']):
                params['pattern_input'][i]['mask']=params_with_images['pattern_input'][i]['mask']

        dt1=time.time()-t1
        dt0=time.time()-t0
        
        frac=(epoch-start_epoch+1.0)/params['epoch_number']
        eta=sec2str(round(dt0/frac-dt0))
        
        status("%.4f...ETA %s" % (dt1,eta),parent)
        
        sim['moments_mat']=numpy.dstack( (sim['moments_mat'],m) )
        
        test_stimulus_type=params['test_stimulus'][layer]['type']
        
        if test_stimulus_type==1: # test OR single
            
            response_var=test_OR_single(params,weights)
            response_var_list[0]=response_var
            if not response_var:
                params['test_stimulus'][layer]['type']=0
            else:
                for r,mat in zip(response_var,response_mat):
                    mat.append(r[0])

        t_mat.append(t_mat[-1]+params['iter_per_epoch'])

        if params['keep_every_epoch']:
            weights_mat.append(deepcopy(weights))
           
        if params['display'] and epoch%params['epoch_per_display']==0:
            if parent:
                parent.Plot(sim)
            else:
                Plot(sim)
        
        if parent:
            parent.Yield()
            if parent.Stopping():
                break
            
        if dt0>(60*60*20):   # every 20 minutes
            status('Incremental Save: %s' % (save_sim_file),parent)
            zpickle.save(sim,save_sim_file)
        
    
    end_time=time.time()
    sim_time=end_time-start_time
        
    if params['display']:
        if parent:
            parent.Plot(sim)
        else:
            Plot(sim)

    status('Save: %s' % (save_sim_file),parent)
    zpickle.save(sim,save_sim_file)
Пример #23
0
        var['im'][i]=var['im'][i].astype(resolution)
        
    var['im_scale_shift']=[d/maxval, mn]
    
    


if __name__=="__main__":

    # cat stuff
    imfname='catim081604.pics'
    if not os.path.exists(imfname):
        var_raw=make_raw('cat',show=False)
        zpickle.save(var_raw,imfname)

    var_raw=zpickle.load('catim081604.pics')
#    var_raw['im']=[var_raw['im'][0]]  # for debugging: use 1 image
    imfname='catim081604_dog13.pics'
    if not os.path.exists(imfname):
        var=make_dog(var_raw)
        set_resolution(var,'uint8')
        zpickle.save(var,imfname)

    imfname='catim081604_white.pics'
    if not os.path.exists(imfname):
        var=make_white(var_raw)
        set_resolution(var,'uint8')
        zpickle.save(var,imfname)

    imfname='catim081604_dog13_rot4.pics'
    if not os.path.exists(imfname):
Пример #24
0
def run_sim(params=None, parent=None):

    if not params:
        params = default_params()
    elif isinstance(params, basestring):
        fname = params
        d = zpickle.load(fname)

        params = d['params']

    if parent:
        save_sim_file = params['tmpfile']
        params['display'] = 1
    else:
        save_sim_file = params['save_sim_file']
        if params['load_sim_file'] == params['tmpfile']:
            params['load_sim_file'] = ''

    if not save_sim_file is None and not save_sim_file:
        save_sim_file = 'sims/untitled.dat'

    # Deal the random number seed here

    if params['random_seed'] == 'clock':
        numpy.random.seed(None)
    else:
        numpy.random.seed(params['random_seed'])

    params['actual_random_seed'] = numpy.random.get_state()
    params['random_seed2'] = int(round(numpy.random.rand() * 1e6))

    start_time = time.time()
    end_time = None
    sim_time = None

    num_channels = len(params['pattern_input'])

    num_moments = get_num_moments(params)

    layer = 0
    test_stimulus = params['test_stimulus'][layer]['type']

    total_num_inputs = 0
    for p in params['pattern_input']:
        total_num_inputs = total_num_inputs + p['num_inputs']

    num_neurons = prod(params['num_neurons'])

    if params['load_sim_file']:

        d = zpickle.load(params['load_sim_file'])
        load_sim_params = d['params']

        weights = d['weights']
        moments = d['moments']
        individual_moments = d['individual_moments']

        initial_weights = d['weights']
        initial_moments = d['moments']
        initial_individual_moments = d['individual_moments']
        t_mat = None

        if params['continue']:
            t_mat = d['t_mat']
            moments_mat = d['moments_mat']
            individual_moments_mat = d['individual_moments_mat']
            response_mat = d['response_mat']
            weights_mat = d['weights_mat']

            if not t_mat:
                moments_mat = None
                individual_moments_mat = None
                response_mat = None
        else:
            params['initial_weights'] = deepcopy(weights)
            params['initial_moments'] = deepcopy(moments)
            params['initial_individual_moments'] = deepcopy(individual_moments)

            initial_weights = params['initial_weights']
            initial_moments = params['initial_moments']
            initial_individual_moments = params['initial_individual_moments']

    else:
        params['continue'] = 0

        full_mask = get_full_mask(params)

        if not params['initial_weights']:
            params['initial_weights'] = [
                numpy.asarray(
                    numpy.random.rand(num_neurons, total_num_inputs) *
                    (params['initial_weight_range'][1] -
                     params['initial_weight_range'][0]) +
                    params['initial_weight_range'][0], numpy.float64)
            ]

            # I love broadcasting! full_mask has a length of the number of inputs
            params['initial_weights'][
                layer] = params['initial_weights'][layer] * full_mask

        initial_weights = params['initial_weights']

        if not params['initial_moments']:
            params['initial_moments'] = [
                numpy.asarray(
                    numpy.random.rand(num_moments, num_neurons) *
                    (params['initial_moment_range'][1] -
                     params['initial_moment_range'][0]) +
                    params['initial_moment_range'][0], numpy.float64)
            ]

            # same size as the weights - make into a list if you need to
            # have more of these
            params['initial_individual_moments'] = [
                numpy.asarray(
                    numpy.random.rand(num_neurons, total_num_inputs) *
                    (params['initial_moment_range'][1] -
                     params['initial_moment_range'][0]) +
                    params['initial_moment_range'][0], numpy.float64)
            ]

        initial_moments = params['initial_moments']
        initial_individual_moments = params['initial_individual_moments']

        moments_mat = numpy.dstack((initial_moments[0], ))

        t_mat = None

    if not t_mat:  # not loaded from file

        t_mat = [0]
        start_epoch = 1
        moments_mat = numpy.dstack((initial_moments[0], ))

        if params['keep_every_epoch']:
            weights_mat = [initial_weights]
            individual_moments_mat = [initial_individual_moments]
        else:
            weights_mat = []
            individual_moments_mat = []

        response_mat = []
        response_var = []

        test_stimulus_type = params['test_stimulus'][layer]['type']
        if test_stimulus_type == 1:  # test OR single

            response_var = test_OR_single(params, initial_weights)
            if not response_var:
                params['test_stimulus'][layer]['type'] = 0
            else:
                for r in response_var:
                    response_mat.append([r[0]])

    else:
        start_epoch = len(t_mat)
        test_stimulus_type = params['test_stimulus'][layer]['type']
        response_var = []
        if test_stimulus_type == 1:  # test OR single

            response_var = test_OR_single(params, initial_weights)
            if not response_var:
                params['test_stimulus'][layer]['type'] = 0
            else:
                for r, m in zip(response_var, response_mat):
                    m.append(r[0])

    weights = deepcopy(initial_weights)
    moments = deepcopy(initial_moments)
    individual_moments = deepcopy(initial_individual_moments)

    response_var_list = [response_var]

    extra_mat = []

    sim = {
        'params': params,
        'weights': weights,
        'moments': moments,
        'individual_moments': individual_moments,
        'moments_mat': moments_mat,
        'individual_moments_mat': individual_moments_mat,
        'weights_mat': weights_mat,
        'response_mat': response_mat,
        'response_var_list': response_var_list,
        'initial_weights': initial_weights,
        'initial_moments': initial_moments,
        't_mat': t_mat,
        'start_time': start_time,
        'end_time': end_time,
        'sim_time': sim_time,
        'extra_mat': extra_mat
    }

    params_with_images = deepcopy(params)

    # make these point to the same thing
    params_with_images['saved_input_vectors'] = params['saved_input_vectors']

    fix_images_directories(params_with_images, parent)

    if not save_sim_file is None:
        status('Preemtive Save: %s' % save_sim_file, parent)
        zpickle.save(sim, save_sim_file)

    w = weights[layer]
    m = moments[layer]
    ind_m = individual_moments[layer]

    t0 = time.time()
    last_display_time = t0
    extra_input = []
    try:

        for epoch in range(start_epoch, start_epoch + params['epoch_number']):
            t1 = time.time()

            extra = train(epoch - start_epoch, params_with_images, w, m, ind_m,
                          extra_input)

            if extra:
                extra_mat.append(extra)

            # copy over mask stuff
            if epoch == start_epoch:
                for i, p in enumerate(params['pattern_input']):
                    params['pattern_input'][i]['mask'] = params_with_images[
                        'pattern_input'][i]['mask']

            dt1 = time.time() - t1
            dt0 = time.time() - t0
            if (time.time() -
                    last_display_time) > params['minimum_print_time']:
                last_display_time = time.time()

                frac = (epoch - start_epoch + 1.0) / params['epoch_number']
                eta = sec2str(dt0 / frac - dt0)

                status("%.4f...ETA %s" % (dt1, eta), parent)

            sim['moments_mat'] = numpy.dstack((sim['moments_mat'], m))

            test_stimulus_type = params['test_stimulus'][layer]['type']

            if test_stimulus_type == 1:  # test OR single

                response_var = test_OR_single(params, weights)
                response_var_list[0] = response_var
                if not response_var:
                    params['test_stimulus'][layer]['type'] = 0
                else:
                    for r, mat in zip(response_var, response_mat):
                        mat.append(r[0])

            t_mat.append(t_mat[-1] + params['iter_per_epoch'])

            if params['keep_every_epoch']:
                weights_mat.append(deepcopy(weights))
                individual_moments_mat.append(deepcopy(individual_moments))

            if params['display'] and epoch % params['epoch_per_display'] == 0:
                if parent:
                    parent.Plot(sim)
                else:
                    Plot(sim)

            if parent:
                parent.Yield()
                if parent.Stopping():
                    break

            if dt0 > (60 * 60 * 20):  # every 20 minutes
                if not save_sim_file is None:
                    status('Incremental Save: %s' % (save_sim_file), parent)
                    zpickle.save(sim, save_sim_file)

    except KeyboardInterrupt:
        status("Stopping!", parent)
        pass

    end_time = time.time()
    sim_time = end_time - start_time

    if params['display']:
        if parent:
            parent.Plot(sim)
        else:
            Plot(sim)

    tt = sec2str(time.time() - t0)
    if save_sim_file is None:
        status('Save: None. Total time %s' % (tt), parent)
    else:
        status('Save: %s. Total time %s' % (save_sim_file, tt), parent)
        zpickle.save(sim, save_sim_file)

    return sim
Пример #25
0
def Plot(sim):
    gray = pylab.cm.gray
    pylab.ion()

    try:
        if not sim['params']['display']:
            return
    except TypeError:  # sent a string
        sim = zpickle.load(sim)

    try:
        if sim['params']['display_module']:
            pass
    except:
        sim['params']['display_module'] = False

    if sim['params']['display_module']:
        try:
            module = __import__(sim['params']['display_module'],
                                fromlist=['UserPlot'])
        except ImportError:
            sim['params']['display'] = False
            print "Error", "Error in Import: %s.  Turning display off.  %s" % (
                sim['params']['display_module'], sys.exc_info())
            return

        try:
            module.UserPlot(None, sim)
            pylab.draw()
            return
        except ValueError:
            sim['params']['display'] = False
            print "Error in display.  Turning display off"
            return

    try:

        im = weights2image(sim['params'], sim['weights'])
        ax = pylab.subplot(221)
        pylab.pcolor(im, cmap=gray)
        ax.set_axis_bgcolor('k')
        pylab.axis('equal')

        num_moments = sim['moments_mat'].shape[0]

        ax = pylab.subplot(222)
        ax.hold(False)
        if num_moments == 1:
            num_neurons = sim['moments_mat'].shape[1]
            for k in range(num_neurons):
                pylab.plot(sim['t_mat'], sim['moments_mat'][0, k, :], '-o')
                ax.hold(True)
        elif num_moments == 2:
            num_neurons = sim['moments_mat'].shape[1]
            for k in range(num_neurons):
                pylab.plot(sim['t_mat'], sim['moments_mat'][0, k, :], 'b-o')
                ax2 = pylab.twinx(ax)
                pylab.plot(sim['t_mat'], sim['moments_mat'][1, k, :], 'g-o')
                ax2.yaxis.tick_right()

        else:
            num_neurons = sim['moments_mat'].shape[1]
            for k in range(num_neurons):
                for i in range(num_moments):
                    pylab.plot(sim['t_mat'], sim['moments_mat'][i, k, :], '-o')
                    ax.hold(True)

        pylab.subplot(223)
        pylab.hold(False)
        response_mat = sim['response_mat']
        response_var_list = sim['response_var_list']

        styles = ['b-', 'g-', 'r-', 'k-']
        ls = len(styles)
        for i, r in enumerate(response_var_list[-1]):
            x = r[1]
            y = r[2]

            pylab.plot(x, y, styles[i % ls] + "o")
            pylab.hold(True)

        pylab.subplot(224)
        pylab.hold(False)
        for i, r in enumerate(response_mat):
            pylab.plot(r, styles[i % ls])
            pylab.hold(True)

        pylab.draw()

    except ValueError:
        sim['params']['display'] = False
        print "Error in display.  Turning display off"
        return
Пример #26
0
def Plot(sim):
    gray=pylab.cm.gray
    pylab.ion()
    
    try:
        if not sim['params']['display']:
            return
    except TypeError:  # sent a string
        sim=zpickle.load(sim)

        
    try:
        if sim['params']['display_module']:
            pass
    except:
        sim['params']['display_module']=False
        
    if sim['params']['display_module']:
        try:
            module=__import__(sim['params']['display_module'],fromlist=['UserPlot'])
        except ImportError:
            sim['params']['display']=False
            print "Error","Error in Import: %s.  Turning display off" % sim['params']['display_module']
            return
        
        try:
            module.UserPlot(None,sim)
            pylab.draw()
            return
        except ValueError:
            sim['params']['display']=False
            print "Error in display.  Turning display off"
            return
    
    try:
    
        im=weights2image(sim['params'],sim['weights'])
        ax=pylab.subplot(221)
        pylab.pcolor(im,cmap=gray)
        ax.set_axis_bgcolor('k')
        pylab.axis('equal')
        
        num_moments=sim['moments_mat'].shape[0]

        
        ax=pylab.subplot(222)
        ax.hold(False)
        if num_moments==1:
            pylab.plot(sim['t_mat'],sim['moments_mat'][0,0,:],'-o')
        elif num_moments==2:
            pylab.plot(sim['t_mat'],sim['moments_mat'][0,0,:],'b-o')
            ax2=pylab.twinx(ax)
            pylab.plot(sim['t_mat'],sim['moments_mat'][1,0,:],'g-o')
            ax2.yaxis.tick_right()

        else:
            for i in range(num_moments):
                pylab.plot(sim['t_mat'],sim['moments_mat'][i,0,:],'-o')
                ax.hold(True)
        
        
    
        pylab.subplot(223)
        pylab.hold(False)
        response_mat=sim['response_mat']
        response_var_list=sim['response_var_list']
        
        
        styles=['b-','g-']
        for i,r in enumerate(response_var_list[-1]):
            x=r[1]
            y=r[2]
        
            pylab.plot(x,y,styles[i]+"o")
            pylab.hold(True)
    
        pylab.subplot(224)
        pylab.hold(False)
        for i,r in enumerate(response_mat):
            pylab.plot(r,styles[i])
            pylab.hold(True)
    
    
        pylab.draw()


    except ValueError:
        sim['params']['display']=False
        print "Error in display.  Turning display off"
        return
Пример #27
0
def load_sim(fname):

    d = zpickle.load(fname)

    return d['sim_params'], d['N'], d['C']