Python:循环中断的第二部分

2024-04-26 03:37:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个简单的反向传播脚本。现在,我的代码将经历一次迭代(即for j in xrange(1)),但是如果我尝试添加更多的迭代,脚本就会中断。你知道吗

我想我正在改变我的一些变量的格式,但我不知道为什么或者如何解决这个问题。你知道吗

import numpy as np
from sympy import *
import math

def nonlin(x,deriv=False):
    if(deriv==True):
        return x*(1-x)

    return 1/(1+np.exp(-x))

def frange(start, stop, step):
    i = start
    while i < stop:
        yield i
        i += step

A = []
B = []

for i in frange(-2,2,0.1):
    t = np.exp(-abs(i))*sin(i*math.pi)
    A.append(i)
    B.append(t)

X = np.array(A)
y = np.array(B)
# randomly initialize our weights with uniformly distributed [-.5,.5]
weight = np.random.uniform([-.5,.5])

syn0 = weight[0]
syn1 = weight[1]

for j in xrange(2):

# Feed forward through layers 0, 1, and 2
    a0 = X
    a1 = nonlin(np.dot(a0,syn0))
    a2 = nonlin(np.dot(a1,syn1))

# how much did we miss the target value?
    a2_error = y - a2

    if (j% 100) == 0:
        print "Error:" + str(np.mean(np.abs(a2_error)))

# in what direction is the target value?
# were we really sure? if so, don't change too much.
    s2_delta = a2_error*nonlin(a2,deriv=True)

# how much did each k1 value contribute to the k2 error (according to the weights)?
    a1_error = s2_delta.dot(syn1.T)

# in what direction is the target k1?
# were we really sure? if so, don't change too much.
    s1_delta = a1_error * nonlin(a1,deriv=True)

    syn1 += a1.T.dot(s2_delta)
    syn0 += a0.T.dot(s1_delta)

在一次迭代之后,我得到了一个输出的错误(这很好),但是在进一步运行循环时,它会给出以下错误:

Traceback (most recent call last):
  File "HW4.py", line 40, in <module>
    a1 = nonlin(np.dot(a0,syn0))
  File "HW4.py", line 9, in nonlin
    return 1/(1+np.exp(-x))

Tags: theina2ifa1nperrora0