LoginSignup
0
1

More than 5 years have passed since last update.

convnetjsとtensorflow

Last updated at Posted at 2017-04-23

概要

XOR問題をconvnetjsとtensorflowでやってみた。

学習データの用意

データ

[[1, 1], [1, 0] , [0, 1], [0, 0]]

ラベル

[[0], [1] , [1], [0]]

モデルの作成

分類 classification です。

入力、出力

convnetjsの場合

{
    type: 'input',
    out_sx: 1,
    out_sy: 1,
    out_depth: 2
}
{
    type: 'softmax',
    num_classes: 2
}

tensorflowの場合

x_ = tf.placeholder(tf.float32, shape = [4, 2])
y_ = tf.placeholder(tf.float32, shape = [4, 1])

重みとバイアス

convnetjsの場合

{
    type: 'fc',
    num_neurons: 3,
    activation: 'tanh'
}

tensorflowの場合

w1 = tf.Variable(tf.random_uniform([2, 5], -1, 1))
w2 = tf.Variable(tf.random_uniform([5, 1], -1, 1))
b1 = tf.Variable(tf.zeros([5]))
b2 = tf.Variable(tf.zeros([1]))

活性化関数を使う

convnetjsの場合

activation: 'tanh'

tensorflowの場合

layer1 = tf.tanh(tf.matmul(x_, w1) + b1)
out = tf.tanh(tf.matmul(layer1, w2) + b2)
out = tf.add(out, 1)
out = tf.multiply(out, 0.5)

ロス値を定義

convnetjsの場合

stats.loss;

tensorflowの場合

loss = tf.reduce_mean(tf.square(Y - out)) 

最適化の方法定義

convnetjsの場合

var trainer = new convnetjs.Trainer(net);

tensorflowの場合

train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

セッションを定義して学習する。

convnetjsの場合

var point = new convnetjs.Vol(1, 1, 2);
point.w = [1、0];
stats = trainer.train(point, [1]);

tensorflowの場合

sess = tf.Session()
sess.run(init)
for i in range(10001):
    sess.run(train_step, feed_dict = {
        x_: X, 
       y_: Y
    })

テストデータで取り出す。

convnetjsの場合

var point = new convnetjs.Vol(1, 1, 2);
point.w = [1.0, 1.0];
var prediction = net.forward(point);

tensorflowの場合

print('Inference ', sess.run(out, feed_dict = {
   x_: X, 
   y_: Y
}))

サンプルコード

convnetjsの場合
http://jsdo.it/ohisama1/CeCv
tensorflowの場合
https://github.com/ohisama/tensorflow-fizzbuzz

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