【问题标题】:How to increase accuracy of a Feed-forwardNeural Network? [duplicate]如何提高前馈神经网络的准确性? [复制]
【发布时间】:2020-01-25 20:40:02
【问题描述】:

我在提高用 python 编码的前馈神经网络的准确性方面遇到问题。我不确定这是一个真正的错误还是我的数学函数的无能,但我得到的输出模棱两可(如 0.5)无论我增加多少迭代......我的代码:-

from numpy import exp, array, random, dot

class NeuralNetwork():

    def __init__(self):
        random.seed(1)
        self.synaptic_weights = 2 * random.random((3, 1)) - 1     # MM reuslt = 3 (3 * 1)

    def Sigmoid(self, x):
        return 1 / (1 + exp(-x))

    def Sigmoid_Derivative(self, x):
        return x * (1 - x)

    def train(self, Training_inputs, Training_outputs, iterations):
        output = self.think(Training_inputs)
        print ("THe outputs are: -", output)
        erorr = Training_outputs - output

        adjustment = dot(Training_inputs.T, erorr * self.Sigmoid_Derivative(output))
        print ("The adjustments are:-", adjustment)
        self.synaptic_weights += output

    def think(self, inputs):
        Training_inputs = array(inputs)
        return self.Sigmoid(dot(inputs, self.synaptic_weights))

# phew! the class ends..

if __name__ == "__main__":

    neural_network = NeuralNetwork()
    print("Random startin weights", neural_network.synaptic_weights)

    Training_inputs = array([[1, 1, 1], 
                             [0, 0, 0], 
                             [1, 0, 1],])                 # 3 rows * 3 columns???

    Training_outputs = array([[1, 1, 0]]).T

    neural_network.train(Training_inputs, Training_outputs, 0)

    print ("New synaptic weights after training: ")
    print (neural_network.synaptic_weights)

    # Test the neural network with a new situation.
    print ("Considering new situation [1, 0, 0] -> ?: ")
    print (neural_network.think(array([1, 0, 0])))

虽然这些是我的输出:=>

[Running] python -u "/home/neel/Documents/VS-Code_Projects/Machine_Lrn(PY)/test.py"
Random startin weights [[-0.16595599]
 [ 0.44064899]
 [-0.99977125]]
THe outputs are: - [[0.3262757 ]
 [0.5       ]
 [0.23762817]]
The adjustments are:- [[0.10504902]
 [0.14809799]
 [0.10504902]]
New synaptic weights after training: 
[[ 0.16031971]
 [ 0.94064899]
 [-0.76214308]]
Considering new situation [1, 0, 0] -> ?: 
[0.5399943]

[Done] exited with code=0 in 0.348 seconds

[Running] python -u "/home/neel/Documents/VS-Code_Projects/Machine_Lrn(PY)/tempCodeRunnerFile.py"
Random startin weights [[-0.16595599]
 [ 0.44064899]
 [-0.99977125]]
THe outputs are: - [[0.3262757 ]
 [0.5       ]
 [0.23762817]]
The adjustments are:- [[0.10504902]
 [0.14809799]
 [0.10504902]]
New synaptic weights after training: 
[[ 0.16031971]
 [ 0.94064899]
 [-0.76214308]]
Considering new situation [1, 0, 0] -> ?: 
[0.5399943]

[Done] exited with code=0 in 3.985 seconds

我已尝试更改迭代,但差异非常小。我认为问题可能出在我的一个数学(Sigmoid)函数中。除此之外,我认为第 20 行的点乘法可能是个问题,因为调整对我来说看起来很不自然......

另外,0.5 是否表示我的网络没有学习,因为它只是在随机猜测?

PS:-我认为我的问题不是重复的问题,因为它涉及所述模型的“准确性”,而相关问题涉及“不需要的输出”

【问题讨论】:

  • 我假设这只是一个让你熟悉前馈神经网络的练习,但我把它放在这里以防万一。查看 TensorflowKeras 了解可以为您完成繁重工作并使训练神经网络更加容易的库。

标签: python numpy machine-learning neural-network


【解决方案1】:

您的Sigmoid_Derivative 函数错误previous question of yours 中已经指出了这一点;应该是:

def Sigmoid_Derivative(self, x):
    return self.Sigmoid(x) * (1-self.Sigmoid(x))

请参阅 Math.SE 上的 Derivative of sigmoid function 线程,以及讨论 here

如果更正此问题仍无法达到预期结果,请不要更改上述问题 - 而是打开一个新问题...

【讨论】:

  • 感谢您的同情...我还想更新一下,Sigmoid 更改对准确性没有任何影响。我的帖子是关于模型的准确性,但 Prune 指出它是一个“重复”的问题......
  • @neelg 正如已经建议的那样,请打开一个新问题,但请记住:1) 你真的只用 三 (3) 个样本进行训练吗? 2) 准确度计算(或结果)未在您的代码中显示 - 请参阅如何创建 minimal reproducible example(强调 minimal...)
猜你喜欢
  • 2020-12-09
  • 2018-01-10
  • 2017-09-06
  • 2018-09-25
  • 2016-08-02
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多