0
$\begingroup$
import numpy as np
from sklearn import datasets as ds

iris = ds.load_iris()
x = iris.data
y1 = iris.target

x = x / x.max()
y1 = np.matrix(y1)
np.random.seed(1)
y = np.zeros((y1.size, y1.max() + 1))
y[np.arange(y1.size), y1] = 1

class NeuralNetwork(object):

    def __init__(self):
        self.inputSize = 4
        self.outputSize = 3
        self.hiddenSize = 5

        self.W1 = np.random.randn(self.inputSize, self.hiddenSize) * 0.01
        self.W2 = np.random.randn(self.hiddenSize, self.outputSize) * 0.01
        self.b1 = np.random.randn(1, self.hiddenSize)
        self.b2 = np.random.randn(1, self.outputSize)

    def forward(self, x):
        self.z = np.dot(x, self.W1) + self.b1
        self.z2 = self.sigmoid(self.z)
        self.z3 = np.dot(self.z2, self.W2) + self.b2
        o = self.sigmoid(self.z3)
        return o

    def sigmoid(self, s):

        return 1 / (1 + np.exp(-s))

    def sigmoidPrime(self, s):

        return s * (1 - s)

    def backward(self, x, y, o):
        l = 0.2
        self.dz2 = y - o
        self.dw2 = (1 / 150 * self.dz2.T.dot(self.z)).T

        self.db2 = 1 / 150 * np.sum(self.dz2, axis=0).reshape(1, 3)

        self.dz1 = 1 / 150 * self.W2.dot(self.dz2.T).T * self.sigmoidPrime(self.z)

        self.dw1 = (1 / 150 * self.dz1.T.dot(x)).T

        self.db1 = 1 / 150 * np.sum(self.dz1, axis=0).reshape(1, 5)

        self.W1 = self.W1 - l * self.dw1

        self.W2 = self.W2 - l * self.dw2

        self.b1 = self.b1 - l * self.db1

        self.b2 = self.b2 - l * self.db2

    def train(self, x, y):
        o = self.forward(x)
        self.backward(x, y, o)

when I run this code it predicts always same class but when I change backward function to this one:

def backward(self, x, y, o):
    self.o_error = y - o
    self.o_delta = self.o_error * self.sigmoidPrime(o)
    self.z2_error = self.o_delta.dot(self.W2.T)
    self.z2_delta = self.z2_error * self.sigmoidPrime(self.z2)
    self.W1 += x.T.dot(self.z2_delta)
    self.W2 += self.z2.T.dot(self.o_delta)

It predicts correctly. Why first function doesn't work correctly (backward)?

$\endgroup$
2
$\begingroup$

I solve this problem with adjusting learning rate and adding regularization and some optimization

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.