BackpropTrainer Model

This commit is contained in:
aliaksei-kudravets 2022-05-26 10:23:55 +02:00
parent 0f065584fa
commit c06d68e0c3
4 changed files with 97 additions and 3 deletions

View File

@ -31,11 +31,33 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 6,
"id": "04bbec40", "id": "04bbec40",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
"source": [] {
"ename": "ModuleNotFoundError",
"evalue": "No module named 'pybrain3'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpybrain3\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpickle\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpylab\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m \n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'pybrain3'"
]
}
],
"source": [
"import pybrain3\n",
"import pickle\n",
"import matplotlib.pylab as plt \n",
"from numpy import ravel\n",
"from pybrain3.tools.shortcuts import buildNetwork\n",
"from pybrain3.datasets import SupervisedDataSet\n",
"from pybrain3.supervised.trainers import BackpropTrainer \n",
"from pybrain3.tools.xml.networkwriter import NetworkWriter \n",
"from pybrain3.tools.xml.networkreader import NetworkReader"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",

72
neurone/main.py Normal file
View File

@ -0,0 +1,72 @@
import pybrain3
import pickle
import matplotlib.pylab as plt
from numpy import ravel
from pybrain3.tools.shortcuts import buildNetwork
from pybrain3.datasets import SupervisedDataSet
from pybrain3.supervised.trainers import BackpropTrainer
from pybrain3.tools.xml.networkwriter import NetworkWriter
from pybrain3.tools.xml.networkreader import NetworkReader
# https://www.machinelearningmastery.ru/how-to-configure-the-number-of-layers-and-nodes-in-a-neural-network/
class BackPropagationModel():
def __init__(self, metrics:int = 4,
predictions:int = 1,
input_layer:int = 4,
hidden_layer:int = 3,
output_layer:int = 1):
# 4 метрики, 1 предикшн
self.metrics = metrics
self.predictions = predictions
self.input_layer = input_layer
self.hidden_layer = hidden_layer
self.output_layer = output_layer
self.ds = SupervisedDataSet(metrics, predictions)
def activateModel(self):
self.net = buildNetwork(self.input_layer, self.hidden_layer, self.output_layer, bias=True)
self.trainer = BackpropTrainer(self.net, dataset=self.ds, momentum=0.1, learningrate=0.01, verbose=True, weightdecay=0.01)
self.trnerr, self.valerr = self.trainer.trainUntilConvergence()
plt.plot(self.trnerr, 'b', self.valerr, 'r')
plt.show()
def addDataToModel(self, input:list, target:list):
self.ds.addSample(inp=input, target=target)
def predict(self, data:list):
y = self.net.activate(data)
print(y)
return y
def saveModel(self):
fileObject = open('model.txt', 'wb')
pickle.dump(self.net, fileObject)
fileObject.close()
@staticmethod
def getModel():
fileObject = open('model.txt', 'rb')
net2 = pickle.load(fileObject)
fileObject.close()
return net2
model = BackPropagationModel()
model.addDataToModel([2, 3, 80, 1], [5])
model.addDataToModel([5, 5, 50, 2], [4])
model.addDataToModel([10, 7, 40, 3], [3])
model.addDataToModel([15, 9, 20, 4], [2])
model.addDataToModel([20, 11, 10, 5], [1])
model.activateModel()
model.saveModel()
# USE MODEL - >
# model = BackPropagationModel.getModel()
# print(model.activate([2, 3, 80, 1]))

BIN
neurone/model.txt Normal file

Binary file not shown.

View File