Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dadeba
Created July 15, 2015 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadeba/8ab1239cbb1bee32a345 to your computer and use it in GitHub Desktop.
Save dadeba/8ab1239cbb1bee32a345 to your computer and use it in GitHub Desktop.
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