如何使用 PyBrain 将网络保存在 XML 文件中
在本文中,我们将了解如何使用Python中的 PyBrain 将网络保存在 XML 文件中。
一个网络由几个模块组成。这些模块一般用连接件连接。 PyBrain 为程序员提供了神经网络的支持。网络可以解释为无环有向图,其中每个模块都服务于顶点/节点的目的,并且连接被假定为边缘。
创建网络
为了创建网络,PyBrain 为我们提供了 pybrain.tools.shortcuts。我们可以从中导入 buildNetwork 快捷方式。源代码如下,
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)
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')
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')
输出:
FeedForwardNetwork-8
Modules:
[
Connections:
[
将创建的网络保存在 XML 文件中
我们可以使用 pybrain.tools.customxml 中的 NetworkWriter 库。它为我们提供了 writeToFile()函数,其语法如下所示,
Syntax: NetworkWriter.writeToFile()
Parameters:
- writeToFile(network, xml_file)
- network: The created network
- xml_file: The name of the xml file with extension
此函数将首先创建一个名称作为参数传递给函数的 XML 文件,并将创建的 myNetwork 保存在其中。
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')
下面是完整的实现:
正如您在输出中看到的,myNetwork.XML 文件是通过执行以下代码生成的。
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')
输出:
myNetwork.xml 文件: