神经网络不学习

时间:2021-01-30 18:23:34

标签: python numpy machine-learning neural-network

我是 AI 的初学者,我正在尝试使用 python 从头开始​​编写我自己的 AI 以修复我在理论上看到的问题。当我手动完成所有操作时,它工作正常,但是当我尝试创建更动态的网络时,它不会显示结果。

import numpy as np

class Network:
#inputs: n of inputs
#hidden: an array where length is the number of layers and the value the number of neurons
#outputs: n of outputs

dna = {}

def __init__(self, inputs, hidden, outputs):
    if len(hidden) >= 1:
        self.dna['w1'] = np.random.randn(inputs, hidden[0])
        self.dna['b1'] = np.zeros((1,hidden[0]))

        if len(hidden) == 1:
            self.dna['w2'] = np.random.randn(hidden[0], outputs)
            self.dna['b2'] = np.zeros((1, outputs))
            self.dna['size'] = 2

        elif len(hidden) == 2:
            self.dna['w2'] = np.random.randn(hidden[0], hidden[1])
            self.dna['b2'] = np.zeros((1, hidden[1]))

            self.dna['w3'] = np.random.randn(hidden[1], outputs)
            self.dna['b3'] = np.zeros((1, outputs))
            self.dna['size'] = 3

        else:
            for i in range(len(hidden)):
                if i == hidden[len(hidden) - 1 ]:
                    self.dna[self.__weigther(1 + i)] = np.random.randn(hidden[i], outputs)
                    self.dna[self.__biaer(1 + i)] = np.zeros((1, outputs))
                else:
                    self.dna[self.__weigther(1 + i)] = np.random.randn(hidden[i], hidden[i + 1])
                    self.dna[self.__biaer(1 + i)] = np.zeros((1, hidden[i + 1]))

            self.dna[self.__weigther(len(hidden) + 1)] = np.random.randn(hidden[len(hidden) - 1], outputs)
            self.dna[self.__biaer(len(hidden) + 1)] = np.zeros((1, outputs))
            self.dna['size'] = len(hidden) + 1
    else:
        self.dna['w1'] = np.random.randn(inputs, outputs)
        self.dna['b1'] = np.zeros((1, outputs))
        self.dna['size'] = 1

def __weigther(self, i):
    return "w" + str(i)

def __biaer(self, i):
    return "b" + str(i)

def size(self):
    return self.dna.get("size")

def printDNA(self):
    print(self.dna)

def sigmoid(self, x):
    return 1/(1+np.exp(-x))

def sigmoider(self, x):
    return self.sigmoid(x)*(1-self.sigmoid(x))

def __feedfoward(self, dataset):
    size = self.size()

    output = dataset
    no_sigs = [dataset]
    sigs = [0]

    for i in range(1, size + 1):
        output = np.dot(output, self.dna[self.__weigther(i)]) + self.dna[self.__biaer(i)]
        no_sigs.append(output)
        output = self.sigmoid(output)
        sigs.append(output)

    return [no_sigs, sigs]

def predict(self, inputs):
    outputs = self.__feedfoward(inputs)[1]
    print(outputs[len(outputs) - 1])
    return outputs[len(outputs) - 1]

def train(self, epoch, dataset, answers, lr):
    for i in range(epoch):
        outputs = self.__feedfoward(dataset)
        output = outputs[1][len(outputs[1]) - 1]

        error = output - answers

        cost = error
        deriv = self.sigmoider(outputs[0][len(outputs[0]) - 1])

        delta = np.dot(outputs[1][len(outputs[1]) - 1].T, cost * deriv)
        deltas = [delta]

        for l in range(len(outputs), 2, -1):
            if l == 2:
                dcost = error * deriv
                costh = np.dot(dcost, self.dna[self.__weigther(1)].T)
                set = dataset
                hd = np.dot(set.T, self.sigmoider(outputs[0][1]) * costh)
                deltas.append(hd)
            else:
                dcost = error * deriv
                costh = np.dot(dcost, self.dna[self.__weigther(l - 1)].T)
                set = outputs[0][l]
                hd = np.dot(set.T, self.sigmoider(outputs[0][l]) * costh)
                deltas.append(hd)

        for k in range(0, len(deltas)):
            self.dna[self.__weigther(k + 1)] -= lr * deltas[k]

            for num in deltas[k]:
                self.dna[self.__biaer(k + 1)] -= lr * num

这是测试文件:

     import Network as Network import numpy as np
     
     dataset = np.array([
        [0, 0],
         [0, 1],
         [1, 0],
         [1, 1] ])
     
     output = np.array([
         [0],
         [0],
         [0],
         [1] ])
     
     
     net = Network.Network(2, [2], 1) net.predict([0, 0]) net.predict([0,
     1]) net.predict([1, 0]) net.predict([1, 1]) print("#######")
     net.train(20, dataset, output, 0.5) net.predict([0, 0])
     net.predict([0, 1]) net.predict([1, 0]) net.predict([1, 1])

控制台显示它没有太大变化:

[[0.51265837]]
[[0.48473668]]
[[0.50538876]]
[[0.48698964]]
#######
[[0.50487598]]
[[0.48972348]]
[[0.50067702]]
[[0.49862653]]

改变纪元数和学习率不影响。提前致谢。

0 个答案:

没有答案
相关问题