概要
fizzbuzz問題をconvnetjsとtensorflowでやってみた。
学習データの用意
データ
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
ラベル
[1, 0, 0, 0]
モデルの作成
分類 classification です。
入力、出力
convnetjsの場合
{
type: 'input',
out_sx: 1,
out_sy: 1,
out_depth: 10
}
{
type: 'softmax',
num_classes: 4
}
tensorflowの場合
X = tf.placeholder("float", [None, 10])
Y = tf.placeholder("float", [None, 4])
重みとバイアス
convnetjsの場合
{
type: 'fc',
num_neurons: 10,
activation: 'tanh'
}
tensorflowの場合
w_h = tf.Variable(tf.random_normal([10, 100], stddev = 0.01))
w_o = tf.Variable(tf.random_normal([100, 4], stddev = 0.01))
活性化関数を使う
convnetjsの場合
activation: 'tanh'
tensorflowの場合
h = tf.nn.relu(tf.matmul(X, w_h))
out = tf.matmul(h, w_o)
ロス値を定義
convnetjsの場合
stats.loss;
tensorflowの場合
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = out, labels = Y))
最適化の方法定義
convnetjsの場合
var trainer = new convnetjs.Trainer(net);
tensorflowの場合
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
セッションを定義して学習する。
convnetjsの場合
var point = new convnetjs.Vol(1, 1, 10);
point.w = [0, 1, 0, 1, 0, 0, 0, 0, 0, 0];
stats = trainer.train(point, [1, 0, 0, 0]);
tensorflowの場合
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for epoch in range(10000):
sess.run(train_op, feed_dict = {
X: trX[start:end],
Y: trY[start:end]
})
テストデータで取り出す。
convnetjsの場合
var point = new convnetjs.Vol(1, 1, 10);
point.w = [0, 1, 0, 1, 0, 0, 0, 0, 0, 0];
var prediction = net.forward(point);
tensorflowの場合
teY = sess.run(predict_op, feed_dict = {
X: teX
})
サンプルコード
convnetjsの場合
http://jsdo.it/ohisama1/swGpU
tensorflowの場合
https://github.com/ohisama/tensorflow-fizzbuzz