【问题标题】:3-Layer Neural Network Getting Stuck in Local Minima3 层神经网络陷入局部最小值
【发布时间】:2023-03-14 21:08:01
【问题描述】:

我已经用 Python 编写了一个基于 this tutorial 的 3 层神经网络来玩摇滚、纸、剪刀,示例数据使用 -1 代表摇滚0用于纸张1 用于剪刀,以及与教程中类似的数组。每次运行时,我的功能似乎都陷入了相对最小值,我正在寻找一种方法来解决这个问题。程序如下。

#math module
import numpy as np

#sigmoid function converts numbers to percentages(between 0 and 1)
def nonlin(x, deriv = False):
    if (deriv == True): #sigmoid derivative is just
        return x*(1-x)#output * (output - 1)

    return 1/(1+np.exp(-x)) #print the sigmoid function

#input data: using MOCK RPS DATA, -1:ROCK, 0:PAPER, 1:SCISSORS
input_data = np.array([[1, 1, 1],
                    [0, 0, 0],
                    [-1, -1, -1],
                    [-1, 1, -1]])
#also for training
output_data = np.array([[1],
                    [0],
                    [-1],
                    [1]])

#random numbers to not get stuck in local minima for fitness
np.random.seed(1)

#create random weights to be trained in loop
firstLayer_weights = 2*np.random.random((3, 4)) - 1 #size of matrix
secondLayer_weights = 2*np.random.random((4, 1)) - 1

for value in xrange(60000): # loops through training

    #pass input through weights to output: three layers
    layer0 = input_data
    #layer1 takes dot product of the input and weight matrices, then maps them to sigmoid function
    layer1 = nonlin(np.dot(layer0, firstLayer_weights))
    #layer2 takes dot product of layer1 result and weight matrices, then maps the to sigmoid function
    layer2 = nonlin(np.dot(layer1, secondLayer_weights))

    #check computer predicted result against actual data
    layer2_error = output_data - layer2

    #if value is a factor of 10,000, so six times (out of 60,000),
    #print how far off the predicted value was from the data
    if value % 10000 == 0:
        print "Error:" + str(np.mean(np.abs(layer2_error))) #average error

    #find out how much to re-adjust weights based on how far off and how confident the estimate
    layer2_change = layer2_error * nonlin(layer2, deriv = True)

    #find out how layer1 led to error in layer 2, to attack root of problem
    layer1_error = layer2_change.dot(secondLayer_weights.T)
    #^^sends error on layer2 backwards across weights(dividing) to find original error: BACKPROPAGATION

    #same thing as layer2 change, change based on accuracy and confidence
    layer1_change = layer1_error * nonlin(layer1, deriv=True)

    #modify weights based on multiplication of error between two layers
    secondLayer_weights = secondLayer_weights + layer1.T.dot(layer2_change)
    firstLayer_weights = firstLayer_weights + layer0.T.dot(layer1_change)

如您所见,这部分是涉及的数据:

input_data = np.array([[1, 1, 1],
                       [0, 0, 0],
                       [-1, -1, -1],
                       [-1, 1, -1]])
#also for training
output_data = np.array([[1],
                        [0],
                        [-1],
                        [1]])

权重在这里:

firstLayer_weights = 2*np.random.random((3, 4)) - 1 #size of matrix
secondLayer_weights = 2*np.random.random((4, 1)) - 1

似乎在前一千代之后,权重在剩余的编译过程中以最低的效率正确,让我相信它们已经达到了一个相对最小值,如下所示:

有什么方法可以快速有效地解决这个问题?

【问题讨论】:

  • 为什么要手动设置随机种子?
  • @BlackBear 我不知道还有另一种方法可以解决。感谢您指出!您对另一种方法有什么建议吗?

标签: python machine-learning neural-network artificial-intelligence


【解决方案1】:

您的网络的一个问题是输出(layer2 的元素的值)只能在 0 和 1 之间变化,因为您使用的是 sigmoid 非线性。由于您的四个目标值之一是 -1 并且最接近的可能预测是 0,因此总会有至少 25% 的错误。以下是一些建议:

  1. 对输出使用 one-hot 编码:即具有三个输出节点——ROCKPAPERSCISSORS 各一个——并训练网络计算这些输出的概率分布(通常使用 softmax 和交叉熵损失)。

  2. 使网络的输出层成为线性层(应用权重和偏差,但不是非线性)。要么添加另一层,要么从当前输出层中移除非线性。

您可以尝试其他方法,但不太可能可靠地工作,因为您实际上是在处理分类数据而不是连续输出:

  1. 缩放您的数据,使训练数据中的所有输出都介于 0 和 1 之间。

  2. 使用产生介于 -1 和 1 之间的值的非线性(例如 tanh)。

【讨论】:

  • 非常感谢您的回复,我衷心感谢。在您注意到我的输入根本不允许 -1 作为响应后,我将该值更改为 0.5,以便计算机可以选择 0、0.5 或 1 作为响应,并且运行顺利。谢谢!
【解决方案2】:

在每次迭代后为权重添加一点噪音。这将使您的程序脱离局部最小值并改进(如果可能)。关于这方面的文献相当多。以http://paper.ijcsns.org/07_book/200705/20070513.pdf 为例。

【讨论】:

  • 感谢您的回答!您是否有此实现的示例,或者解释如何执行此操作?论文中涉及的数学相对难以理解。
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2013-02-14
  • 2018-12-04
  • 1970-01-01
  • 2011-03-28
  • 2016-09-02
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多