神经网络多项式回归不能拟合曲线

时间:2016-01-02 15:11:29

标签: python numpy machine-learning

我想通过使用神经网络和python numpy来适应一个正弦波,但是我的程序无法适应正弦波,我想我会错过反向传播的东西

我有一个问题,如果我使用神经网络进行回归,我仍需要在向前传播后进行sigmoid?

import numpy
import matplotlib.pyplot as plt 
from sklearn import preprocessing



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

def function(x):
    #return x**3 +10
    #return numpy.sin(x)
    return numpy.exp(-x) * numpy.sin(3 * x)

rng = numpy.random.RandomState(12345)

#data generate
x_1 = numpy.arange(0,4.2,0.2) # one order x 
x = preprocessing.scale(numpy.array([x_1,x_1**2,x_1**3,x_1**4]).T)
y =  function(x_1)

n_in = x.shape[1] # feature
n_out = 4# hidden unit

w_1 = numpy.asarray(
    rng.uniform(
        low = -numpy.sqrt(0.5 / (n_in )),
        high = numpy.sqrt(0.5 / (n_in )),
        size = (n_in, n_out)))
w_2 = numpy.asarray(
    rng.uniform(
        low = -numpy.sqrt(0.5/ (n_in )),
        high = numpy.sqrt(0.5 /(n_in )),
        size = (n_out,1)))
b_1 = numpy.asarray(
    rng.uniform(
        low = -numpy.sqrt(0.5/ (n_in )),
        high = numpy.sqrt(0.5 /(n_in )),
        size = (n_out)))
b_2 = numpy.asarray(
    rng.uniform(
        low = -numpy.sqrt(0.5/ (n_in )),
        high = numpy.sqrt(0.5 /(n_in )),
        size = (1)))

lr = 0.0001

for step in range(1000):
    activate_hidden = numpy.dot(x,w_1) + b_1 #forward 
    activate_output = numpy.dot(activate_hidden,w_2) + b_2 #forward
    delta_output = -(activate_output - numpy.reshape(y,(x_1.shape[0],1))) 
    w_2 =  w_2 + lr * (numpy.dot(activate_hidden.T, delta_output).mean())
    b_2 = b_2 + lr * delta_output.mean()
    delta_hidden = numpy.dot(delta_output, w_2.T) 
    w_1 =  w_1 + lr * (numpy.dot(x.T,delta_hidden).mean())
    b_1 = b_1 + lr * delta_hidden.mean()

activate_hidden = numpy.dot(x,w_1) + b_1
activate_output = numpy.dot(activate_hidden,w_2) + b_2

plt.subplot(121)
plt.plot(x_1,y)

plt.subplot(122)
plt.plot(x_1,activate_output)

plt.show()

1 个答案:

答案 0 :(得分:0)

您是通过乘以x * w_1,然后是w_2来计算网络激活。您需要具有隐藏图层激活功能,否则这将成为线性转换。

activate_hidden = numpy.dot(x,w_1)  # forward 
activate_output = numpy.dot(activate_hidden,w_2)  # forward

你也可以选择x * (w_1 * w_2)。您应该通过层之间的激活功能传递它。你可以考虑使用sigmoid,tanh或relu。