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
# Python program to demonstrate how to create a network # Import libraries from pybrain.tools.shortcuts import buildNetwork # Creating a network # This network has two inputs, four hidden # and one output neuron 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
# Python program for demonstrating how # to add the created network in an xml file # Importing library from pybrain.tools.customxml import NetworkWriter # Creating myNetwork.xml file and # saving the myNetwork into it 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
# Python program to demonstrate # how to save a network in an xml file # gfg.py # Import libraries from pybrain.tools.shortcuts import buildNetwork from pybrain.tools.customxml import NetworkWriter # Creating a network # This network has two inputs, # four hidden and one output neuron myNetwork = buildNetwork( 2 , 4 , 1 ) # Creating myNetwork.xml file and # saving the myNetwork into it NetworkWriter.writeToFile(myNetwork, 'myNetwork.xml' ) |
Output:
myNetwork.xml file: