LoginSignup
0
1

More than 3 years have passed since last update.

obd-python-gan-chapter2.2

Posted at

chapter2-2.py

import chainer.optimizers as Opt
import chainer.functions as F
import chainer.links as L
from chainer import Variable, Chain, config

import numpy as np
#import matplotlib.pyplot as plt

### Generate data
np.random.seed(123)
D=100
N=2

xdata = np.random.randn(D*N).reshape(D,N).astype(np.float32)

def f(x):
    y=x*x
    return y

tdata = (xdata[:,1] > f(xdata[:,0])).astype(np.int32)

### Neural network
C=2
#NN_ver1 = Chain(l1=L.Linear(N,C))
NN = Chain(l1=L.Linear(N,4), l2=L.Linear(4,C))

"""
def model_ver1(x):
    y=NN.l1(x)
    return y
"""

def model(x):
    h=NN.l1(x)
    h = F.sigmoid(h)
    y = NN.l2(h)
    return y

optNN = Opt.SGD()
optNN.setup(NN)

loss_series = []
acc_series  = []

T = 20000
for time in range(T):
    config.train = True
    optNN.target.zerograds()
    ydata = model(xdata)
    loss = F.softmax_cross_entropy(ydata, tdata)
    acc = F.accuracy(ydata, tdata)
    loss.backward()
    optNN.update()
    loss_series.append(loss.data)
    acc_series.append(acc.data)

for time in range(0,T,100):
    print(time, loss_series[time], acc_series[time])

Result (20 seconds on obd)

0 0.6227948 0.77
100 0.5632122 0.77
200 0.54191923 0.77
300 0.5280521 0.77
400 0.51627535 0.77
...
19600 0.15089221 0.95
19700 0.15005276 0.95
19800 0.1492205 0.95
19900 0.14839555 0.95

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1