Exemple #1
0
void
testGetSynapses(backend_t backend, bool stdp)
{
	nemo::Configuration conf = configuration(stdp, 1024, backend);

	unsigned m = 1000;
	boost::scoped_ptr<nemo::Network> net1(nemo::torus::construct(4, m, stdp, 32, false));
	testGetSynapses(*net1, conf, 0, m);

	unsigned n0 = 1000000U;
	boost::scoped_ptr<nemo::Network> net2(createRing(1500, n0));
	testGetSynapses(*net2, conf, n0, 1);
}
void testMultipleNetwork() {
	log("******************************************");
	log("TESTING MultipleNetwork");
	log("REQUIRES Network class to have been tested");

	log("Creating an empty multiple network...",false);
	MultipleNetwork mnet;
	log("done!");

	log("Adding seven global vertexes with different methods...",false);
	global_vertex_id gv0 = mnet.addVertex();
	global_vertex_id gv1 = mnet.addVertex();
	global_vertex_id gv2 = mnet.addVertex();
	global_vertex_id gv3 = mnet.addVertex();
	mnet.addVertexes(3);
	if (mnet.getNumVertexes()!=7) throw FailedUnitTestException("Wrong number of global vertexes");
	log("done!");

	log("Creating and adding three anonymous undirected and directed networks...",false);
	// Network 1
	Network net1(false,false,false);
	vertex_id n1v0 = net1.addVertex();
	vertex_id n1v1 = net1.addVertex();
	vertex_id n1v2 = net1.addVertex();
	// Network 2
	Network net2(false,true,false);
	vertex_id n2v0 = net2.addVertex();
	vertex_id n2v1 = net2.addVertex();
	vertex_id n2v2 = net2.addVertex();
	// Network 3
	Network net3(false,true,false);
	vertex_id n3v0 = net3.addVertex();
	vertex_id n3v1 = net3.addVertex();
	vertex_id n3v2 = net3.addVertex();

	network_id n1 = mnet.addNetwork(net1);
	network_id n2 = mnet.addNetwork(net2);
	network_id n3 = mnet.addNetwork(net3);
	if (mnet.getNumNetworks()!=3) throw FailedUnitTestException("Wrong number of networks");
	log("done!");

	log("Specifying vertex mappings...",false);
	// To network 1
	mnet.map(gv0,n1v0,n1);
	mnet.map(gv1,n1v1,n1);
	mnet.map(gv2,n1v2,n1);
	// To network 2
	mnet.map(gv0,n2v0,n2);
	mnet.map(gv1,n2v1,n2);
	mnet.map(gv3,n2v2,n2);
	// To network 3
	mnet.map(gv0,n3v0,n3);
	mnet.map(gv2,n3v1,n3);
	mnet.map(gv3,n3v2,n3);
	if (mnet.getGlobalVertexId(n3v2,n3)!=gv3) throw FailedUnitTestException("Wrong mapping between global and local vertex");
	if (mnet.getLocalVertexId(gv3,n2)!=n2v2) throw FailedUnitTestException("Wrong mapping between global and local vertex");
	log("done!");

	/* behavior to be decided and tested
	log("Adding duplicate vertex mapping...",false);
	try {
		mnet.map(gv3,n3v2,n3);
		// should not arrive here
		throw FailedUnitTestException("Duplicate value non caught");
	}
	catch (DuplicateElementException& ex) {
		log("correctly thrown Exception!");
	}
	*/

	log("Adding five edges...",false); // they can also be added directly to the single networks
	mnet.getNetwork(n1)->addEdge(n1v0,n1v1);
	mnet.getNetwork(n2)->addEdge(n2v0,n2v1);
	mnet.getNetwork(n2)->addEdge(n2v1,n2v2);
	mnet.getNetwork(n3)->addEdge(n3v0,n3v2);
	mnet.getNetwork(n3)->addEdge(n3v1,n3v2);
	if (mnet.getNumEdges()!=5) throw FailedUnitTestException("Wrong number of global edges");
	log("done!");

	log("Mapping to a non existing network...",false);
	try {
		mnet.map(gv3,n2v2,n3+1);
		// should not arrive here
		throw FailedUnitTestException("Non existing network non caught");
	}
	catch (ElementNotFoundException& ex) {
		log("[FAIL] done!");
	}

	log("Mapping between non existing vertexes...",false);
	try {
		mnet.map(gv3,n2v2+1,n2);
		// should not arrive here
		throw FailedUnitTestException("Non existing vertex non caught");
	}
	catch (ElementNotFoundException& ex) {
		log("[FAIL] done!");
	}

	log("TEST SUCCESSFULLY COMPLETED (MultipleNetwork class - numeric identifiers)");

	log("Printing final multiple network information:");
	print(mnet);

}
void testMultilayerNetwork() {
	log("**************************************************************");
	log("TESTING basic multilayer network components (global_vertex_id, global_edge_id, network_id)...",false);
	/*
	 * these are normally automatically created
	 * by functions of the MultilayerNetwork class.
	 * However, here we test them by directly manipulating them.
	 */
	network_id nid1 = 0;
	network_id nid2 = 1;
	vertex_id vid1 = 0;
	vertex_id vid2 = 1;
	vertex_id vid3 = 0;
	global_vertex_id gvid1(vid1,nid1);
	global_vertex_id gvid2(vid2,nid1);
	global_vertex_id gvid3(vid3,nid2);
	// a directed edge
	global_edge_id e1(vid1,vid2,nid1,true);
	// another directed edge
	global_edge_id e2(vid2,vid1,nid1,true);
	// a third directed edge
	global_edge_id e3(vid2,vid1,nid1,true);
	// an undirected edge
	global_edge_id e4(vid1,vid2,nid1,false);
	// another undirected edge
	global_edge_id e5(vid2,vid1,nid1,false);
	if (e1==e2) throw FailedUnitTestException("Wrong edge_id comparison");
	if (e2!=e3) throw FailedUnitTestException("Wrong edge_id comparison");
	if (e4!=e5) throw FailedUnitTestException("Wrong edge_id comparison");
	log("done!");
	log("******************************************");
	log("TESTING MultilayerNetwork");
	log("REQUIRES Network class having been tested");

	log("Creating an empty multiple network...",false);
	MultilayerNetwork mnet;
	log("done!");

	log("Creating and adding three anonymous undirected and directed networks...",false);
	// Network 1
	Network net1(false,false,false);
	vertex_id n1v0 = net1.addVertex();
	vertex_id n1v1 = net1.addVertex();
	/*vertex_id n1v2 = */net1.addVertex();
	// Network 2
	Network net2(false,true,false);
	vertex_id n2v0 = net2.addVertex();
	vertex_id n2v1 = net2.addVertex();
	vertex_id n2v2 = net2.addVertex();
	// Network 3
	Network net3(false,true,false);
	vertex_id n3v0 = net3.addVertex();
	vertex_id n3v1 = net3.addVertex();
	vertex_id n3v2 = net3.addVertex();

	network_id n1 = mnet.addNetwork(net1);
	network_id n2 = mnet.addNetwork(net2);
	network_id n3 = mnet.addNetwork(net3);
	if (mnet.getNumNetworks()!=3) throw FailedUnitTestException("Wrong number of networks");
	log("done!");

	// TODO Check named networks

	log("Adding five edges...",false); // they can also be added directly to the single networks
	mnet.getNetwork(n1).addEdge(n1v0,n1v1);
	mnet.getNetwork(n2).addEdge(n2v0,n2v1);
	mnet.getNetwork(n2).addEdge(n2v1,n2v2);
	mnet.getNetwork(n3).addEdge(n3v0,n3v2);
	mnet.getNetwork(n3).addEdge(n3v1,n3v2);
	if (mnet.getNumEdges()!=5) throw FailedUnitTestException("Wrong number of global edges");
	log("done!");

	// Iterating through global edges and vertexes
	std::set<global_vertex_id> vertexes;
	std::set<global_edge_id> edges;

	log("TEST SUCCESSFULLY COMPLETED (MultilayerNetwork class - numeric identifiers)");

	log("Printing final multiple network information:");
	print(mnet);

}
/**
 * Second test of bpAlgSt class.
 */
void BpAlgStTest::test2(){
	/* ------------------------- */
	/* testing data and networks */
	/* ------------------------- */

	//training dataset
	Dataset set;
	set.setMinSize(1,1,1);
	set.setInput(0,0,-1);
	set.setOutput(0,0,1);

	//neural network
	MlnNetSt net;
	net.setInputCount(1);
	net.appendLayer();
	net.appendNeuron(0);

	//first inner layer neuron
	net[0][0][0] = 0.55;
	net[0][0].setBias(0.25);

	//second inner layer neuron
	net[0][1][0] = -0.48;
	net[0][1].setBias(-0.3);

	//output layer neuron
	net[1][0][0] = 0.12;
	net[1][0][1] = -0.18;
	net[1][0].setBias(0.1);

	//copy of neural network
	MlnNetSt net2(net);

	/* ------------------------------------------------------- */
	/* first back propagation iteration to get expected output */
	/* ------------------------------------------------------- */

	//learning coeficient
	double alpha = 1;

	//network output
	QList< QList<double> > out = net.layerOutput(set.inputVector(0));

	//output neuron delta calculation
	double dOutX1 = net[1][0][0]*out[1][0] + net[1][0][1]*out[1][1] + net[1][0].bias();
	double dOut = (set.output(0,0)-out[2][0]) * net[0][0].trFcnD(dOutX1);

	//first inner neuron delta
	double dInX1 = net[0][0][0]*out[0][0] + net[0][0].bias();
	double dIn1 = (dOut*net[1][0][0]) * net[0][0].trFcnD(dInX1);

	//second inner neuron delta
	double dInX2 = net[0][1][0]*out[0][0] + net[0][1].bias();
	double dIn2 = (dOut*net[1][0][1]) * net[0][0].trFcnD(dInX2);

	//output neuron weight adaptation
	net[1][0][0] += alpha * dOut * out[1][0];
	net[1][0][1] += alpha * dOut * out[1][1];
	net[1][0].addBias(alpha * dOut);

	//first inner layer neuron
	net[0][0][0] += alpha * dIn1 * out[0][0];
	net[0][0].addBias(alpha * dIn1);

	//second inner layer neuron
	net[0][1][0] += alpha * dIn2 * out[0][0];
	net[0][1].addBias(alpha * dIn2);

	/* ------------------------------------------------------- */
	/* one back propagation iteration by implemented algorithm */
	/* ------------------------------------------------------- */

	BpAlgSt alg;
	alg.setDataset(&set);
	alg.setNetwork(&net2);
	alg.setAlpha(alpha);
	alg.setStopIteration(1);
	alg.start();

	/* -------------------------------------------------------- */
	/* compares neural networks after one iteration of learning */
	/* -------------------------------------------------------- */

	QCOMPARE(net.toString(), net2.toString());
}