Files
PSI/LAB4.ipynb

34 KiB

Laboratorium 4

In [107]:
import numpy as np
import struct
import scipy as sc

Kod laboratorium 3:

In [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]

Zadanie 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%

Zadanie 2

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%

Zadanie 3

a) Tangens Hipreboliczny:

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%

b) Sigmoidalna:

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%

c) Softmax:

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%
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[129], line 19
     15     nn.miniBatchGradientDescent(X_train, Y_train, n=100)
     16 
     17     correct_train = 0
     18     for i in range(n_train_samples):
---> 19         prediction = nn.predict(train_image[i])
     20         if np.argmax(prediction) == train_image_labels[i]:
     21             correct_train += 1
     22 

Cell In[108], line 224, in NeuralNewtork.predict(self, input)
    222     def predict(self, input):
    223         input_data = (input.copy()).reshape(-1, 1)
--> 224         activation, _ = self.forward_propagation(input_data)
    225         return activation[-1]

Cell In[108], line 90, in NeuralNewtork.forward_propagation(self, input)
     86         raw_output = []
     87 
     88         current_input = input
     89         for i in range(len(self.layers['weights'])):
---> 90             z = np.dot(self.layers['weights'][i], current_input)
     91             raw_output.append(z)
     92 
     93             if self.layers['activation_function'][i] is not None:

KeyboardInterrupt: 
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%