概要
Sin問題をconvnetjsとtensorflowでやってみた。
学習データの用意
データ
Math.pi * 4
ラベル
Math.sin
モデルの作成
回帰 regression です。
入力、出力
convnetjsの場合
{
type: 'input',
out_sx: 1,
out_sy: 1,
out_depth: 1
}
{
type: 'regression',
num_neurons: 1
}
tensorflowの場合
X = tf.placeholder(tf.float32, [None, 1])
Y = tf.placeholder(tf.float32, [None, 1])
重みとバイアス
convnetjsの場合
{
type: 'fc',
num_neurons: 20,
activation: 'relu'
}
{
type: 'fc',
num_neurons: 20,
activation: 'tanh'
}
tensorflowの場合
w_h = tf.Variable(tf.random_uniform([1, 10], minval = low0, maxval = high0, dtype = tf.float32))
b_h = tf.Variable(tf.zeros([1, 10], dtype = tf.float32))
w_o = tf.Variable(tf.random_uniform([10, 1], minval = low1, maxval = high1, dtype = tf.float32))
b_o = tf.Variable(tf.zeros([1, 1], dtype = tf.float32))
活性化関数を使う
convnetjsの場合
activation: 'relu'
activation: 'tanh'
tensorflowの場合
h = tf.nn.sigmoid(tf.matmul(X, w_h) + b_h)
out = tf.matmul(h, w_o) + b_o
ロス値を定義
convnetjsの場合
stats.loss;
tensorflowの場合
loss = tf.nn.l2_loss(out - Y)
最適化の方法定義
convnetjsの場合
var trainer = new convnetjs.Trainer(net);
tensorflowの場合
train_op = tf.train.AdamOptimizer().minimize(loss)
セッションを定義して学習する。
convnetjsの場合
var point = new convnetjs.Vol(1, 1, 2);
point.w = pi;
stats = trainer.train(point, sin);
tensorflowの場合
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(10001):
sess.run(train_op, feed_dict = {
X: trainx[start:end],
Y: trainy[start:end]
})
テストデータで取り出す。
convnetjsの場合
var point = new convnetjs.Vol(1, 1, 2);
point.w[0] = pi;
var prediction = net.forward(point);
tensorflowの場合
yy = sess.run(out, feed_dict = {
X: validx
})
サンプルコード
convnetjsの場合
http://jsdo.it/ohisama1/gGSV
tensorflowの場合
https://github.com/ohisama/tensorflow-fizzbuzz