som02.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /** som02.cpp is a learning simulation.
  2. Learning phases are intermitted by test phases. In Test phases all stimuli of a stimulus set are presented.
  3. For data analysis spike files and other data files are marked with a postfix according to the loop number:
  4. TEST0, TEST1
  5. simulations using som02.cpp were published in:
  6. Using spatiotemporal correlations to learn topographic maps for invariant object recognition.
  7. Michler F, Eckhorn R, Wachtler T.
  8. J Neurophysiol. 2009 Aug;102(2):953-64. Epub 2009 Jun 3.
  9. */
  10. #include "sys.hpp"
  11. #include "debug.hpp"
  12. #include <iostream>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <objsimlibrary.hpp>
  17. #include <anyoption.h>
  18. #include <valarray>
  19. #include <time.h>
  20. #include "version.h"
  21. void DoNetworkPropertytest(SimLoop* MainSimLoop, VecDepressiveConnection* ConITIT, float StartWeight);
  22. ////////////////////
  23. int main(int argc, char** argv)
  24. {
  25. Debug(objsim::debug::init());
  26. turnOffAllDebug();
  27. Debug( dc::input.on() );
  28. std::string SimName("som02");
  29. cout << testfunction();
  30. cout << get_version_info();
  31. clock_t cpu_start, cpu_end;
  32. double cpu_time_used;
  33. ////////////////Begin//ProcessCommandLineArguments//////////////
  34. cpu_start = clock();
  35. std::string HomeDir(getenv("HOME"));
  36. std::string BaseDir(HomeDir+"/prog/objsim");
  37. std::string ConfigDir(".");
  38. std::string ConfigFile(ConfigDir+"/settings_"+SimName+".cfg");
  39. // 'a' = dummy; do not use -a !! (setOption without second para doesn't work!!)
  40. AnyOptionWrapper *myAnyWrap = new AnyOptionWrapper(argc, argv, ConfigFile.c_str());
  41. CSimStandardOptions StdOpt(myAnyWrap);
  42. SimModuleITLayer MITLayer(myAnyWrap);
  43. MITLayer.SetCmdLineOptions();
  44. InvarLayer InvarianceLayer(myAnyWrap);
  45. InvarianceLayer.SetCmdLineOptions();
  46. // SimModuleMovieInput MInput(myAnyWrap);
  47. smodMovieInput MInput(myAnyWrap);
  48. MInput.SetCmdLineOptions();
  49. AnyWrapParaStringCmdOnly(SimControlName, "");
  50. AnyWrapParaBool(TestSim, false);
  51. AnyWrapParaBool(LoadForwardWeights, false);
  52. //AnyWrapParaInt(ProbeSteps, 4);
  53. AnyWrapParaFloat(TauRec, 50);
  54. AnyWrapParaFloat(U_se, 0.08);
  55. AnyWrapParaFloat(ITExExWeights, 0.1);
  56. AnyWrapParaFloat(ITExExRange, 0.02);
  57. AnyWrapParaFloat(FWMaxWeights, 2.20);
  58. AnyWrapParaFloat(InhLearnRate, 0.00001);
  59. AnyWrapParaFloat(InhLearnBaseLine, 1);
  60. //AnyWrapParaFloat(RossumCp, 0.000001);
  61. //AnyWrapParaFloat(ITNormFrequency, 30);
  62. AnyWrapParaBool(RandomInputOrder,false);
  63. AnyWrapParaFloat(TestStimDur, 100);
  64. AnyWrapParaBool(Teach,false);
  65. AnyWrapParaFloat(ExExMinDelay, 3);
  66. AnyWrapParaFloat(ExExMaxDelay, 6);
  67. AnyWrapParaBool(QuadraticNorm, false);
  68. AnyWrapParaBool(Learn, true);
  69. AnyWrapParaBool(Scan, true);
  70. AnyWrapParaBool(PicSlideWinStimulus, false);
  71. AnyWrapParaInt(NWeightSaves, 2);
  72. // 0: save weights every trial, otherwise: number of trials with weight saving
  73. AnyWrapParaBool(StopSignal,false);
  74. AnyWrapParaBool(NetworkPropertyTest, false);
  75. AnyWrapParaBool(ScrambleExExWeights, false); //!< for second revision
  76. // 0: no test phases, 1: test after last two training phases
  77. AnyWrapParaInt(TestPhaseMode, 1);
  78. AnyWrapParaFloat(NetTestStartWeight,1);
  79. AnyWrapParaString(MovieInputMode, "Default");
  80. myAnyWrap->process();
  81. ////////////////End//ProcessCommandLineArguments//////////////
  82. bool VecCon=true;
  83. // bool VecCon=false;
  84. ////////////////Begin//InitializeGlobalSimLoop
  85. SimLoop* MainSimLoop = InitLibCSim();
  86. MainSimLoop->SetDataDirectory(StdOpt.DataDirectory.c_str());
  87. float dt=MainSimLoop->GetDeltaT(); // milli sec
  88. cout << "int main(): dt = " << dt << "\n";
  89. myAnyWrap->Save((MainSimLoop->GetDataDirectory() + "/backup_settings").c_str());
  90. delete myAnyWrap;
  91. // system(("cp progversion.txt "+MainSimLoop->GetDataDirectory()).c_str());
  92. // now in libcsim.cpp
  93. ////////////////End//InitializeGlobalSimLoop
  94. // bool Learn=true;
  95. if ((MInput.LearnRate == 0) || TestSim) {
  96. Learn=false;
  97. cout << "No Learning\n";
  98. }
  99. int i, j, k, sec, t;
  100. if (TestSim) StdOpt.LoadWeights=true;
  101. if (StdOpt.LoadWeights)
  102. {
  103. LoadForwardWeights=true;
  104. }
  105. ////////////////End//ProcessCommandLineArguments//////////////
  106. //////////////////////////////////////////////
  107. int InputNx, InputNy, NStimuli;
  108. //////// Setup IT Module
  109. layer* ITLayer=MITLayer.Setup(MainSimLoop, 5, StdOpt.LoadWeights, Learn, false, StdOpt.SaveInitialWeights);
  110. cout << "Setup InvLayer \n";
  111. fflush(stdout);
  112. // layer* InvLayer = InvarianceLayer.Setup(ITLayer, false);
  113. layer* InvLayer = InvarianceLayer.Setup(ITLayer, VecCon, StdOpt.SaveInitialWeights);
  114. //////////connections
  115. VecDepressiveConnection *ConITIT;
  116. if (ITExExWeights > 0) {
  117. ConITIT = new VecDepressiveConnection(ITLayer, ITLayer,csimInputChannel_NMDA_AMPA, TauRec, U_se);
  118. ConITIT->SetName("conIT_ExEx");
  119. if (MITLayer.LoadITWeights) ConITIT->Load();
  120. // else ConITIT->ConnectGaussianProb(ITExExRange, ITExExWeights, 2, ExExMaxDelay,ExExMinDelay, 0.5, true);
  121. else ConITIT->ConnectGaussian(ITExExRange, ITExExWeights, ExExMaxDelay,ExExMinDelay, true);
  122. if (ScrambleExExWeights) ConITIT->ScrambleSynTargets();
  123. if (StdOpt.SaveInitialWeights) {
  124. ConITIT->Save();
  125. }
  126. MainSimLoop->AddSimElement(ConITIT);
  127. }
  128. //////// Setup Input
  129. cout << "before minput.setup \n";
  130. fflush(stdout);
  131. MInput.Setup(csimInputChannel_AMPA, StdOpt.InputStrength, StdOpt.LoadWeights);
  132. // NStimuli = l1inp->GetNStimuli();
  133. if (RandomInputOrder) {
  134. MInput.SetInputMode(csimInputRandom);
  135. } else {
  136. MInput.SetInputMode(InputModeFromString(MovieInputMode));
  137. }
  138. cout << "after minput.setup \n";
  139. fflush(stdout);
  140. cout << "Calling MInput.ConnectTo\n";
  141. MInput.ConnectTo(ITLayer, LoadForwardWeights, Learn, VecCon, StdOpt.SaveInitialWeights);
  142. ////////////////////////////////////
  143. int trial=0;
  144. int step=0;
  145. sec =0;
  146. NStimuli = MInput.NTestStimuli;
  147. StdOpt.TestNSteps = int((250)*(NStimuli+1)/250.);
  148. cout << "StdOpt.TestNSteps=" << StdOpt.TestNSteps << "\n";
  149. if (TestSim) {
  150. StdOpt.NTrials = 1;
  151. cout << "Test (recall) ----------------------Simulation\n";
  152. } else cout << "NOtestsim------------------------\n";
  153. fflush(stdout);
  154. int TotalTime=0;
  155. int WeightSaveBase=StdOpt.NTrials;
  156. if (NWeightSaves > 0) {
  157. WeightSaveBase=StdOpt.NTrials/NWeightSaves;
  158. }
  159. MainSimLoop->Hallo();
  160. MainSimLoop->SaveSimInfo();
  161. int MacroTimeStep = MainSimLoop->GetMacroTimeStep();
  162. cpu_end=clock();
  163. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  164. cout << "Network is Initialized. Initialization needed " << cpu_time_used << " seconds \n ";
  165. cpu_start=clock();
  166. // network is now initialized
  167. if (!NetworkPropertyTest) { // normal main sim loop
  168. // main simulation loop
  169. for (trial=0; trial<StdOpt.NTrials; ++trial)
  170. {
  171. // test phase: test network with stimulus set
  172. // scan=start sim multiple times with different parameters
  173. // then only the last two learning periods should be followed by test trials
  174. if ((TestPhaseMode==1) && ((! Scan) || (trial >(StdOpt.NTrials-3)))) {
  175. // Invariance layer is turned of only in learning simulations, not in slowlyness test
  176. MainSimLoop->TurnOffLearning();
  177. if (Learn) {
  178. InvarianceLayer.TurnOn();
  179. }
  180. MainSimLoop->SetSimTag(("TEST"+stringify(trial)).c_str());
  181. MInput.SetInputMode(csimInputTest);
  182. for (step=0;step<StdOpt.TestNSteps;++step)
  183. {
  184. cpu_end=clock();
  185. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  186. cout << "TTest" << trial << " Step=" << step
  187. << " sec= " << dt*(sec++) << " cputime="
  188. << cpu_time_used << " sec \n ";
  189. fflush(stdout);
  190. // simulation of 1 MacroTimeStep
  191. for (t=0;t<MacroTimeStep;t++)
  192. {
  193. MainSimLoop->proceede(MacroTimeStep*step+t);
  194. }
  195. MainSimLoop->prepare(step);
  196. }
  197. cout << "removed sim tag\n";
  198. fflush(stdout);
  199. MainSimLoop->TurnOnLearning();
  200. if (RandomInputOrder) {
  201. MInput.SetInputMode(csimInputRandom);
  202. } else {
  203. MInput.SetInputMode(InputModeFromString(MovieInputMode));
  204. }
  205. } // if ((TestPhaseMode==1) && ((! Scan) || (trial >(StdOpt.NTrials-3)) || trial == 0 || trial == 1))
  206. MainSimLoop->SetSimTag("");
  207. // Invariance layer is turned of only in learning simulations, not in slowlyness test
  208. if (Learn) {
  209. InvarianceLayer.TurnOff();
  210. }
  211. cout << "TrialNr=" << trial << "\n";
  212. // l1inp->TurnOn();
  213. // learn
  214. for (step=0;step<StdOpt.NSteps;++step)
  215. {
  216. cpu_end=clock();
  217. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  218. cout << "TLearn" << trial << " Step=" << step
  219. << " sec= " << dt*(sec++) << " cputime="
  220. << cpu_time_used << " sec \n ";
  221. fflush(stdout);
  222. // simulation of one MacroTimeStep
  223. for (t=0;t<MacroTimeStep;t++)
  224. {
  225. MainSimLoop->proceede(MacroTimeStep*step+t);
  226. }
  227. cout << "prepare(step)\n";
  228. fflush(stdout);
  229. MainSimLoop->prepare(step);
  230. }
  231. // SaveWeights
  232. if (((trial+1) %WeightSaveBase) == 0) {
  233. cout << "SaveLearningWeights\n";
  234. fflush(stdout);
  235. MainSimLoop->SaveLearningWeights(trial);
  236. }
  237. if (SimControlName != "") system((BaseDir + "/scripts/mytksend " + SimControlName + " SetTrialNr " +stringify(trial)+ " &").c_str());
  238. }
  239. } else {
  240. DoNetworkPropertytest(MainSimLoop, ConITIT, NetTestStartWeight);
  241. }
  242. // delete Layer1;
  243. // delete Layer2;
  244. // delete l1l2con;
  245. // delete l1inp;
  246. cpu_end=clock();
  247. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  248. cout << "\n\n***********CpuTimeUsed= " << cpu_time_used << " seconds \n";
  249. if (StopSignal) system("bluetclbutton v4learn finished &");
  250. }
  251. void DoNetworkPropertytest(SimLoop* MainSimLoop, VecDepressiveConnection* ConITIT, float StartWeight)
  252. {
  253. // NetworkPropertyTest
  254. // measure: patch size during input, self sustained patch size
  255. int NetworkTestNSteps=100;
  256. float WeightFactor=0.9;
  257. int StepSize=8;
  258. int MacroTimeStep = MainSimLoop->GetMacroTimeStep();
  259. cout << "Starting Network Property Test \n";
  260. ConITIT->MultiplyAllTargetWeights(StartWeight);
  261. MainSimLoop->TurnOffLearning();
  262. for (int step=0;step<NetworkTestNSteps;++step)
  263. {
  264. // simulation of one MacroTimeStep
  265. for (int t=0;t<MacroTimeStep;t++)
  266. {
  267. MainSimLoop->proceede(MacroTimeStep*step+t);
  268. }
  269. cout << "prepare(step)\n";
  270. fflush(stdout);
  271. MainSimLoop->prepare(step);
  272. if ((step % StepSize) == StepSize-1) {
  273. StartWeight*=WeightFactor;
  274. cout << " -------> changing sim parameters, Factor=" << StartWeight << "\n";
  275. ConITIT->MultiplyAllTargetWeights(WeightFactor);
  276. }
  277. }
  278. }