Ejemplo n.º 1
0
def analyseBlilinearModel():
    d = contrib.dd.loadResults("newest_dataset.dat")

    dataset_node = d.children[0].children[0]

    training_set = dataset_node.data["training_set"]
    validation_set = dataset_node.data["validation_set"]
    training_inputs = dataset_node.data["training_inputs"]
    validation_inputs = dataset_node.data["validation_inputs"]
    raw_validation_set = dataset_node.data["raw_validation_set"]

    kernels = dataset_node.children[1].children[4].data['Kernels']

    K1 = kernels[0]
    K2 = kernels[1]

    pred_act = numpy.multiply(training_inputs * K1.T, training_inputs * K2.T)
    pred_val_act = numpy.multiply(validation_inputs * K1.T,
                                  validation_inputs * K2.T)

    compareModelPerformanceWithRPI(training_set, validation_set,
                                   training_inputs, validation_inputs,
                                   numpy.mat(pred_act),
                                   numpy.mat(pred_val_act), raw_validation_set,
                                   85)
Ejemplo n.º 2
0
def loadLSCSMandAnalyse(): 
    
    res = contrib.dd.DB(None)
    (sizex,sizey,training_inputs,training_set,validation_inputs,validation_set,ff,db_node) = contrib.JanA.dataimport.sortOutLoading(res)
    raw_validation_set = db_node.data["raw_validation_set"]
    
    res = contrib.dd.loadResults("LSCSMbasic.dat")
    
    dataset_node = res.children[0].children[0]
    
    training_set = dataset_node.data["training_set"]
    validation_set = dataset_node.data["validation_set"]
    training_inputs= dataset_node.data["training_inputs"]
    validation_inputs= dataset_node.data["validation_inputs"]
    
    lscsm_new = contrib.JanA.LSCSM.LSCSM1(numpy.mat(training_inputs),numpy.mat(training_set),8,103)
    
    
    K = res.children[0].children[0].children[0].children[0].data["Kernels"]
    lscsm = res.children[0].children[0].children[0].children[0].data["LSCSM"]
    rfs  = lscsm_new.returnRFs(K)
    	
    pred_act = lscsm.response(training_inputs,K)
    pred_val_act = lscsm.response(validation_inputs,K)
   
    sizex=numpy.sqrt(numpy.shape(training_inputs)[1])
    from contrib.JanA.visualization import compareModelPerformanceWithRPI  
    
    compareModelPerformanceWithRPI(numpy.mat(training_set),numpy.mat(validation_set),numpy.mat(training_inputs),numpy.mat(validation_inputs),numpy.mat(pred_act),numpy.mat(pred_val_act),numpy.array(raw_validation_set),sizex,sizex,modelname='Model')
Ejemplo n.º 3
0
def runBilinearRegression():
    d = contrib.dd.loadResults("newest_dataset.dat")
    (sizex,sizey,training_inputs,training_set,validation_inputs,validation_set,ff,db_node) = sortOutLoading(d)
    raw_validation_set = db_node.data["raw_validation_set"]
    
    contrib.modelfit.save_fig_directory='/home/antolikjan/Doc/reports/Sparsness/InputContext/'
    
    params={}
    params["Bilinear"]=True
    db_node = db_node.get_child(params)
    
    params={}
    params["alpha"] = __main__.__dict__.get('Alpha',0.02)
    params["num_steps"] = __main__.__dict__.get('NumSteps',1)
    params["num_neurons"]= __main__.__dict__.get('NumNeurons',10)
    db_node = db_node.get_child(params)
    
    kernels = bilinearRegression(training_inputs,training_set,sizex,sizey,params["num_steps"],params["alpha"],params["num_neurons"])
     
    pred_act = bilinearModelResponse(training_inputs,kernels[0],kernels[1],sizex,sizey)
    pred_val_act = bilinearModelResponse(validation_inputs,kernels[0],kernels[1],sizex,sizey)
    
    compareModelPerformanceWithRPI(training_set[:,0:params["num_neurons"]],validation_set[:,0:params["num_neurons"]],training_inputs,validation_inputs,numpy.mat(pred_act),numpy.mat(pred_val_act),numpy.array(raw_validation_set)[:,:,0:params["num_neurons"]],'BilinearModel')
    
    db_node.add_data("Kernels",kernels,force=True)
    contrib.dd.saveResults(d,"newest_dataset.dat")
Ejemplo n.º 4
0
def analyseBlilinearModel():
    d = contrib.dd.loadResults("newest_dataset.dat")
    	
    dataset_node = d.children[0].children[0]
	
    training_set = dataset_node.data["training_set"]
    validation_set = dataset_node.data["validation_set"]
    training_inputs= dataset_node.data["training_inputs"]
    validation_inputs= dataset_node.data["validation_inputs"]
    raw_validation_set = dataset_node.data["raw_validation_set"]
	
    kernels = dataset_node.children[1].children[4].data['Kernels']
    
    K1 = kernels[0]
    K2 = kernels[1]
       
    pred_act = numpy.multiply(training_inputs*K1.T,training_inputs*K2.T)
    pred_val_act = numpy.multiply(validation_inputs*K1.T,validation_inputs*K2.T)
    
    compareModelPerformanceWithRPI(training_set,validation_set,training_inputs,validation_inputs,numpy.mat(pred_act),numpy.mat(pred_val_act),raw_validation_set,85)	
Ejemplo n.º 5
0
def runBilinearRegression():
    d = contrib.dd.loadResults("newest_dataset.dat")
    (sizex, sizey, training_inputs, training_set, validation_inputs,
     validation_set, ff, db_node) = sortOutLoading(d)
    raw_validation_set = db_node.data["raw_validation_set"]

    contrib.modelfit.save_fig_directory = '/home/antolikjan/Doc/reports/Sparsness/InputContext/'

    params = {}
    params["Bilinear"] = True
    db_node = db_node.get_child(params)

    params = {}
    params["alpha"] = __main__.__dict__.get('Alpha', 0.02)
    params["num_steps"] = __main__.__dict__.get('NumSteps', 1)
    params["num_neurons"] = __main__.__dict__.get('NumNeurons', 10)
    db_node = db_node.get_child(params)

    kernels = bilinearRegression(training_inputs, training_set, sizex, sizey,
                                 params["num_steps"], params["alpha"],
                                 params["num_neurons"])

    pred_act = bilinearModelResponse(training_inputs, kernels[0], kernels[1],
                                     sizex, sizey)
    pred_val_act = bilinearModelResponse(validation_inputs, kernels[0],
                                         kernels[1], sizex, sizey)

    compareModelPerformanceWithRPI(
        training_set[:, 0:params["num_neurons"]],
        validation_set[:, 0:params["num_neurons"]], training_inputs,
        validation_inputs, numpy.mat(pred_act), numpy.mat(pred_val_act),
        numpy.array(raw_validation_set)[:, :, 0:params["num_neurons"]],
        'BilinearModel')

    db_node.add_data("Kernels", kernels, force=True)
    contrib.dd.saveResults(d, "newest_dataset.dat")
Ejemplo n.º 6
0
def analyse():
   
    (pred_act,pred_val_act,training_set,validation_set,training_inputs,validation_inputs,raw_validation_set)= fetch_data()
    size =numpy.sqrt(numpy.shape(training_inputs)[1])
    
    compareModelPerformanceWithRPI(training_set,validation_set,training_inputs,validation_inputs,numpy.mat(pred_act),numpy.mat(pred_val_act),raw_validation_set,size,size,modelname='LSCSM')	
Ejemplo n.º 7
0
def sequentialFilterFinding():
    d = contrib.dd.loadResults("newest_dataset.dat")
    (sizex, sizey, training_inputs, training_set, validation_inputs,
     validation_set, ff, db_node) = sortOutLoading(d)
    raw_validation_set = db_node.data["raw_validation_set"]
    contrib.modelfit.save_fig_directory = '/home/antolikjan/Doc/reports/Sparsness/SequentialFilterFitting/'

    params = {}
    params["SequentialFilterFitting"] = True
    db_node = db_node.get_child(params)

    params = {}
    params["alpha"] = __main__.__dict__.get('Alpha', 0.02)
    params["num_neurons"] = __main__.__dict__.get('NumNeurons', 10)
    params["OF"] = __main__.__dict__.get('OF', 'Square')
    db_node = db_node.get_child(params)

    num_neurons_to_run = params["num_neurons"]

    training_set = numpy.mat(training_set)[:, 0:num_neurons_to_run]
    validation_set = numpy.mat(validation_set)[:, 0:num_neurons_to_run]
    training_inputs = numpy.mat(training_inputs)
    validation_inputs = numpy.mat(validation_inputs)
    raw_validation_set = numpy.array(raw_validation_set)[:, :,
                                                         0:num_neurons_to_run]

    num_pres, kernel_size = numpy.shape(training_inputs)

    laplace = laplaceBias(numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))
    rpi = numpy.linalg.pinv(training_inputs.T * training_inputs +
                            __main__.__dict__.get('RPILaplaceBias', 0.0001) *
                            laplace) * training_inputs.T * training_set

    new_training_inputs = numpy.zeros(numpy.shape(training_inputs))
    new_validation_inputs = numpy.zeros(numpy.shape(validation_inputs))

    Ks = numpy.zeros((num_neurons_to_run, kernel_size * 2 + 4))

    second_pred_act = []
    second_pred_val_act = []

    for i in xrange(0, num_neurons_to_run):
        print i
        # project out STA
        a = rpi[:, i] / numpy.sqrt(numpy.power(rpi[:, i], 2))

        for j in xrange(0, numpy.shape(training_inputs)[0]):
            new_training_inputs[j, :] = training_inputs[j, :] - a.T * (
                (training_inputs[j, :] * a)[0, 0])

        for j in xrange(0, numpy.shape(validation_inputs)[0]):
            new_validation_inputs[j, :] = validation_inputs[j, :] - a.T * (
                (validation_inputs[j, :] * a)[0, 0])

        k0 = numpy.zeros(
            (1, kernel_size * 2)).flatten().tolist() + [0, 0, 0, 0]
        scm = SimpleContextualModel(numpy.mat(new_training_inputs),
                                    numpy.mat(training_set[:, i]),
                                    laplace,
                                    of1=params["OF"],
                                    of2='Zero')
        #K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
        (K, success, c) = fmin_tnc(scm.func(),
                                   numpy.array(k0)[:],
                                   fprime=scm.der(),
                                   maxfun=1000,
                                   messages=0)
        Ks[i, :] = K

        second_pred_act.append(
            scm.response(new_training_inputs, numpy.array([K])))
        second_pred_val_act.append(
            scm.response(new_validation_inputs, numpy.array([K])))

    second_pred_act = numpy.hstack(second_pred_act)
    second_pred_val_act = numpy.hstack(second_pred_val_act)

    showRFS(
        numpy.reshape(Ks[:, 0:kernel_size],
                      (-1, numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))))
    release_fig('K1.png')
    showRFS(
        numpy.reshape(Ks[:, kernel_size:2 * kernel_size],
                      (-1, numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))))
    release_fig('K2.png')

    rpi_pred_act = training_inputs * rpi
    rpi_pred_val_act = validation_inputs * rpi

    #ofs = run_nonlinearity_detection(numpy.mat(training_set),numpy.mat(rpi_pred_act),num_bins=10,display=True)
    #rpi_pred_act_t = numpy.mat(apply_output_function(numpy.mat(rpi_pred_act),ofs))
    #rpi_pred_val_act_t = numpy.mat(apply_output_function(numpy.mat(rpi_pred_val_act),ofs))

    #ofs = run_nonlinearity_detection(numpy.mat(training_set),numpy.mat(second_pred_act),num_bins=10,display=True)
    #second_pred_act_t = numpy.mat(apply_output_function(numpy.mat(second_pred_act),ofs))
    #second_pred_val_act_t = numpy.mat(apply_output_function(numpy.mat(second_pred_val_act),ofs))

    visualize2DOF(rpi_pred_act, numpy.mat(second_pred_act), training_set)

    visualize2DOF(rpi_pred_val_act, numpy.mat(second_pred_val_act),
                  validation_set)

    #pred_act = second_pred_act_t + rpi_pred_act_t #numpy.multiply(second_pred_act_t,rpi_pred_act_t)
    #pred_val_act = second_pred_val_act_t + rpi_pred_val_act_t #numpy.multiply(second_pred_val_act_t,rpi_pred_val_act_t)

    of = fit2DOF(rpi_pred_act, numpy.mat(second_pred_act), training_set)

    pred_act = apply2DOF(rpi_pred_act, numpy.mat(second_pred_act), of)
    pred_val_act = apply2DOF(rpi_pred_val_act, numpy.mat(second_pred_val_act),
                             of)

    compareModelPerformanceWithRPI(training_set, validation_set,
                                   training_inputs, validation_inputs,
                                   numpy.mat(pred_act),
                                   numpy.mat(pred_val_act),
                                   numpy.array(raw_validation_set),
                                   'BilinearModel')
Ejemplo n.º 8
0
def runSCM():
    res = contrib.dd.loadResults("newest_dataset.dat")
    (sizex, sizey, training_inputs, training_set, validation_inputs,
     validation_set, ff, db_node) = contrib.JanA.dataimport.sortOutLoading(res)
    raw_validation_set = db_node.data["raw_validation_set"]

    params = {}
    params["SCM"] = True
    db_node = db_node.get_child(params)

    params = {}
    params["CLaplacaBias"] = __main__.__dict__.get('CLaplaceBias', 0.0004)
    params["SLaplacaBias"] = __main__.__dict__.get('SLaplaceBias', 0.0004)
    params["OF1"] = __main__.__dict__.get('OF1', 'Exp')
    params["OF2"] = __main__.__dict__.get('OF2', 'Square')
    params["num_neurons"] = __main__.__dict__.get('NumNeurons', 103)

    # creat history
    training_set = numpy.mat(training_set)
    validation_set = numpy.mat(validation_set)
    training_inputs = numpy.mat(training_inputs)
    validation_inputs = numpy.mat(validation_inputs)

    db_node1 = db_node
    db_node = db_node.get_child(params)

    num_pres, num_neurons = numpy.shape(training_set)
    num_pres, kernel_size = numpy.shape(training_inputs)
    num_neurons_to_run = params["num_neurons"]

    Ks = numpy.zeros((num_neurons, kernel_size * 2 + 4))

    print 'Kernel size', kernel_size

    laplace = laplaceBias(numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))

    rpi = numpy.linalg.pinv(training_inputs.T * training_inputs +
                            __main__.__dict__.get('RPILaplaceBias', 0.0001) *
                            laplace) * training_inputs.T * training_set

    for i in xrange(0, num_neurons_to_run):
        print i
        #k0 = rpi[:,i].getA1().tolist() +  numpy.zeros((1,kernel_size)).flatten().tolist() + [0,0]
        k0 = numpy.zeros(
            (1, kernel_size * 2)).flatten().tolist() + [0, 0, 0, 0]
        scm = SimpleContextualModel(numpy.mat(training_inputs),
                                    numpy.mat(training_set[:, i]),
                                    laplace,
                                    of1=params["OF1"],
                                    of2=params["OF2"])

        #K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
        (K, success, c) = fmin_tnc(scm.func(),
                                   numpy.array(k0)[:],
                                   fprime=scm.der(),
                                   maxfun=10000,
                                   messages=0)
        #print success
        #print c
        Ks[i, :] = K

    pred_act = scm.response(training_inputs, Ks)
    pred_val_act = scm.response(validation_inputs, Ks)

    print Ks[0, :]

    showRFS(
        numpy.reshape(Ks[:, 0:kernel_size],
                      (-1, numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))))
    release_fig('K1.png')
    showRFS(
        numpy.reshape(Ks[:, kernel_size:2 * kernel_size],
                      (-1, numpy.sqrt(kernel_size), numpy.sqrt(kernel_size))))
    release_fig('K2.png')

    compareModelPerformanceWithRPI(
        training_set[:, :num_neurons_to_run],
        validation_set[:, :num_neurons_to_run], training_inputs,
        validation_inputs,
        numpy.mat(pred_act)[:, :num_neurons_to_run],
        numpy.mat(pred_val_act)[:, :num_neurons_to_run],
        numpy.array(raw_validation_set)[:, :, :num_neurons_to_run], 'SCM')

    db_node.add_data("Kernels", Ks, force=True)
    db_node.add_data("GLM", scm, force=True)
Ejemplo n.º 9
0
def runCNM():
    res = contrib.dd.loadResults("newest_dataset.dat")
    (sizex,sizey,training_inputs,training_set,validation_inputs,validation_set,ff,db_node) = contrib.JanA.dataimport.sortOutLoading(res)
    raw_validation_set = db_node.data["raw_validation_set"]
    
    params={}
    params["SCM"]=True
    db_node = db_node.get_child(params)
    
    params={}
    params["LaplacaBias"] = __main__.__dict__.get('LaplaceBias',0.0004)
    params["OFAff"] = __main__.__dict__.get('OFAff','Exp')
    params["OFSurr"] = __main__.__dict__.get('OFSurr','Linear')
    params["num_neurons"] = __main__.__dict__.get('NumNeurons',103)
     
    significant = [0,1,6,7,8,9,12,13,15,16,17,19,20,21,22,23,24,25,26,27,28,30,31,32,33,34,38,39,40,42,43,45,46,47,48,49,50,51,52,53,56,58,59,61,62,63,64,65,66,69,71,72,75,77,78,79,80,83,85,89,91,92,93,94,96,100,101,102] 
    # creat history
    training_set = numpy.mat(training_set)[:,significant]
    validation_set = numpy.mat(validation_set)[:,significant]
    training_inputs= numpy.mat(training_inputs)
    validation_inputs= numpy.mat(validation_inputs)
    
    for i in xrange(0,len(raw_validation_set)):
        raw_validation_set[i] = numpy.mat(raw_validation_set[i])[:,significant]
	
    db_node1 = db_node
    db_node = db_node.get_child(params)
    
    num_pres,num_neurons = numpy.shape(training_set)
    num_pres,kernel_size = numpy.shape(training_inputs)
    num_neurons_to_run=params["num_neurons"]
    
    Ks = numpy.zeros((num_neurons,kernel_size+7))
    
    print 'Kernel size',kernel_size
    
    laplace = laplaceBias(sizex,sizey)
    
    
    rpi = numpy.linalg.pinv(training_inputs.T*training_inputs + __main__.__dict__.get('RPILaplaceBias',0.0004)*laplace) * training_inputs.T * training_set
    
    
    
     
    bounds  = []
     
    for i in xrange(0,kernel_size):
	bounds.append((-1000000000,10000000000))
    
    bounds = [(6,26),(6,25),(1,1000000),(0.0,500000),(0.001,100000),(-100,100),(-100,100)] + bounds
    
    for i in xrange(0,num_neurons_to_run): 
	print i
	k0 = [15,15,20,100000,1,0,0] + rpi[:,i].getA1().tolist() 
	
	print numpy.shape(k0)
	print sizex,sizey
	scm = ContrastNormalizationModel(training_inputs,numpy.mat(training_set[:,i]),laplace,sizex,sizey,of_aff=params["OFAff"],of_surr=params["OFSurr"])

	#K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
	(K,success,c)=fmin_tnc(scm.func(),numpy.array(k0)[:],fprime=scm.der(),bounds = bounds,maxfun = 100000,messages=0)
	print scm.func()(K)
	Ks[i,:] = K

    pred_act = scm.response(training_inputs,Ks)
    pred_val_act = scm.response(validation_inputs,Ks)
    
    from contrib.JanA.sparsness_analysis import TrevesRollsSparsness
    
    showRFS(numpy.reshape(numpy.array(rpi.T),(-1,sizex,sizey)))
    
    showRFS(numpy.reshape(Ks[:,7:kernel_size+7],(-1,sizex,sizey)))
    
    print Ks[:,:7]

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(pred_val_act)).flatten()) 
    
    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(pred_val_act.T)).flatten()) 

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(validation_set)).flatten()) 
    
    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(validation_set.T)).flatten()) 


    compareModelPerformanceWithRPI(training_set[:,:num_neurons_to_run],validation_set[:,:num_neurons_to_run],training_inputs,validation_inputs,numpy.mat(pred_act)[:,:num_neurons_to_run],numpy.mat(pred_val_act)[:,:num_neurons_to_run],numpy.array(raw_validation_set)[:,:,:num_neurons_to_run],sizex,sizey,'SCM')	

    db_node.add_data("Kernels",Ks,force=True)
    db_node.add_data("GLM",scm,force=True)
Ejemplo n.º 10
0
def runCNM():
    res = contrib.dd.loadResults("newest_dataset.dat")
    (sizex, sizey, training_inputs, training_set, validation_inputs,
     validation_set, ff, db_node) = contrib.JanA.dataimport.sortOutLoading(res)
    raw_validation_set = db_node.data["raw_validation_set"]

    params = {}
    params["SCM"] = True
    db_node = db_node.get_child(params)

    params = {}
    params["LaplacaBias"] = __main__.__dict__.get('LaplaceBias', 0.0004)
    params["OFAff"] = __main__.__dict__.get('OFAff', 'Exp')
    params["OFSurr"] = __main__.__dict__.get('OFSurr', 'Linear')
    params["num_neurons"] = __main__.__dict__.get('NumNeurons', 103)

    significant = [
        0, 1, 6, 7, 8, 9, 12, 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26,
        27, 28, 30, 31, 32, 33, 34, 38, 39, 40, 42, 43, 45, 46, 47, 48, 49, 50,
        51, 52, 53, 56, 58, 59, 61, 62, 63, 64, 65, 66, 69, 71, 72, 75, 77, 78,
        79, 80, 83, 85, 89, 91, 92, 93, 94, 96, 100, 101, 102
    ]
    # creat history
    training_set = numpy.mat(training_set)[:, significant]
    validation_set = numpy.mat(validation_set)[:, significant]
    training_inputs = numpy.mat(training_inputs)
    validation_inputs = numpy.mat(validation_inputs)

    for i in xrange(0, len(raw_validation_set)):
        raw_validation_set[i] = numpy.mat(raw_validation_set[i])[:,
                                                                 significant]

    db_node1 = db_node
    db_node = db_node.get_child(params)

    num_pres, num_neurons = numpy.shape(training_set)
    num_pres, kernel_size = numpy.shape(training_inputs)
    num_neurons_to_run = params["num_neurons"]

    Ks = numpy.zeros((num_neurons, kernel_size + 7))

    print 'Kernel size', kernel_size

    laplace = laplaceBias(sizex, sizey)

    rpi = numpy.linalg.pinv(training_inputs.T * training_inputs +
                            __main__.__dict__.get('RPILaplaceBias', 0.0004) *
                            laplace) * training_inputs.T * training_set

    bounds = []

    for i in xrange(0, kernel_size):
        bounds.append((-1000000000, 10000000000))

    bounds = [(6, 26), (6, 25), (1, 1000000), (0.0, 500000), (0.001, 100000),
              (-100, 100), (-100, 100)] + bounds

    for i in xrange(0, num_neurons_to_run):
        print i
        k0 = [15, 15, 20, 100000, 1, 0, 0] + rpi[:, i].getA1().tolist()

        print numpy.shape(k0)
        print sizex, sizey
        scm = ContrastNormalizationModel(training_inputs,
                                         numpy.mat(training_set[:, i]),
                                         laplace,
                                         sizex,
                                         sizey,
                                         of_aff=params["OFAff"],
                                         of_surr=params["OFSurr"])

        #K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
        (K, success, c) = fmin_tnc(scm.func(),
                                   numpy.array(k0)[:],
                                   fprime=scm.der(),
                                   bounds=bounds,
                                   maxfun=100000,
                                   messages=0)
        print scm.func()(K)
        Ks[i, :] = K

    pred_act = scm.response(training_inputs, Ks)
    pred_val_act = scm.response(validation_inputs, Ks)

    from contrib.JanA.sparsness_analysis import TrevesRollsSparsness

    showRFS(numpy.reshape(numpy.array(rpi.T), (-1, sizex, sizey)))

    showRFS(numpy.reshape(Ks[:, 7:kernel_size + 7], (-1, sizex, sizey)))

    print Ks[:, :7]

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(pred_val_act)).flatten())

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(pred_val_act.T)).flatten())

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(validation_set)).flatten())

    pylab.figure()
    pylab.hist(TrevesRollsSparsness(numpy.mat(validation_set.T)).flatten())

    compareModelPerformanceWithRPI(
        training_set[:, :num_neurons_to_run],
        validation_set[:, :num_neurons_to_run], training_inputs,
        validation_inputs,
        numpy.mat(pred_act)[:, :num_neurons_to_run],
        numpy.mat(pred_val_act)[:, :num_neurons_to_run],
        numpy.array(raw_validation_set)[:, :, :num_neurons_to_run], sizex,
        sizey, 'SCM')

    db_node.add_data("Kernels", Ks, force=True)
    db_node.add_data("GLM", scm, force=True)
Ejemplo n.º 11
0
def runSCM():
    res = contrib.dd.loadResults("newest_dataset.dat")
    (sizex,sizey,training_inputs,training_set,validation_inputs,validation_set,ff,db_node) = contrib.JanA.dataimport.sortOutLoading(res)
    raw_validation_set = db_node.data["raw_validation_set"]
    
    params={}
    params["SCM"]=True
    db_node = db_node.get_child(params)
    
    params={}
    params["CLaplacaBias"] = __main__.__dict__.get('CLaplaceBias',0.0004)
    params["SLaplacaBias"] = __main__.__dict__.get('SLaplaceBias',0.0004)
    params["OF1"] = __main__.__dict__.get('OF1','Exp')
    params["OF2"] = __main__.__dict__.get('OF2','Square')
    params["num_neurons"] = __main__.__dict__.get('NumNeurons',103)
     
    # creat history
    training_set = numpy.mat(training_set)
    validation_set = numpy.mat(validation_set)
    training_inputs= numpy.mat(training_inputs)
    validation_inputs= numpy.mat(validation_inputs)
    
    db_node1 = db_node
    db_node = db_node.get_child(params)
    
    num_pres,num_neurons = numpy.shape(training_set)
    num_pres,kernel_size = numpy.shape(training_inputs)
    num_neurons_to_run=params["num_neurons"]
    
    Ks = numpy.zeros((num_neurons,kernel_size*2+4))
    
    print 'Kernel size',kernel_size
    
    laplace = laplaceBias(numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))
    
    rpi = numpy.linalg.pinv(training_inputs.T*training_inputs + __main__.__dict__.get('RPILaplaceBias',0.0001)*laplace) * training_inputs.T * training_set
     
    for i in xrange(0,num_neurons_to_run): 
	print i
	#k0 = rpi[:,i].getA1().tolist() +  numpy.zeros((1,kernel_size)).flatten().tolist() + [0,0]
	k0 = numpy.zeros((1,kernel_size*2)).flatten().tolist() + [0,0,0,0]
	scm = SimpleContextualModel(numpy.mat(training_inputs),numpy.mat(training_set[:,i]),laplace,of1=params["OF1"],of2=params["OF2"])

	
	#K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
	(K,success,c)=fmin_tnc(scm.func(),numpy.array(k0)[:],fprime=scm.der(),maxfun = 10000,messages=0)
	#print success
	#print c
	Ks[i,:] = K
    
    pred_act = scm.response(training_inputs,Ks)
    pred_val_act = scm.response(validation_inputs,Ks)
    
    print Ks[0,:]
    
    
    showRFS(numpy.reshape(Ks[:,0:kernel_size],(-1,numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))))
    release_fig('K1.png')
    showRFS(numpy.reshape(Ks[:,kernel_size:2*kernel_size],(-1,numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))))
    release_fig('K2.png')
        
    compareModelPerformanceWithRPI(training_set[:,:num_neurons_to_run],validation_set[:,:num_neurons_to_run],training_inputs,validation_inputs,numpy.mat(pred_act)[:,:num_neurons_to_run],numpy.mat(pred_val_act)[:,:num_neurons_to_run],numpy.array(raw_validation_set)[:,:,:num_neurons_to_run],'SCM')	

    db_node.add_data("Kernels",Ks,force=True)
    db_node.add_data("GLM",scm,force=True)
Ejemplo n.º 12
0
def sequentialFilterFinding():
    d = contrib.dd.loadResults("newest_dataset.dat")
    (sizex,sizey,training_inputs,training_set,validation_inputs,validation_set,ff,db_node) = sortOutLoading(d)
    raw_validation_set = db_node.data["raw_validation_set"]
    contrib.modelfit.save_fig_directory='/home/antolikjan/Doc/reports/Sparsness/SequentialFilterFitting/'
    
    params={}
    params["SequentialFilterFitting"]=True
    db_node = db_node.get_child(params)
    
    params={}
    params["alpha"] = __main__.__dict__.get('Alpha',0.02)
    params["num_neurons"]= __main__.__dict__.get('NumNeurons',10)
    params["OF"] = __main__.__dict__.get('OF','Square')
    db_node = db_node.get_child(params)
    
    num_neurons_to_run=params["num_neurons"]
    
    training_set = numpy.mat(training_set)[:,0:num_neurons_to_run]
    validation_set = numpy.mat(validation_set)[:,0:num_neurons_to_run]
    training_inputs= numpy.mat(training_inputs)
    validation_inputs= numpy.mat(validation_inputs)
    raw_validation_set = numpy.array(raw_validation_set)[:,:,0:num_neurons_to_run] 

    
    num_pres,kernel_size = numpy.shape(training_inputs)
    
    laplace = laplaceBias(numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))
    rpi = numpy.linalg.pinv(training_inputs.T*training_inputs + __main__.__dict__.get('RPILaplaceBias',0.0001)*laplace) * training_inputs.T * training_set

    new_training_inputs = numpy.zeros(numpy.shape(training_inputs))
    new_validation_inputs = numpy.zeros(numpy.shape(validation_inputs)) 
            
    Ks = numpy.zeros((num_neurons_to_run,kernel_size*2+4))
    
    second_pred_act=[]
    second_pred_val_act=[]
    
    for i in xrange(0,num_neurons_to_run):
	print i
	# project out STA
	a = rpi[:,i]/numpy.sqrt(numpy.power(rpi[:,i],2))
	
	for j in xrange(0,numpy.shape(training_inputs)[0]):
		new_training_inputs[j,:] = training_inputs[j,:] - a.T * ((training_inputs[j,:]*a)[0,0])
	
	for j in xrange(0,numpy.shape(validation_inputs)[0]):
		new_validation_inputs[j,:] = validation_inputs[j,:] - a.T * ((validation_inputs[j,:]*a)[0,0])

	
	
	k0 = numpy.zeros((1,kernel_size*2)).flatten().tolist() + [0,0,0,0]
	scm = SimpleContextualModel(numpy.mat(new_training_inputs),numpy.mat(training_set[:,i]),laplace,of1=params["OF"],of2='Zero')
	#K = fmin_ncg(scm.func(),numpy.array(k0),scm.der(),fhess = scm.hess(),avextol=0.0000001,maxiter=20)
	(K,success,c)=fmin_tnc(scm.func(),numpy.array(k0)[:],fprime=scm.der(),maxfun = 1000,messages=0)
	Ks[i,:] = K
        
	second_pred_act.append(scm.response(new_training_inputs,numpy.array([K])))
        second_pred_val_act.append(scm.response(new_validation_inputs,numpy.array([K])))

    second_pred_act = numpy.hstack(second_pred_act)
    second_pred_val_act = numpy.hstack(second_pred_val_act)

    showRFS(numpy.reshape(Ks[:,0:kernel_size],(-1,numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))))
    release_fig('K1.png')
    showRFS(numpy.reshape(Ks[:,kernel_size:2*kernel_size],(-1,numpy.sqrt(kernel_size),numpy.sqrt(kernel_size))))
    release_fig('K2.png')

    
    rpi_pred_act = training_inputs * rpi
    rpi_pred_val_act = validation_inputs * rpi
    
    #ofs = run_nonlinearity_detection(numpy.mat(training_set),numpy.mat(rpi_pred_act),num_bins=10,display=True)
    #rpi_pred_act_t = numpy.mat(apply_output_function(numpy.mat(rpi_pred_act),ofs))
    #rpi_pred_val_act_t = numpy.mat(apply_output_function(numpy.mat(rpi_pred_val_act),ofs))

    #ofs = run_nonlinearity_detection(numpy.mat(training_set),numpy.mat(second_pred_act),num_bins=10,display=True)
    #second_pred_act_t = numpy.mat(apply_output_function(numpy.mat(second_pred_act),ofs))
    #second_pred_val_act_t = numpy.mat(apply_output_function(numpy.mat(second_pred_val_act),ofs))

    visualize2DOF(rpi_pred_act,numpy.mat(second_pred_act),training_set)
    
    visualize2DOF(rpi_pred_val_act,numpy.mat(second_pred_val_act),validation_set)
    
    #pred_act = second_pred_act_t + rpi_pred_act_t #numpy.multiply(second_pred_act_t,rpi_pred_act_t)
    #pred_val_act = second_pred_val_act_t + rpi_pred_val_act_t #numpy.multiply(second_pred_val_act_t,rpi_pred_val_act_t)
    
    of = fit2DOF(rpi_pred_act,numpy.mat(second_pred_act),training_set)
    
    pred_act = apply2DOF(rpi_pred_act,numpy.mat(second_pred_act),of)
    pred_val_act = apply2DOF(rpi_pred_val_act,numpy.mat(second_pred_val_act),of)

    compareModelPerformanceWithRPI(training_set,validation_set,training_inputs,validation_inputs,numpy.mat(pred_act),numpy.mat(pred_val_act),numpy.array(raw_validation_set),'BilinearModel')