神经网络对错误进行分类

时间:2019-12-21 21:08:59

标签: python numpy machine-learning neural-network classification

我制作了一个神经网络,当我将其与梯度下降拟合时,它会收敛以进行损失,但将零归类为错误。

  

示例:

     

(array([0.49191714,0.50808286]),array([1。,0。]))

     

(array([0.04342812,0.95657188​​]),array([0。,1。]))

它对值进行1级精细分类,但不能对0s进行分类。

代码:

class NeuralNetwork(object):
def __init__(self, layerSizes):
    layers = [(a, b) for a, b in zip(layerSizes[:-1], layerSizes[1:])]
    print(layers)
    self.weights = [np.ones(size) for size in layers]

@staticmethod
def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def sigmoidp(self, z):
    return self.sigmoid(z) * (1 - self.sigmoid(z))

def forward(self, _a):
    self.z = []
    self.a = [_a]
    for w in self.weights:
        _z = np.dot(_a, w)
        _a = self.sigmoid(_z)
        self.z.append(_z)
        self.a.append(_a)
    y_hat = self.a[-1]
    return y_hat

def costFunction(self, x, y):
    return sum(sum((1 / 2) * (self.forward(x) - y)))

def calculateDeltas(self, x, y):
    self.y_hat = self.forward(x)
    self.deltas = [np.multiply(-(y - self.y_hat), self.sigmoidp(self.z[-1]))]
    for a, z, w in zip(self.a[::1], self.z[:-1][::-1], self.weights[::-1]):
        delta = np.dot(self.deltas[-1], w.T) * self.sigmoidp(z)
        self.deltas.append(delta)
    return self.deltas

def weightGradients(self):
    self.dcost_dws = []
    for a, d in zip(self.a[:-1][::-1], self.deltas):
        dcost_dw = np.dot(a.T, d)
        self.dcost_dws.append(dcost_dw)
    return self.dcost_dws

def gradientDescent(self, x, y, lr, iterations):
    for iteration in range(1, iterations + 1):
        for i, dcost_dw in enumerate(self.dcost_dws[::-1]):
            self.weights[i] = self.weights[i] - lr * dcost_dw
        print(self.costFunction(x, y))
        self.calculateDeltas(x, y)
        self.weightGradients()

注意:

before training run the methods: forward, calculateDeltas and weightGradients to initialize some variables

0 个答案:

没有答案