Created
July 15, 2015 08:16
-
-
Save dadeba/8ab1239cbb1bee32a345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math as math | |
import numpy as np | |
from chainer import Function, FunctionSet, gradient_check, Variable, optimizers | |
import chainer.functions as F | |
model = FunctionSet( | |
l1 = F.Linear(1, 10), | |
l3 = F.Linear(10, 1), | |
) | |
def forward(x_in,y_in): | |
x = Variable(x_in) | |
t = Variable(y_in) | |
h1 = F.relu(model.l1(x)) | |
y = model.l3(h1) | |
f = x*y | |
return F.mean_squared_error(f, t), f | |
def sol(x): | |
return math.exp(-(x/5.0))*math.sin(x) | |
optimizer = optimizers.SGD() | |
optimizer.setup(model.collect_parameters()) | |
xx = np.zeros( (21,1), dtype = np.float32 ) | |
yy = np.zeros( (21,1), dtype = np.float32 ) | |
for i in xrange(21): | |
x = (i) * 0.1 | |
y = sol(x) | |
xx[i][0] = x | |
yy[i][0] = y | |
for epoch in xrange(25000): | |
optimizer.zero_grads() | |
loss, yyy = forward(xx, yy) | |
loss.backward() | |
optimizer.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment