diff --git a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SimMongoLog.scala b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SimMongoLog.scala index 0bd647cbff11db264b25207943b6279bf34706ef..b57383483bda861b30024c5dad8fe48a7441300c 100644 --- a/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SimMongoLog.scala +++ b/n2s3/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/features/logging/SimMongoLog.scala @@ -211,7 +211,8 @@ class NeuronsFireLog(name : String,mongoCollection: MongoCollection[Document], o } override def destroy(): Unit = { - mongoCollection.insertMany(documents).subscribe(observer) + if (documents != List.empty){ + mongoCollection.insertMany(documents).subscribe(observer)} } } @@ -249,7 +250,8 @@ class NeuronsPotentialLog(name : String,mongoCollection: MongoCollection[Documen } override def destroy(): Unit = { - mongoCollection.insertMany(documents).subscribe(observer) + if (documents != List.empty){ + mongoCollection.insertMany(documents).subscribe(observer)} } } @@ -295,7 +297,8 @@ class LabelMonitoring(n2s3 : N2S3, outputNeuron : Seq[NeuronGroupRef],mongoColle } def destroy(): Unit = { - mongoCollection.insertMany(documents).subscribe(observer) + if (documents != List.empty){ + mongoCollection.insertMany(documents).subscribe(observer)} } } @@ -402,7 +405,8 @@ class SynapticWeight(list : Seq[Seq[Seq[ConnectionPath]]], refreshRate : Int = 1 } override def destroy(): Unit = { - mongoCollection.insertMany(documents).subscribe(observer) + if (documents != List.empty){ + mongoCollection.insertMany(documents).subscribe(observer)} } override def process(message: Message, sender : ActorRef): Unit = { diff --git a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleMnist2.scala b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleMnist2.scala index fa8a35e5cd2dcbd8552a77c80a744efc010c17c1..d98c98ff35a18f0b49b7bdb412c0078f0b56f1ca 100644 --- a/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleMnist2.scala +++ b/n2s3_examples/src/main/scala/fr/univ_lille/cristal/emeraude/n2s3/apps/ExampleMnist2.scala @@ -59,8 +59,8 @@ object ExampleMnist2 extends App { var logger: SimMongoLog = null var log = true var prune = false - var globalIteration = 6 - var sizeChunk = 10000 + var globalIteration = 1 + var sizeChunk = 60000 for(x <- 1 to 1) { implicit val timeout = Config.longTimeout @@ -102,14 +102,20 @@ object ExampleMnist2 extends App { var stream: MnistFileInputStreamP = null //inputStream.append(stream) - val unsupervisedLayer = n2s3.createNeuronGroup() + val unsupervisedLayer1 = n2s3.createNeuronGroup() .setIdentifier("Layer1") .setNumberOfNeurons(20) .setNeuronModel(LIF, Seq( (MembranePotentialThreshold, 35 millivolts))) + + val unsupervisedLayer2 = n2s3.createNeuronGroup() + .setIdentifier("Layer2") + .setNumberOfNeurons(10) + .setNeuronModel(LIF, Seq( + (MembranePotentialThreshold, 35 millivolts))) - inputLayer.connectTo(unsupervisedLayer, new FullConnection(() => new SimplifiedSTDP())) - unsupervisedLayer.connectTo(unsupervisedLayer, new FullConnection(() => new InhibitorySynapse())) + inputLayer.connectTo(unsupervisedLayer1, new FullConnection(() => new SimplifiedSTDP())) + var Layer1WTAconnection = unsupervisedLayer1.connectTo(unsupervisedLayer1, new FullConnection(() => new InhibitorySynapse())) n2s3.create() @@ -117,34 +123,23 @@ object ExampleMnist2 extends App { inputLayer.neurons.toList.foreach(n => { CTotal += n.group.connections.length }) - CTotal = CTotal * unsupervisedLayer.neurons.size + CTotal = CTotal * unsupervisedLayer1.neurons.size CRmaining = CTotal - //showHeatMap(inputLayer,unsupervisedLayer) - SynapsesIndex(inputLayer, unsupervisedLayer) + //showHeatMap(inputLayer,unsupervisedLayer1) + SynapsesIndexInput(inputLayer, unsupervisedLayer1) SimTime = formatter.format(new Date()) folderName = SimName.split("-")(0) + " [" + x + "] " + SimTime new File(folderName).mkdir() - //println("Start loading "+System.currentTimeMillis()) - //NetworkLoading.from(n2s3,"1600N/1600N-1 Arch 6 cropped") - //println("Done loading "+System.currentTimeMillis()) - //NetworkLoading.fromWithDefaultWeights(n2s3,"100N [1] 03-02-2020 23:50:43/100N-1 Archce - // 6 cropped","100N [1] 03-02-2020 23:50:43/100N-1 Arch 6 cropped",loadRandomWeights = false,binary = false) - saveCroppedSynapses(unsupervisedLayer, "Initial") - NetworkSaving.save(n2s3, croppedCnx, SynapseIndex, folderName + "/" + SimName + " Arch") - - // ImageSynapsesWeight.save(unsupervisedLayer.neurons.map(n => (n.getNetworkAddress, 128, 128)), 4, 1, "freeway.layer1.png") - - // - val inputToClassifierIndex = new ConnectionIndex(inputLayer, unsupervisedLayer) - var listOfConnexions = for (outputIndex <- 0 until unsupervisedLayer.shape.getNumberOfPoints) yield { + var inputToClassifierIndex = new ConnectionIndex(inputLayer, unsupervisedLayer1) + var listOfConnexions = for (outputIndex <- 0 until unsupervisedLayer1.shape.getNumberOfPoints) yield { for (inputX <- 0 until InputMnist.shape.dimensions(0)) yield { for { inputY <- 0 until InputMnist.shape.dimensions(1) input = inputLayer.getNeuronPathAt(inputX, inputY) - output = unsupervisedLayer.getNeuronPathAt(outputIndex) + output = unsupervisedLayer1.getNeuronPathAt(outputIndex) if inputToClassifierIndex.getConnectionsBetween(input, output).nonEmpty } yield inputToClassifierIndex.getConnectionsBetween(input, output).head } @@ -154,57 +149,62 @@ object ExampleMnist2 extends App { val labelTestFile = N2S3ResourceManager.getByName("mnist-test-labels").getAbsolutePath - while (iteration <= globalIteration) { simTime = "" - unsupervisedLayer.unfixNeurons() - n2s3.first = false - stream = InputMnist.DataParts(dataFile, labelFile, sizeChunk*(iteration-1), sizeChunk) + // L1 + unsupervisedLayer1.unfixNeurons() + stream = InputMnist.DataParts(dataFile, labelFile, sizeChunk, sizeChunk) inputStream.append(stream) - stream.goto() - println("Start Training ... " + "[" + iteration + "]") - if (iteration == 1 && log) { + println("Start Training L1") + + if (log) { logger = new SimMongoLog(n2s3, listOfConnexions, SimName.split("-")(0), true, true, true, true, "MNIST", SimTime, SynapseIndex) } duration = System.currentTimeMillis() - simTime = "Train " + iteration + simTime = "Train L1" n2s3.runAndWait() println(System.currentTimeMillis() - duration) simTime = simTime + " | " + (System.currentTimeMillis() - duration) + "\n" - if (iteration == globalIteration && log) { + if (log) { logger.storeSimInfoAndDestroy() } - //println("saving : " + System.currentTimeMillis()) - //NetworkSaving.save(n2s3, croppedCnx, SynapseIndex, folderName + "/" + SimName + " Arch " + iteration) - //println("end : " + System.currentTimeMillis()) - //println("saving cropped : " + System.currentTimeMillis()) - //saveCroppedSynapses(unsupervisedLayer, "[" + iteration + "]") - //println("end : " + System.currentTimeMillis()) - - /* - inputStream.append(InputMnist.DataFrom(dataTestFile, labelTestFile)) + // L2 + unsupervisedLayer1.fixNeurons() + Layer1WTAconnection.disconnect() - println("Start Testing " + "[" + iteration + "]") - - unsupervisedLayer.fixNeurons() - - n2s3.first = false - - benchmarkMonitor = n2s3.createBenchmarkMonitor(unsupervisedLayer) - - duration = System.currentTimeMillis() - simTime = simTime + "Test " + iteration + "\n" + duration - - n2s3.runAndWait() + unsupervisedLayer1.connectTo(unsupervisedLayer2, new FullConnection(() => new SimplifiedSTDP())) + var Layer2WTAconnection = unsupervisedLayer2.connectTo(unsupervisedLayer2, new FullConnection(() => new InhibitorySynapse())) + n2s3.first = false + stream = InputMnist.DataParts(dataFile, labelFile, sizeChunk, sizeChunk) + inputStream.append(stream) - simTime = simTime + " | " + (System.currentTimeMillis() - duration) + "\n" + println("Start Training L2") + + var layerTolayerIndex = new ConnectionIndex(unsupervisedLayer1, unsupervisedLayer2) + var listOfConnexions2 = for (outputIndex <- 0 until unsupervisedLayer2.shape.getNumberOfPoints) yield { + for (_ <- 0 until 1) yield { + for { + inputIndex <- 0 until unsupervisedLayer1.shape.getNumberOfPoints + input = unsupervisedLayer1.getNeuronPathAt(inputIndex) + output = unsupervisedLayer2.getNeuronPathAt(outputIndex) + if layerTolayerIndex.getConnectionsBetween(input, output).nonEmpty + } yield layerTolayerIndex.getConnectionsBetween(input, output).head + }} + + SynapsesIndex(unsupervisedLayer1, unsupervisedLayer2) + + if (log) { + logger = new SimMongoLog(n2s3, listOfConnexions2, SimName.split("-")(0), true, true, true, true, "MNIST", SimTime, SynapseIndex) + } + duration = System.currentTimeMillis() + simTime = "Train L2" + n2s3.runAndWait() - benchmarkMonitor.exportToHtmlView(folderName + "/" + "test " + SimName + " [" + iteration + "]" + ".html") - benchmarkMonitor.saveCrops(folderName + "/" + "Sim " + SimName + " info a:" + alpha + " no b:" + beta, simTime, " Ratio " + Ratio) -*/ + println(System.currentTimeMillis() - duration) + simTime = simTime + " | " + (System.currentTimeMillis() - duration) + "\n" if (prune) { Ratio = Ratio + (alpha * (CRmaining / CTotal)) @@ -215,34 +215,33 @@ object ExampleMnist2 extends App { // cropInput(n2s3.inputLayerRef.get, Ratio) } - if (iteration == globalIteration) { - inputStream.append(InputMnist.DataFrom(dataTestFile, labelTestFile)) + // Testing + inputStream.append(InputMnist.DataFrom(dataTestFile, labelTestFile)) - println("Start Testing cropped " + "[" + iteration + "]" + " with " + Ratio) + println("Start Testing cropped " + "[" + iteration + "]" + " with " + Ratio) - benchmarkMonitor = n2s3.createBenchmarkMonitor(unsupervisedLayer) + benchmarkMonitor = n2s3.createBenchmarkMonitor(unsupervisedLayer1) - duration = System.currentTimeMillis() - simTime = simTime + "Test cropped " + iteration + duration = System.currentTimeMillis() + simTime = simTime + "Test cropped " + iteration - n2s3.runAndWait() + n2s3.runAndWait() - println(System.currentTimeMillis() - duration) + println(System.currentTimeMillis() - duration) - simTime = simTime + " | " + (System.currentTimeMillis() - duration) + "\n" + simTime = simTime + " | " + (System.currentTimeMillis() - duration) + "\n" - saveCroppedSynapses(unsupervisedLayer, "cropped " + "[" + iteration + "]" + " with " + Ratio) - NetworkSaving.save(n2s3, croppedCnx, SynapseIndex, folderName + "/" + SimName + " Arch " + iteration + " cropped") + //saveCroppedSynapses(unsupervisedLayer1, "cropped " + "[" + iteration + "]" + " with " + Ratio) + //NetworkSaving.save(n2s3, croppedCnx, SynapseIndex, folderName + "/" + SimName + " Arch " + iteration + " cropped") - benchmarkMonitor.exportToHtmlView(folderName + "/" + "test cropped " + SimName + " [" + iteration + "]" + ".html") + benchmarkMonitor.exportToHtmlView(folderName + "/" + "test cropped " + SimName + " [" + iteration + "]" + ".html") - benchmarkMonitor.saveCrops(folderName + "/" + "Sim " + SimName + " info a:" + alpha + " b:" + beta, simTime, CropInfo + " Ratio " + Ratio) - } - if (iteration == globalIteration && log) { + //benchmarkMonitor.saveCrops(folderName + "/" + "Sim " + SimName + " info a:" + alpha + " b:" + beta, simTime, CropInfo + " Ratio " + Ratio) + + if (log) { logger.storeBenchmarkTestInfo(benchmarkMonitor) + logger.storeSimInfoAndDestroy() } - iteration += 1 - } } //logger.destroy() n2s3.destroy() @@ -288,7 +287,7 @@ object ExampleMnist2 extends App { }) } - def SynapsesIndex(inputL: InputNeuronGroupRef,layer : NeuronGroupRef): Unit = { + def SynapsesIndexInput(inputL: InputNeuronGroupRef,layer : NeuronGroupRef): Unit = { val inputToClassifierIndex = new ConnectionIndex(inputL, layer) var listOfConnexions = for(outputIndex <- 0 until layer.shape.getNumberOfPoints) yield { @@ -317,6 +316,38 @@ object ExampleMnist2 extends App { SynapseIndex = SynapseIndex.reverse } + def SynapsesIndex(layer1: NeuronGroupRef,layer2 : NeuronGroupRef): Unit = { + val layerTolayerIndex = new ConnectionIndex(layer1, layer2) + + var listOfConnexions = for (outputIndex <- 0 until layer2.shape.getNumberOfPoints) yield { + for (_ <- 0 until 1) yield { + for { + inputIndex <- 0 until layer1.shape.getNumberOfPoints + input = layer1.getNeuronPathAt(inputIndex) + output = layer2.getNeuronPathAt(outputIndex) + if layerTolayerIndex.getConnectionsBetween(input, output).nonEmpty + } yield layerTolayerIndex.getConnectionsBetween(input, output).head + }} + + + var x = -1 + var y = 0 + SynapseIndex = List[(Integer,Integer,Integer)]() + + listOfConnexions.head.foreach( + cnx => { + cnx.toList.foreach(c => { + x+=1 + SynapseIndex = SynapseIndex.::(c.connectionID,x,y) + }) + y+=1 + x = -1 + } + ) + + SynapseIndex = SynapseIndex.reverse + } + def saveCroppedSynapses(layer : NeuronGroupRef,name : String): Unit ={