i due file sinaptici di nome who.dat e wih.dat:
- Codice: Seleziona tutto
import numpy as np
import cv2
import cv2 as cv
import sys
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)
self.bias_hidden = np.zeros((1, self.hidden_size))
self.bias_output = np.zeros((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def feedforward(self, X):
self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
self.hidden_output = self.sigmoid(self.hidden_activation)
self.output_activation = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
self.predicted_output = self.sigmoid(self.output_activation)
return self.predicted_output
def backward(self, X, y, learning_rate):
output_error = y - self.predicted_output
output_delta = output_error * self.sigmoid_derivative(self.predicted_output)
hidden_error = np.dot(output_delta, self.weights_hidden_output.T)
hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)
self.weights_hidden_output += np.dot(self.hidden_output.T, output_delta) * learning_rate
self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) * learning_rate
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
output = self.feedforward(X)
self.backward(X, y, learning_rate)
if epoch % 4000 == 0:
loss = np.mean(np.square(y - output))
print(f"Epoch {epoch}, Loss:{loss}")
IMAGE1 = 'day_open.jpg'
IMAGE2 = 'day_closed.jpg'
IMAGE3 = 'night_open.jpg'
IMAGE4 = 'night_closed.jpg'
IMAGE5 = 'DX.jpg'
IMAGE6 = 'SX.jpg'
IMAGE7 = 'day_open.jpg'
image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)
image5 = cv2.imread(IMAGE5)
image6 = cv2.imread(IMAGE6)
image7 = cv2.imread(IMAGE7)
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray_image1 = cv2.resize(gray_image1,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
gray_image2 = cv2.resize(gray_image2,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
gray_image3 = cv2.resize(gray_image3,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
gray_image4 = cv2.resize(gray_image4,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image5 = cv2.cvtColor(image5, cv2.COLOR_BGR2GRAY)
gray_image5 = cv2.resize(gray_image5,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image6 = cv2.cvtColor(image6, cv2.COLOR_BGR2GRAY)
gray_image6 = cv2.resize(gray_image6,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image7 = cv2.cvtColor(image7, cv2.COLOR_BGR2GRAY)
gray_image7 = cv2.resize(gray_image7,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image1 = gray_image1 / 255
gray_image2 = gray_image2 / 255
gray_image3 = gray_image3 / 255
gray_image4 = gray_image4 / 255
gray_image5 = gray_image5 / 255
gray_image6 = gray_image6 / 255
gray_image7 = gray_image7 / 255
X=np.array([gray_image1.reshape(10000),gray_image2.reshape(10000),gray_image3.reshape(10000),gray_image4.reshape(10000),gray_image5.reshape(10000),gray_image6.reshape(10000)])
y=np.array([[0,1,0,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,1,1,0],[0,1,0,1]])
print("output = ", y)
print(X.shape)
nn = NeuralNetwork(input_size=10000, hidden_size=300, output_size=4)
nn.train(X, y, epochs=700, learning_rate=0.8)
#caricamento pesi tra ingresso e strato di uscita
nn.weights_input_hidden = np.fromfile("wih.dat", dtype=float)
nn.weights_hidden_output = np.fromfile("who.dat", dtype=float)
print(len(nn.weights_input_hidden))
print(len(nn.weights_hidden_output))
print(f'training')
#
#salvataggio pesi tra ingresso e strato hidden
wih=np.dot(nn.weights_input_hidden, )
wih.tofile("wih.dat")
#salvataggio pesi tra strato hidden e uscita
who=np.dot(nn.weights_hidden_output)
who.tofile("who.dat")
print("Predictions after training:")
print(f' image1 {nn.feedforward(gray_image1.reshape(1,10000))}')
print(f' image2 {nn.feedforward(gray_image2.reshape(1,10000))}')
print(f' image3 {nn.feedforward(gray_image3.reshape(1,10000))}')
print(f' image4 {nn.feedforward(gray_image4.reshape(1,10000))}')
print(f' image5 {nn.feedforward(gray_image5.reshape(1,10000))}')
print(f' image6 {nn.feedforward(gray_image6.reshape(1,10000))}')
print(f' image7 {nn.feedforward(gray_image7.reshape(1,10000))}')
Devo cercare di gestire eventuali errori dovuti dalla eventuale mancanza di tali file.
In sostanza voglio far si che l' addestramento e tofile() partano solo se mancano i file who.dat e wih.dat, se invece i file sono presenti deve solo leggerli e "caricare" le sinapsi con tali valori.
Ho cercato in rete ma la gestione di errori per fromfile() e tofile() non esiste.
Come posso fare?

Elettrotecnica e non solo (admin)
Un gatto tra gli elettroni (IsidoroKZ)
Esperienza e simulazioni (g.schgor)
Moleskine di un idraulico (RenzoDF)
Il Blog di ElectroYou (webmaster)
Idee microcontrollate (TardoFreak)
PICcoli grandi PICMicro (Paolino)
Il blog elettrico di carloc (carloc)
DirtEYblooog (dirtydeeds)
Di tutto... un po' (jordan20)
AK47 (lillo)
Esperienze elettroniche (marco438)
Telecomunicazioni musicali (clavicordo)
Automazione ed Elettronica (gustavo)
Direttive per la sicurezza (ErnestoCappelletti)
EYnfo dall'Alaska (mir)
Apriamo il quadro! (attilio)
H7-25 (asdf)
Passione Elettrica (massimob)
Elettroni a spasso (guidob)
Bloguerra (guerra)




