How To Save The Network In XML File Using PyBrain
Last Updated :
20 Feb, 2022
In this article, we are going to see how to save the network in an XML file using PyBrain in Python.
A network consists of several modules. These modules are generally connected with connections. PyBrain provides programmers with the support of neural networks. A network can be interpreted as an acyclic directed graph where each module serves the purpose of vertex/node and connections are assumed to edge.
Creating a Network
To create a network, PyBrain provides us with pybrain.tools.shortcuts. We can import buildNetwork shortcuts from this. The source code is given below,
Python3
from pybrain.tools.shortcuts import buildNetwork
myNetwork = buildNetwork( 2 , 4 , 1 )
print (myNetwork)
|
Output:
FeedForwardNetwork-8
Modules:
[<BiasUnit ‘bias’>, <LinearLayer ‘in’>, <SigmoidLayer ‘hidden0’>, <LinearLayer ‘out’>]
Connections:
[<FullConnection ‘FullConnection-4’: ‘hidden0’ -> ‘out’>, <FullConnection ‘FullConnection-5’: ‘in’ -> ‘hidden0’>, <FullConnection ‘FullConnection-6’: ‘bias’ -> ‘out’>, <FullConnection ‘FullConnection-7’: ‘bias’ -> ‘hidden0’>]
Saving the created network in an XML file
we can use NetworkWriter library from pybrain.tools.customxml. It provides us writeToFile() function whose syntax is given below,
Syntax: NetworkWriter.writeToFile()
Parameters:
- writeToFile(network, xml_file)
- network: The created network
- xml_file: The name of the xml file with extension
This function will first create an XML file with the name passed as the parameter to the function and save the created myNetwork in it.
Python3
from pybrain.tools.customxml import NetworkWriter
NetworkWriter.writeToFile(myNetwork,
'myNetwork.xml' )
|
Below is the complete implementation:
As you can see in the output, the myNetwork.XML file is generated by executing the below code.
Python3
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.customxml import NetworkWriter
myNetwork = buildNetwork( 2 , 4 , 1 )
NetworkWriter.writeToFile(myNetwork, 'myNetwork.xml' )
|
Output:
myNetwork.xml file:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...