34 KiB
34 KiB
In [107]:
import numpy as np
import struct
import scipy as scIn [108]:
def sigmoid(x):
x = np.clip(x, -500, 500)
return 1 / (1 + np.exp(-x))
def sigmoid_deriv(x):
s = sigmoid(x)
return s * (1 - s)
def tangens_hip(x):
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
def tangens_hip_deriv(x):
return 1 - x ** 2
def softmax(x):
return sc.special.softmax(x)
def softmax_deriv(x):
return np.ones_like(x)
def ReLu_deriv(x):
return np.where(x > 0, 1, 0)
def ReLu(x):
return np.maximum(x, 0)
def load_mnist_images(filename):
with open(filename, 'rb') as f:
magic, num_images, rows, cols = struct.unpack('>IIII', f.read(16))
image_data = np.frombuffer(f.read(), dtype=np.uint8)
images = image_data.reshape(num_images, rows * cols).astype(np.float32) / 255.0
return images
def load_mnist_labels(filename):
with open(filename, 'rb') as f:
magic, num_labels = struct.unpack('>II', f.read(8))
labels = np.frombuffer(f.read(), dtype=np.uint8)
return labels
def to_one_hot(label, num_classes=10):
one_hot = np.zeros((num_classes, 1))
one_hot[label] = 1.0
return one_hot
class NeuralNewtork:
def __init__(self, n, a=0.01):
self.num_neurons = n
self.alpha = a
self.layers = {
'weights': [],
'activation_function': [],
'derivative_function': [],
}
def add_layer(self, n, activation_func, w_min=-1.0, w_max=1.0):
weights = np.random.uniform(w_min, w_max, (n, self.num_neurons))
self.layers['weights'].append(weights)
self.num_neurons = n
if activation_func is not None:
self.layers['activation_function'].append(activation_func)
if activation_func == sigmoid:
self.layers['derivative_function'].append(sigmoid_deriv)
elif activation_func == ReLu:
self.layers['derivative_function'].append(ReLu_deriv)
elif activation_func == tangens_hip:
self.layers['derivative_function'].append(tangens_hip_deriv)
elif activation_func == softmax:
self.layers['derivative_function'].append(softmax_deriv)
else:
self.layers['activation_function'].append(None)
self.layers['derivative_function'].append(None)
def forward_propagation(self, input):
activation = []
raw_output = []
current_input = input
for i in range(len(self.layers['weights'])):
z = np.dot(self.layers['weights'][i], current_input)
raw_output.append(z)
if self.layers['activation_function'][i] is not None:
a = self.layers['activation_function'][i](z)
else:
a = z
activation.append(a)
current_input = a
return activation, raw_output
def back_propagation(self, activation, input, raw_output, expected_output):
N = expected_output.shape[0]
layer_output_delta = (2 * 1 / N) * (activation[-1] - expected_output)
layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)
layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)
self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta
for i in reversed(range(len(self.layers['weights']) - 1)):
layer_hidden_delta = layer_hidden_delta * self.layers['derivative_function'][i](
raw_output[i])
wejscie_lub_aktywacja = activation[i - 1] if i > 0 else input
layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)
self.layers['weights'][i] = self.layers['weights'][i] - self.alpha * layer_hidden_weight_delta
def dropout(self, input, expected_output, proc=0.5):
activation = []
raw_output = []
dropout_vectors = []
current_input = input
num_layers = len(self.layers['weights'])
for i in range(num_layers):
z = np.dot(self.layers['weights'][i], current_input)
raw_output.append(z)
if self.layers['activation_function'][i] is not None:
a = self.layers['activation_function'][i](z)
else:
a = z
if i < num_layers - 1:
vector = np.random.default_rng().integers(0, 2, size=a.shape)
dropout_vectors.append(vector)
a = a * vector * (1 / proc)
else:
dropout_vectors.append(None)
activation.append(a)
current_input = a
N = expected_output.shape[0]
layer_output_delta = (2 * 1 / N) * (activation[-1] - expected_output)
layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)
layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)
self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta
for i in reversed(range(num_layers - 1)):
layer_hidden_delta = dropout_vectors[i] * layer_hidden_delta * self.layers['derivative_function'][i](
raw_output[i])
wejscie_lub_aktywacja = activation[i - 1] if i > 0 else input
layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)
self.layers['weights'][i] = self.layers['weights'][i] - self.alpha * layer_hidden_weight_delta
if i > 0:
layer_hidden_delta = np.dot(self.layers['weights'][i].T, layer_hidden_delta)
def miniBatchGradientDescent(self, input, expected_output, n, proc=0.5):
num_samples, h = input.shape
for i in range(0, num_samples, n):
batch_input = input[i: i + n, :].T
batch_expected = expected_output[i: i + n, :].T
dropout_mask = []
activation = []
raw_output = []
Nr = batch_input.shape[1]
current_input = batch_input
for l in range(len(self.layers['weights'])):
z = np.dot(self.layers['weights'][l], current_input)
raw_output.append(z)
if self.layers['activation_function'][l] is not None:
a = self.layers['activation_function'][l](z)
else:
a = z
if l < len(self.layers['weights']) - 1:
mask = np.random.default_rng().integers(0, 2, size=a.shape)
a = a * mask * (1 / proc)
dropout_mask.append(mask)
else:
dropout_mask.append(None)
activation.append(a)
current_input = a
if self.layers['activation_function'][-1] == softmax:
layer_output_delta = ((activation[-1] - batch_expected)) / n
else:
layer_output_delta = (2 * 1 / Nr) * (activation[-1] - batch_expected)
layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)
layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)
self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta
for l in reversed(range(len(self.layers['weights']) - 1)):
layer_hidden_delta = dropout_mask[l] * layer_hidden_delta * self.layers['derivative_function'][l](
raw_output[l])
wejscie_lub_aktywacja = activation[l - 1] if l > 0 else batch_input
layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)
self.layers['weights'][l] = self.layers['weights'][l] - self.alpha * layer_hidden_weight_delta
if l > 0:
layer_hidden_delta = np.dot(self.layers['weights'][l].T, layer_hidden_delta)
def fit(self, input, expected_output):
input_data = (input.copy()).reshape(1, -1).T
activation, raw_output = self.forward_propagation(input_data)
self.back_propagation(activation, input_data, raw_output, expected_output)
def predict(self, input):
input_data = (input.copy()).reshape(-1, 1)
activation, _ = self.forward_propagation(input_data)
return activation[-1]
In [109]:
nn = NeuralNewtork(784, a=0.005)
nn.add_layer(40, ReLu, -0.1, 0.1)
nn.add_layer(10, None, -0.1, 0.1)
train_image = load_mnist_images('train-images.idx3-ubyte')
train_image_labels = load_mnist_labels('train-labels.idx1-ubyte')
test_image = load_mnist_images('t10k-images.idx3-ubyte')
test_image_labels = load_mnist_labels('t10k-labels.idx1-ubyte')
epochs = 350
n_train_samples = 1000
print("Nauka z Dropout:")
for epoch in range(epochs):
correct_train = 0
for i in range(n_train_samples):
x = train_image[i].reshape(-1, 1)
label = train_image_labels[i]
expected = to_one_hot(label)
nn.dropout(x, expected)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%")Nauka z Dropout: Epoka 1/350 | Skutecznosc: 20.80% Epoka 10/350 | Skutecznosc: 72.10% Epoka 20/350 | Skutecznosc: 77.30% Epoka 30/350 | Skutecznosc: 83.00% Epoka 40/350 | Skutecznosc: 85.10% Epoka 50/350 | Skutecznosc: 87.60% Epoka 60/350 | Skutecznosc: 89.70% Epoka 70/350 | Skutecznosc: 90.60% Epoka 80/350 | Skutecznosc: 91.70% Epoka 90/350 | Skutecznosc: 92.40% Epoka 100/350 | Skutecznosc: 93.20% Epoka 110/350 | Skutecznosc: 93.80% Epoka 120/350 | Skutecznosc: 93.70% Epoka 130/350 | Skutecznosc: 95.00% Epoka 140/350 | Skutecznosc: 95.40% Epoka 150/350 | Skutecznosc: 95.60% Epoka 160/350 | Skutecznosc: 95.80% Epoka 170/350 | Skutecznosc: 95.80% Epoka 180/350 | Skutecznosc: 96.20% Epoka 190/350 | Skutecznosc: 96.40% Epoka 200/350 | Skutecznosc: 96.60% Epoka 210/350 | Skutecznosc: 96.90% Epoka 220/350 | Skutecznosc: 96.90% Epoka 230/350 | Skutecznosc: 97.30% Epoka 240/350 | Skutecznosc: 97.20% Epoka 250/350 | Skutecznosc: 97.40% Epoka 260/350 | Skutecznosc: 97.60% Epoka 270/350 | Skutecznosc: 97.80% Epoka 280/350 | Skutecznosc: 97.70% Epoka 290/350 | Skutecznosc: 98.00% Epoka 300/350 | Skutecznosc: 97.90% Epoka 310/350 | Skutecznosc: 98.10% Epoka 320/350 | Skutecznosc: 97.90% Epoka 330/350 | Skutecznosc: 98.10% Epoka 340/350 | Skutecznosc: 97.90% Epoka 350/350 | Skutecznosc: 98.20%
In [110]:
print("Sprawdzamy skutecznosc")
correct_train = 0
for i in range(n_train_samples):
x = test_image[i].reshape(-1, 1)
label = test_image_labels[i]
expected = to_one_hot(label)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
print(f"Skutecznosc: {train_acc:.2f}%")Sprawdzamy skutecznosc Skutecznosc: 84.00%
In [111]:
nn = NeuralNewtork(784, a=0.1)
nn.add_layer(40, ReLu, -0.1, 0.1)
nn.add_layer(10, None, -0.1, 0.1)
epochs = 350
n_train_samples = 1000
X_train = train_image[:n_train_samples]
Y_train = np.zeros((n_train_samples, 10))
for i in range(n_train_samples):
Y_train[i, train_image_labels[i]] = 1.0
print("Nauka z miniBatchGradientDescent:")
for epoch in range(epochs):
nn.miniBatchGradientDescent(X_train, Y_train, n=100)
correct_train = 0
for i in range(n_train_samples):
prediction = nn.predict(train_image[i])
if np.argmax(prediction) == train_image_labels[i]:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%")Nauka z miniBatchGradientDescent: Epoka 1/350 | Skutecznosc: 43.00% Epoka 10/350 | Skutecznosc: 78.30% Epoka 20/350 | Skutecznosc: 85.60% Epoka 30/350 | Skutecznosc: 89.00% Epoka 40/350 | Skutecznosc: 91.60% Epoka 50/350 | Skutecznosc: 93.10% Epoka 60/350 | Skutecznosc: 93.80% Epoka 70/350 | Skutecznosc: 94.40% Epoka 80/350 | Skutecznosc: 95.10% Epoka 90/350 | Skutecznosc: 95.40% Epoka 100/350 | Skutecznosc: 95.90% Epoka 110/350 | Skutecznosc: 96.60% Epoka 120/350 | Skutecznosc: 96.60% Epoka 130/350 | Skutecznosc: 96.70% Epoka 140/350 | Skutecznosc: 97.10% Epoka 150/350 | Skutecznosc: 97.10% Epoka 160/350 | Skutecznosc: 97.20% Epoka 170/350 | Skutecznosc: 97.60% Epoka 180/350 | Skutecznosc: 97.80% Epoka 190/350 | Skutecznosc: 98.10% Epoka 200/350 | Skutecznosc: 98.00% Epoka 210/350 | Skutecznosc: 98.50% Epoka 220/350 | Skutecznosc: 98.70% Epoka 230/350 | Skutecznosc: 98.60% Epoka 240/350 | Skutecznosc: 98.80% Epoka 250/350 | Skutecznosc: 98.50% Epoka 260/350 | Skutecznosc: 99.20% Epoka 270/350 | Skutecznosc: 99.10% Epoka 280/350 | Skutecznosc: 99.20% Epoka 290/350 | Skutecznosc: 99.20% Epoka 300/350 | Skutecznosc: 99.30% Epoka 310/350 | Skutecznosc: 99.30% Epoka 320/350 | Skutecznosc: 99.30% Epoka 330/350 | Skutecznosc: 99.30% Epoka 340/350 | Skutecznosc: 99.40% Epoka 350/350 | Skutecznosc: 99.40%
In [128]:
print("Sprawdzamy skutecznosc")
correct_train = 0
for i in range(n_train_samples):
x = test_image[i].reshape(-1, 1)
label = test_image_labels[i]
expected = to_one_hot(label)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
print(f"Skutecznosc: {train_acc:.2f}%")Sprawdzamy skutecznosc Skutecznosc: 9.90%
In [113]:
nn = NeuralNewtork(784, a=0.02)
nn.add_layer(100, tangens_hip, -0.01, 0.01)
nn.add_layer(10, None, -0.1, 0.1)
epochs = 350
n_train_samples = 1000
X_train = train_image[:n_train_samples]
Y_train = np.zeros((n_train_samples, 10))
for i in range(n_train_samples):
Y_train[i, train_image_labels[i]] = 1.0
print("Nauka z miniBatchGradientDescent: (Tangens hiperboliczny)")
for epoch in range(epochs):
nn.miniBatchGradientDescent(X_train, Y_train, n=100)
correct_train = 0
for i in range(n_train_samples):
prediction = nn.predict(train_image[i])
if np.argmax(prediction) == train_image_labels[i]:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%")Nauka z miniBatchGradientDescent: (Tangens hiperboliczny) Epoka 1/350 | Skutecznosc: 59.80% Epoka 10/350 | Skutecznosc: 81.20% Epoka 20/350 | Skutecznosc: 84.50% Epoka 30/350 | Skutecznosc: 86.20% Epoka 40/350 | Skutecznosc: 87.30% Epoka 50/350 | Skutecznosc: 88.00% Epoka 60/350 | Skutecznosc: 89.10% Epoka 70/350 | Skutecznosc: 89.50% Epoka 80/350 | Skutecznosc: 90.10% Epoka 90/350 | Skutecznosc: 90.60% Epoka 100/350 | Skutecznosc: 90.90% Epoka 110/350 | Skutecznosc: 90.50% Epoka 120/350 | Skutecznosc: 91.00% Epoka 130/350 | Skutecznosc: 91.80% Epoka 140/350 | Skutecznosc: 92.10% Epoka 150/350 | Skutecznosc: 92.10% Epoka 160/350 | Skutecznosc: 92.50% Epoka 170/350 | Skutecznosc: 92.70% Epoka 180/350 | Skutecznosc: 92.90% Epoka 190/350 | Skutecznosc: 93.20% Epoka 200/350 | Skutecznosc: 93.50% Epoka 210/350 | Skutecznosc: 93.30% Epoka 220/350 | Skutecznosc: 93.70% Epoka 230/350 | Skutecznosc: 93.60% Epoka 240/350 | Skutecznosc: 93.70% Epoka 250/350 | Skutecznosc: 93.80% Epoka 260/350 | Skutecznosc: 94.00% Epoka 270/350 | Skutecznosc: 94.40% Epoka 280/350 | Skutecznosc: 94.50% Epoka 290/350 | Skutecznosc: 94.50% Epoka 300/350 | Skutecznosc: 94.40% Epoka 310/350 | Skutecznosc: 94.90% Epoka 320/350 | Skutecznosc: 94.90% Epoka 330/350 | Skutecznosc: 95.30% Epoka 340/350 | Skutecznosc: 95.30% Epoka 350/350 | Skutecznosc: 95.20%
In [114]:
print("Sprawdzamy skutecznosc (Hipreboliczny)")
correct_train = 0
for i in range(n_train_samples):
x = test_image[i].reshape(-1, 1)
label = test_image_labels[i]
expected = to_one_hot(label)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
print(f"Skutecznosc: {train_acc:.2f}%")Sprawdzamy skutecznosc (Hipreboliczny) Skutecznosc: 76.00%
In [115]:
nn = NeuralNewtork(784, a=0.01)
nn.add_layer(100, sigmoid, -0.1, 0.1)
nn.add_layer(10, None, -0.1, 0.1)
epochs = 350
n_train_samples = 1000
X_train = train_image[:n_train_samples]
Y_train = np.zeros((n_train_samples, 10))
for i in range(n_train_samples):
Y_train[i, train_image_labels[i]] = 1.0
print("Nauka z miniBatchGradientDescent: (Tangens Sigmoidalna)")
for epoch in range(epochs):
nn.miniBatchGradientDescent(X_train, Y_train, n=100)
correct_train = 0
for i in range(n_train_samples):
prediction = nn.predict(train_image[i])
if np.argmax(prediction) == train_image_labels[i]:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%")Nauka z miniBatchGradientDescent: (Tangens Sigmoidalna) Epoka 1/350 | Skutecznosc: 12.30% Epoka 10/350 | Skutecznosc: 38.10% Epoka 20/350 | Skutecznosc: 52.40% Epoka 30/350 | Skutecznosc: 56.50% Epoka 40/350 | Skutecznosc: 61.20% Epoka 50/350 | Skutecznosc: 65.50% Epoka 60/350 | Skutecznosc: 62.00% Epoka 70/350 | Skutecznosc: 64.50% Epoka 80/350 | Skutecznosc: 63.60% Epoka 90/350 | Skutecznosc: 65.60% Epoka 100/350 | Skutecznosc: 63.00% Epoka 110/350 | Skutecznosc: 66.90% Epoka 120/350 | Skutecznosc: 67.20% Epoka 130/350 | Skutecznosc: 68.50% Epoka 140/350 | Skutecznosc: 70.40% Epoka 150/350 | Skutecznosc: 66.30% Epoka 160/350 | Skutecznosc: 68.60% Epoka 170/350 | Skutecznosc: 69.50% Epoka 180/350 | Skutecznosc: 70.30% Epoka 190/350 | Skutecznosc: 67.90% Epoka 200/350 | Skutecznosc: 69.40% Epoka 210/350 | Skutecznosc: 69.50% Epoka 220/350 | Skutecznosc: 68.30% Epoka 230/350 | Skutecznosc: 68.50% Epoka 240/350 | Skutecznosc: 69.60% Epoka 250/350 | Skutecznosc: 70.00% Epoka 260/350 | Skutecznosc: 69.00% Epoka 270/350 | Skutecznosc: 69.20% Epoka 280/350 | Skutecznosc: 70.90% Epoka 290/350 | Skutecznosc: 71.20% Epoka 300/350 | Skutecznosc: 70.50% Epoka 310/350 | Skutecznosc: 69.90% Epoka 320/350 | Skutecznosc: 69.90% Epoka 330/350 | Skutecznosc: 71.40% Epoka 340/350 | Skutecznosc: 72.00% Epoka 350/350 | Skutecznosc: 71.90%
In [116]:
print("Sprawdzamy skutecznosc (Sigmoidalna)")
correct_train = 0
for i in range(n_train_samples):
x = test_image[i].reshape(-1, 1)
label = test_image_labels[i]
expected = to_one_hot(label)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
print(f"Skutecznosc: {train_acc:.2f}%")Sprawdzamy skutecznosc (Sigmoidalna) Skutecznosc: 65.30%
In [129]:
nn = NeuralNewtork(784, a=0.02)
nn.add_layer(100, ReLu, -0.1, 0.1)
nn.add_layer(10, softmax, -0.1, 0.1)
epochs = 350
n_train_samples = 1000
X_train = train_image[:n_train_samples]
Y_train = np.zeros((n_train_samples, 10))
for i in range(n_train_samples):
Y_train[i, train_image_labels[i]] = 1.0
print("Nauka z miniBatchGradientDescent: (Softmax:)")
for epoch in range(epochs):
nn.miniBatchGradientDescent(X_train, Y_train, n=100)
correct_train = 0
for i in range(n_train_samples):
prediction = nn.predict(train_image[i])
if np.argmax(prediction) == train_image_labels[i]:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
if (epoch + 1) % 10 == 0 or epoch == 0:
print(f"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%")Nauka z miniBatchGradientDescent: (Softmax:) Epoka 1/350 | Skutecznosc: 13.90% Epoka 10/350 | Skutecznosc: 14.40% Epoka 20/350 | Skutecznosc: 11.70% Epoka 30/350 | Skutecznosc: 11.70% Epoka 40/350 | Skutecznosc: 11.70% Epoka 50/350 | Skutecznosc: 11.70% Epoka 60/350 | Skutecznosc: 11.70% Epoka 70/350 | Skutecznosc: 11.70% Epoka 80/350 | Skutecznosc: 11.70%
[31m---------------------------------------------------------------------------[39m [31mKeyboardInterrupt[39m Traceback (most recent call last) [36mCell[39m[36m [39m[32mIn[129][39m[32m, line 19[39m [32m 15[39m nn.miniBatchGradientDescent(X_train, Y_train, n=[32m100[39m) [32m 16[39m [32m 17[39m correct_train = [32m0[39m [32m 18[39m [38;5;28;01mfor[39;00m i [38;5;28;01min[39;00m range(n_train_samples): [32m---> [39m[32m19[39m prediction = nn.predict(train_image[i]) [32m 20[39m [38;5;28;01mif[39;00m np.argmax(prediction) == train_image_labels[i]: [32m 21[39m correct_train += [32m1[39m [32m 22[39m [36mCell[39m[36m [39m[32mIn[108][39m[32m, line 224[39m, in [36mNeuralNewtork.predict[39m[34m(self, input)[39m [32m 222[39m [38;5;28;01mdef[39;00m predict(self, input): [32m 223[39m input_data = (input.copy()).reshape(-[32m1[39m, [32m1[39m) [32m--> [39m[32m224[39m activation, _ = self.forward_propagation(input_data) [32m 225[39m [38;5;28;01mreturn[39;00m activation[-[32m1[39m] [36mCell[39m[36m [39m[32mIn[108][39m[32m, line 90[39m, in [36mNeuralNewtork.forward_propagation[39m[34m(self, input)[39m [32m 86[39m raw_output = [] [32m 87[39m [32m 88[39m current_input = input [32m 89[39m [38;5;28;01mfor[39;00m i [38;5;28;01min[39;00m range(len(self.layers[[33m'weights'[39m])): [32m---> [39m[32m90[39m z = np.dot(self.layers[[33m'weights'[39m][i], current_input) [32m 91[39m raw_output.append(z) [32m 92[39m [32m 93[39m [38;5;28;01mif[39;00m self.layers[[33m'activation_function'[39m][i] [38;5;28;01mis[39;00m [38;5;28;01mnot[39;00m [38;5;28;01mNone[39;00m: [31mKeyboardInterrupt[39m:
In [118]:
correct_train = 0
for i in range(n_train_samples):
x = test_image[i].reshape(-1, 1)
label = test_image_labels[i]
expected = to_one_hot(label)
prediction = nn.predict(x)
if np.argmax(prediction) == label:
correct_train += 1
train_acc = (correct_train / n_train_samples) * 100
print(f"Skutecznosc: {train_acc:.2f}%")Skutecznosc: 9.90%