Пример #1
0
//! main function for Conditioning experiment
int main() {

  // set up random number generator:
	gsl_rng_env_setup();
	r = gsl_rng_alloc(gsl_rng_default);
	gsl_rng_set(r, RANDOM_SEED);

	// initialise the specifice parameters for this simulation.
	initDerivedParams();
	
	//char filename[1024];
	//sprintf(filename, "data/conditioning_%d_%4f_%d.dat", TRAININGCYCLES, ETA, TAU_ALPHA);
	FILE *F = fopen(FILENAME, "w+b"); //! output file name
	
	gsl_vector *psp = gsl_vector_alloc(NPRE); //! to hold PSPs
	gsl_vector *pspS = gsl_vector_alloc(NPRE); //! to hold (filtered?) PSPs
	gsl_vector *sue = gsl_vector_alloc(NPRE); //! "positive" part of PSP fur integration
	gsl_vector *sui = gsl_vector_alloc(NPRE); //! "negative" part of PSP fur integration
	gsl_vector *pspTilde = gsl_vector_alloc(NPRE); //! low pass (2nd) filtered PSS
	gsl_vector *wB  = gsl_vector_alloc(NPRE); //! weights of connections to Bacon neuron
	gsl_vector *wW  = gsl_vector_alloc(NPRE); //! weights of connections to Water neuron
	gsl_vector *wR  = gsl_vector_alloc(NPRE);  //! weights of connections to Reward neuron
	gsl_vector *ou  = gsl_vector_alloc(NPRE); //! xxxx
	gsl_vector *oum  = gsl_vector_alloc(NPRE); //! xxx
	gsl_vector *pres  = gsl_vector_alloc(NPRE); //! xxx

	//! short-hand pointer to the datastructures above.
	double *pspP = gsl_vector_ptr(psp,0);
	double *pspSP = gsl_vector_ptr(pspS,0);
	double *sueP = gsl_vector_ptr(sue,0);
	double *suiP = gsl_vector_ptr(sui,0);
	double *pspTildeP = gsl_vector_ptr(pspTilde,0);
	double *wBP = gsl_vector_ptr(wB,0);
	double *wWP = gsl_vector_ptr(wW,0);
	double *wRP = gsl_vector_ptr(wR,0);
	double *ouP = gsl_vector_ptr(ou,0);
	double *oumP = gsl_vector_ptr(oum,0);
	double *presP = gsl_vector_ptr(pres,0);

	// initialise data structures:
	for(int i=0; i<NPRE; i++) {
	  *(pspP+i) = 0;
		*(sueP+i) = 0;
		*(suiP+i) = 0;
		// random connections to B, W, and R neurons
		*(wBP+i) = gsl_ran_gaussian(r, .04) + .07; 
		*(wWP+i) = gsl_ran_gaussian(r, .04) + .07;
		*(wRP+i) = gsl_ran_gaussian(r, .04) + .07;
	}
	
	/*! Bacon neuron:
	  - uB soma potential
	  - uVB potential from dendrited alone
	  - rU rate from some
	  - rVB rate from dentritic input alone
	  and then the same for W,R neurons
	*/
	double uB = 0, uVB = 0, rUB = 0, rVB = 0;
	double uW = 0, uVW = 0, rUW = 0, rVW = 0;
	double uR = 0, uVR = 0, rUR = 0, rVR = 0;
	
	//! we only recorded activites of this many pres
	int nOfRecordedPre = 50;
	//! this describes the length of the network state we store
	int stateLength = 4 * nOfRecordedPre + 12;
	//! the states consistes of u, uV, rU, rV of all recorded pres and of B,R,W neurons.
	double *state[stateLength];
	for(int i = 0; i < nOfRecordedPre; i++) {
	  *(state + 0*nOfRecordedPre + i) = wBP + i; // points to weight vector of connection to B etc
	  *(state + 1*nOfRecordedPre + i) = wWP + i; 
	  *(state + 2*nOfRecordedPre + i) = wRP + i;
	  *(state + 3*nOfRecordedPre + i) = presP + i; // points to presP values
	} 
	*(state + 4*nOfRecordedPre) = &uB; // points to potential u of B etc
	*(state + 4*nOfRecordedPre+1) = &uVB;
	*(state + 4*nOfRecordedPre+2) = &rUB;
	*(state + 4*nOfRecordedPre+3) = &rVB;
	*(state + 4*nOfRecordedPre+4) = &uW;
	*(state + 4*nOfRecordedPre+5) = &uVW;
	*(state + 4*nOfRecordedPre+6) = &rUW;
	*(state + 4*nOfRecordedPre+7) = &rVW;
	*(state + 4*nOfRecordedPre+8) = &uR;
	*(state + 4*nOfRecordedPre+9) = &uVR;
	*(state + 4*nOfRecordedPre+10) = &rUR;
	*(state + 4*nOfRecordedPre+11) = &rVR;
	
	// pointers to input currents  xxx to do with B,W,R
	//! \todo why initialised this way?
	double *IB, *IW, *IR = I1, *ou_t, uI;

	double IRf = 1; //! reward factor
	
	/* Start of simulations */
	// repeat for all training cycles
	for( int s = 0; s < TRAININGCYCLES; s++) {
	  // apply Bacon and Water stimulus alternatinglly with corresponding reward
		if( s%2==0 ) {
			ou_t = OU2; IB = I2; IW = I1; IRf = .5;
		} else {
			ou_t = OU1; IB = I1; IW = I2; IRf = 1; 
		}

		// now for all time bins:
		for( int t = 0; t < TIMEBINS; t++) {
			for( int i = 0; i < NPRE; i++) {
				mixOUs(ouP + i, ou_t[t * NPRE + i], MIX[t], oumP + i);
				updatePre(sueP+i, suiP+i, pspP + i, pspSP + i, pspTildeP + i, *(presP + i) = spiking(DT * phi(*(oumP + i)), gsl_ran_flat(r,0,1))); 
			}
			updateMembrane(&uB, &uVB, &uI, wB, psp, IB[t], 0);
			updateMembrane(&uW, &uVW, &uI, wW, psp, IW[t], 0);
			updateMembrane(&uR, &uVR, &uI, wR, psp, IRf*IR[t], 0);
			//rUB = spiking(phi(uB), gsl_ran_flat(r,0,1)); rVB = phi(uVB);
			//rUW = spiking(phi(uW), gsl_ran_flat(r,0,1)); rVW = phi(uVW);
			//rUR = spiking(phi(uR), gsl_ran_flat(r,0,1)); rVR = phi(uVR);

			//! do calculates on the potentials only, not the actual spikes:
			rUB = phi(uB); rVB = phi(uVB);
			rUW = phi(uW); rVW = phi(uVW);
			rUR = phi(uR); rVR = phi(uVR);

			for(int i = 0; i < NPRE; i++) {
				updateWeight(wBP + i, rUB, *(pspTildeP+i), rVB, *(pspSP+i));
				updateWeight(wWP + i, rUW, *(pspTildeP+i), rVW, *(pspSP+i));
				updateWeight(wRP + i, rUR, *(pspTildeP+i), rVR, *(pspSP+i));
			}

			// write out states after the first 10 cycles:
			if(s > TRAININGCYCLES - 9 ) { 
				for(int i=0; i<stateLength; i++) {
					fwrite(*(state+i), sizeof(double), 1, F);
				}
			}
		}
	}
	
	gsl_vector_free(psp); gsl_vector_free(pspS); gsl_vector_free(wB); gsl_vector_free(wW); gsl_vector_free(wR);
	free(ou); free(oum); free(OU1); free(OU2); free(MIX); free(I1); free(I2);
	
	fclose(F); 
	
	return 0;
}
Пример #2
0
/**
 main simulation loop
*/
int main() {

  // init own parameters.
  initDerivedParams(); 

  // init random generator
  gsl_rng_env_setup();
  r = gsl_rng_alloc(gsl_rng_default);
  gsl_rng_set(r, SEED_MAIN);

  // file handle for xxx file
  FILE *postF = fopen(FILENAME_POST, FILEPOST_FLAG);

  // file handle for xxx file
  FILE *preF = fopen(FILENAME_PRE, "wb");
	
  // set up vectors:

  // to hold post synaptic potentials [unused??]
  gsl_vector *psp = gsl_vector_alloc(NPRE);
  // to hold post synaptic potentials 1st filtered
  gsl_vector *pspS = gsl_vector_alloc(NPRE);
  // to hold "excitatory" part of psp for Euler integration
  gsl_vector *sue = gsl_vector_alloc(NPRE);
  // to hold "inhibitory" part of psp for Euler integration
  gsl_vector *sui = gsl_vector_alloc(NPRE);
  // to hold psp 2nd filter
  gsl_vector *pspTilde = gsl_vector_alloc(NPRE);
  // to hold weights
  gsl_vector *w  = gsl_vector_alloc(NPRE);
  // to hold xxx
  gsl_vector *pres  = gsl_vector_alloc(NPRE);

  // ?? ou XXX \todo
#ifdef PREDICT_OU
  gsl_vector *ou = gsl_vector_alloc(N_OU);
  gsl_vector *preU = gsl_vector_calloc(NPRE);
  gsl_vector *wInput = gsl_vector_alloc(N_OU);
  gsl_matrix *wPre  = gsl_matrix_calloc(NPRE, N_OU);
  double *preUP = gsl_vector_ptr(preU,0);
  double *ouP = gsl_vector_ptr(ou,0);
  double *wInputP = gsl_vector_ptr(wInput,0);
  double *wPreP = gsl_matrix_ptr(wPre,0,0);
#endif

  // get pointers to array within the gsl_vector data structures above.
  double *pspP = gsl_vector_ptr(psp,0);
  double *pspSP = gsl_vector_ptr(pspS,0);
  double *sueP = gsl_vector_ptr(sue,0);
  double *suiP = gsl_vector_ptr(sui,0);
  double *pspTildeP = gsl_vector_ptr(pspTilde,0);
  double *wP = gsl_vector_ptr(w,0);
  double *presP = gsl_vector_ptr(pres,0);

  for(int i=0; i<NPRE; i++) {

    // init pspP etc to zero
    *(pspP+i) = 0;
    *(sueP+i) = 0;
    *(suiP+i) = 0;
#ifdef RANDI_WEIGHTS
    // Gaussian weights
    *(wP+i) = gsl_ran_gaussian(r, .1);
#else
    *(wP+i) = 0;
#endif
  }


  //! OU \todo what for?	
#ifdef PREDICT_OU
  for(int j=0; j < N_OU; j++) {
    *(ouP + j) = gsl_ran_gaussian(r, 1) + M_OU;
    *(wInputP + j) = gsl_ran_lognormal(r, 0., 2.)/N_OU/exp(2.)/2.;
    for(int i=0; i < NPRE; i++) *(wPreP + j*NPRE + i) = gsl_ran_lognormal(r, 0., 2.)/N_OU/exp(2.)/2.;
  }
#endif

  // temp variables for the simulation yyyy
  double 
    u = 0, // soma potential.
    uV = 0, // some potential from dendrite only (ie discounted
	    // dendrite potential
    rU = 0, // instantneou rate 
    rV = 0, // rate on dendritic potential only
    uI = 0, // soma potential only from somatic inputs
    rI = 0, // rate on somatic potential only
    uInput = 0; // for OU?

  // run simulatio TRAININGCYCLES number of times
  for( int s = 0; s < TRAININGCYCLES; s++) {

    // for all TIMEBINS
    for( int t = 0; t < TIMEBINS; t++) {

#ifdef PREDICT_OU
      for(int i = 0; i < N_OU; i++) {
	*(ouP+i) = runOU(*(ouP+i), M_OU, GAMMA_OU, S_OU);
      }
      gsl_blas_dgemv(CblasNoTrans, 1., wPre, ou, 0., preU); 
#endif

      // update PSP of our neurons for inputs from all presynaptic neurons
      for( int i = 0; i < NPRE; i++) {

#ifdef RAMPUPRATE
	/** just read in the PRE_ACT and generate a spike and store it in presP -- so PRE_ACT has inpretation of potential */
	updatePre(sueP+i, suiP+i, pspP + i, pspSP + i, pspTildeP + i, *(presP + i) = spiking(PRE_ACT[t*NPRE + i], gsl_rng_uniform(r)));

#elif defined PREDICT_OU
	//*(ouP+i) = runOU(*(ouP+i), M_OU, GAMMA_OU, S_OU); // why commented out?
	updatePre(sueP+i, suiP+i, pspP + i, pspSP + i, pspTildeP + i, *(presP + i) = DT * phi(*(preUP+i)));//spiking(DT * phi(*(preUP+i)), gsl_rng_uniform(r))); // why commented out?

#else
	// PRE_ACT intepreated as spikes
	updatePre(sueP+i, suiP+i, pspP + i, pspSP + i, pspTildeP + i, *(presP + i) = PRE_ACT[t*NPRE + i]);
#endif
      } // endfor NPRE

#ifdef PREDICT_OU
      gsl_blas_ddot(wInput, ou, &uInput);
      GE[t] = DT * phi(uInput);

#endif
      // now update the membrane potential.
      updateMembrane(&u, &uV, &uI, w, psp, GE[t], GI[t]);


      // now calculate rates from from potentials.
#ifdef POSTSPIKING // usually switch off as learning is faster when
		   // learning from U
      // with low-pass filtering of soma potential from actual
      // generation of spikes (back propgating dentric spikes?
      rU = GAMMA_POSTS*rU + (1-GAMMA_POSTS)*spiking(DT * phi(u),  gsl_rng_uniform(r))/DT;
#else
      // simpler -- direct.
      rU = phi(u); 
#endif
      rV = phi(uV); rI = phi(uI);

      // now update weights based on rU, RV, the 2nd filtered PSP and
      // the pspSP
      for(int i = 0; i < NPRE; i++) {
	updateWeight(wP + i, rU, *(pspTildeP+i), rV, *(pspSP+i));
      }
#ifdef TAUEFF
      /**
	 write rU to postF, but only for the last run of the
	 simulation and then only before the STIM_ONSET time --
	 ie it is the trained output without somatic drive.
       */
      if(s == TRAININGCYCLES - 1 && t < STIM_ONSET/DT) {
	fwrite(&rU, sizeof(double), 1, postF); 
      }
#else
      /**
	 for every 10th training cycle write all variables below to
	 postF in order:
       */
      if(s%(TRAININGCYCLES/10)==0) {
	fwrite(&rU, sizeof(double), 1, postF);
	fwrite(GE+t, sizeof(double), 1, postF);
	fwrite(&rV, sizeof(double), 1, postF);
	fwrite(&rI, sizeof(double), 1, postF);
	fwrite(&u, sizeof(double), 1, postF);
      }
      if(s == TRAININGCYCLES - 1) {
#ifdef RECORD_PREACT
	// for the last cycle also record the activity of the
	// presynaptic neurons
	fwrite(PRE_ACT + t * NPRE, sizeof(double), 20, preF);
	//fwrite(ouP, sizeof(double), 20, preF);
	fwrite(presP, sizeof(double), 20, preF);
#else
	// and the 1st and 2nd filtered PSP
	fwrite(pspSP, sizeof(double), 1, preF);
	fwrite(pspTildeP, sizeof(double), 1, preF);
#endif
      }
#endif
    }
  }
  
  fclose(preF);
  fclose(postF);
  
  return 0;
}
Пример #3
0
int main() {
	gsl_rng_env_setup();
	r = gsl_rng_alloc(gsl_rng_default);
	gsl_rng_set(r, RANDOM_SEED);
	initDerivedParams();
	
	FILE *outF = fopen("data/recurrent.dat", "w+b");
	FILE *wDF = fopen("data/recurrentWD.dat", "w+b");
	FILE *wF = fopen("data/recurrentW.dat", "w+b");
	
	gsl_rng_set(r, 0);
	
	gsl_vector *psp = gsl_vector_calloc(N);
	gsl_vector *pspS = gsl_vector_calloc(N);
	gsl_vector *sue = gsl_vector_calloc(N);
	gsl_vector *sui = gsl_vector_calloc(N);
	gsl_vector *pspTilde = gsl_vector_calloc(N);
	gsl_matrix *w  = gsl_matrix_calloc(N, N);
	gsl_vector *u  = gsl_vector_calloc(N);
	gsl_vector *uV  = gsl_vector_calloc(N);
	gsl_vector *pre  = gsl_vector_calloc(N);
	gsl_vector *rU  = gsl_vector_calloc(N);
	gsl_vector *rV  = gsl_vector_calloc(N);
	double *pspP = gsl_vector_ptr(psp,0);
	double *pspSP = gsl_vector_ptr(pspS,0);
	double *sueP = gsl_vector_ptr(sue,0);
	double *suiP = gsl_vector_ptr(sui,0);
	double *pspTildeP = gsl_vector_ptr(pspTilde,0);
	double *wP = gsl_matrix_ptr(w,0,0);
	double *uP = gsl_vector_ptr(u,0);
	double *uVP = gsl_vector_ptr(uV,0);
	double *preP = gsl_vector_ptr(pre,0);
	double *rUP = gsl_vector_ptr(rU,0);
	double *rVP = gsl_vector_ptr(rV,0);

	
	int stateLength = 2 * N ;
	double *state[stateLength];
	for(int i = 0; i < N; i++) {
		*(state + 0*N + i) = preP + i;
		*(state + 1*N + i) = rUP + i;
	} 
	
	gsl_vector_view tmpv; double gE, wij, wd, uI;

	for( int s = 0; s < TRAININGCYCLES+5; s++) {
		wd = 0;
		for( int t = 0; t < TIMEBINS; t++) {
			for( int i = 0; i < N; i++) {
				updatePre(sueP+i, suiP+i, pspP + i, pspSP + i, pspTildeP + i, *(preP + i) = spiking(*(rUP+i), gsl_rng_uniform_pos(r))); 
				tmpv = gsl_matrix_row(w, i);
				if(s < TRAININGCYCLES - 1) gE = *(GE + t*N + i);
				else if(t < 1*TIMEBINS/8 && s == TRAININGCYCLES + 3) gE = *(GE + i); 
				//&& t < 2*TIMEBINS/3 + TIMEBINS/NGROUPS/5
				else gE = 0;
				updateMembrane(uP+i, uVP+i, &uI, &tmpv.vector, psp, gE, 0);
				*(rUP+i) = phi(*(uP+i)); *(rVP+i) = phi(*(uVP+i));
				for(int j = 0; j < N; j++) {
					wij = *(wP + i*N + j);
					if(i != j) {
						updateWeight(wP + i*N + j, *(rUP+i), *(pspTildeP+j), *(rVP+i), *(pspSP+j));
						wd += (wij -  *(wP + i*N + j)) * (wij -  *(wP + i*N + j));
					}
					if( s == TRAININGCYCLES - 1 && t == TIMEBINS - 1) fwrite(&wij, sizeof(double), 1, wF);
				}
			}
			if(s > TRAININGCYCLES - 4) {
				for(int i=0; i<stateLength; i++) fwrite(*(state+i), sizeof(double), 1, outF);
			}
		}
		fwrite(&wd, sizeof(double), 1, wDF);
	}

	fclose(outF); 
	fclose(wDF); 
	fclose(wF); 
	
	return 0;
}