Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@ohisama@github

# 概要

kerasのbackendで、fizzbuzz問題やってみた。

# 実行結果

``````0 [ 1.56234002]
100 [ 0.21391739]
200 [ 0.10527255]
300 [ 0.06537795]
400 [ 0.01701604]
500 [ 0.0023935]
600 [ 0.00431998]
700 [ 0.00739238]
800 [ 0.00098056]
900 [ 0.00199422]
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
buzz
fizz
52
53
fizz
buzz
56
fizz
58
59
fizzbuzz
61
62
fizz
64
buzz
fizz
67
68
fizz
buzz
71
fizz
73
74
fizzbuzz
76
77
fizz
79
buzz
fizz
82
83
fizz
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz
fizz
97
98
fizz
buzz

``````

# サンプルコード

``````from tensorflow.contrib.keras.python.keras import backend as K
import numpy as np

def binary_encode(i, num_digits):
return np.array([i >> d & 1 for d in range(num_digits)])

def fizz_buzz_encode(i):
if i % 15 == 0:
return np.array([0, 0, 0, 1])
elif i % 5  == 0:
return np.array([0, 0, 1, 0])
elif i % 3  == 0:
return np.array([0, 1, 0, 0])
else:
return np.array([1, 0, 0, 0])

def fizz_buzz(i, prediction):
return [str(i), "fizz", "buzz", "fizzbuzz"][prediction]

NUM_DIGITS = 8
dx = np.array([binary_encode(i, NUM_DIGITS) for i in range(101)])
dy = np.array([fizz_buzz_encode(i) for i in range(101)])

input_dim = 8
output_dim = 4
hidden_dim = 48
x = K.placeholder(shape = (None, input_dim), name = "x")
ytrue = K.placeholder(shape = (None, output_dim), name = "y")
W1 = K.random_uniform_variable((input_dim, hidden_dim), -1, 1, name = "W1")
W2 = K.random_uniform_variable((hidden_dim, output_dim), -1, 1, name = "W2")
b1 = K.random_uniform_variable((hidden_dim, ), -1, 1, name = "b1")
b2 = K.random_uniform_variable((output_dim, ), -1, 1, name = "b2")
params = [W1, b1, W2, b2]
hidden = K.tanh(K.dot(x, W1) + b1)
ypred = K.tanh(K.dot(hidden, W2) + b2)
loss = K.mean(K.square(ypred - ytrue), axis = -1)
train = K.function(inputs = [x, ytrue], outputs = [loss], updates = updates)
test = K.function(inputs = [x], outputs = [ypred])
for ep in range(1000):
for i in range(100):
st = train([[dx[i + 1]], [dy[i + 1]]])
if ep % 100 == 0:
print (ep, st[0])

for i in range(100):
pre = test([[dx[i + 1]]])
#print (pre[0])
print (fizz_buzz(i + 1, np.argmax(pre[0])))

``````

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?