- 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'
image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
res1 = cv.resize(gray_image1,(100, 100), interpolation = cv.INTER_CUBIC)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
res2 = cv.resize(gray_image2,(100, 100), interpolation = cv.INTER_CUBIC)
gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
res3 = cv.resize(gray_image3,(100, 100), interpolation = cv.INTER_CUBIC)
gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
res4 = cv.resize(gray_image4,(100, 100), interpolation = cv.INTER_CUBIC)
res1 = res1 / 255
res2 = res2 / 255
res3 = res3 / 255
res4 = res4 / 255
X = np.array([[res1], [res2], [res3], [res4]])
y = np.array([[0,0], [0,1], [1,0], [1,1]])
nn = NeuralNetwork(input_size=10000, hidden_size=500, output_size=2)
nn.train(X, y, epochs=20000, learning_rate=1)
output = nn.feedforward(X)
print("Predictions after training:")
print(X)
print(output)
Mi succede che mi da errore con l'immagine ridimensionata a 10000 pixel.
Se invece imposto la rete a 2 ingressi con relativi array impostati su
nn = NeuralNetwork(input_size=2, hidden_size=500, output_size=2)
la RNA funziona normalmente.
perché se inserisco in ingresso i pixel delle immagini mi da problemi invece con due ingressi funziona?
Aiutatemi per favore.
Messaggio di errore:
- Codice: Seleziona tutto
Traceback (most recent call last):
File "/home/fabio/Scrivania/backpropagation.py", line 85, in <module>
nn.train(X, y, epochs=20000, learning_rate=1)
File "/home/fabio/Scrivania/backpropagation.py", line 48, in train
output = self.feedforward(X)
File "/home/fabio/Scrivania/backpropagation.py", line 26, in feedforward
self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
ValueError: shapes (4,1,100,100) and (10000,500) not aligned: 100 (dim 3) != 10000 (dim 0)

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)





