Python 神经网络中不需要的 [Nan] 输出 [英] Unwanted [Nan] output in Python neural network

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

问题描述

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

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

奇怪的是我的 sigmoid 函数.似乎没有遇到任何溢出,但导数导致混乱.

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)

这是错误信息.我不知道为什么它会溢出,因为权重似乎足够小.顺便说一句请在简单的 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

你的(错误的)sigmoid:

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 函数的导数线程在 Math.SE,以及此处的讨论.

See the Derivative of sigmoid function thread at Math.SE, as well as the discussion here.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆