本文介绍了Python神经网络中不需要的[Nan]输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手在这里.刚刚从JS切换到Python以构建神经网络,但从中获得[Nan]输出.

newbie here. Just switched over from JS to Python to build Neural nets but getting [Nan] outputs from it.

奇怪的是我的乙状结肠功能.似乎没有遇到任何溢出,但导数导致混乱.

The weird thing is that my sigmoid func. doesn't seem to encounter any overflow but the derivative causes chaos.

import numpy as np

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

#The function- 2

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

Training_inputs = np.array([[0,0,1],
                            [1,1,1],
                            [1,0,1],
                            [0,1,1]])

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

np.random.seed(1)

synaptic_weights = np.random.random((3, 1)) - 1

print ("Random starting synaptic weight:")
print (synaptic_weights)

for iteration in range(20000):
  Input_Layer = Training_inputs

  Outputs = sigmoid(np.dot(Input_Layer, synaptic_weights))

  erorr = Training_outputs - Outputs

  adjustments = erorr * Sigmoid_Derivative(Outputs)

  synaptic_weights += np.dot(Input_Layer.T, adjustments)

# The print declaration----------
print ("Synaptic weights after trainig:")
print (synaptic_weights)

print ("Outputs after training: ")
print (Outputs)

这是erorr消息.我不知道为什么它会溢出,因为权重似乎足够小.BTW Pls在我是新手的情况下以简单的python提供了解决方案:-

This is the erorr message. I dunno why it Overflowing because the weights seem to be small enough.BTW Pls give solutions in simple python as I am a newbie :--

Random starting synaptic weight:
[[-0.582978  ]
 [-0.27967551]
 [-0.99988563]]
/home/neel/Documents/VS-Code_Projects/Machine_Lrn(PY)/tempCodeRunnerFile.py:10: RuntimeWarning: overflow encountered in multiply
  return x * (1-x)
Synaptic weights after trainig:
[[nan]
 [nan]
 [nan]]
Outputs after training:
[[nan]
 [nan]
 [nan]
 [nan]]

推荐答案

您的代码至少存在两个问题.

There are at least two issues with your code.

第一个是在sigmoid函数中莫名其妙地使用了2个return语句,它们应该简单地是:

The first is the inexplicable use of 2 return statements in your sigmoid function, which should simply be:

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

给出x=0(0.5)的正确结果,而对于大x则为1:

which gives the correct result for x=0 (0.5), and goes to 1 for large x:

sigmoid(0)
# 0.5
sigmoid(20)
# 0.99999999793884631

您的(错误的)乙状结肠:

Your (wrong) sigmoid:

def your_sigmoid(x):
  return x*(1-x)
  return 1/(1 + np.exp(-x))

很容易导致溢出:

your_sigmoid(20)
# -380

另一个问题是您的派生词是错误的;应该是:

The other issue is that your derivative is wrong; it should be:

def Sigmoid_Derivative(x):
    return sigmoid(x) * (1-sigmoid(x))

请参见 Sigmoid函数的导数线程此处.

这篇关于Python神经网络中不需要的[Nan]输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:48