# fizzbuzz的机器学习解法

624人阅读 评论(0)

fizzbuzz是面试的常见问题,内容为:输出0到100的数字，但是3的倍数输出Fizz，5的倍数输出Buzz，同时是3和5的倍数的输出FizzBuzz。

• fizz
• buzz
• fizzbuzz
• none

all in all,网络输入需要为二进制,我们需要将9表示为[1,0,0,1],我们取输入神经元为10个,达到1024的训练数据,避免作弊嫌疑.

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

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])

NUM_DIGITS = 10
trX = np.array([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)])
trY = np.array([fizz_buzz_encode(i)          for i in range(101, 2 ** NUM_DIGITS)])

NUM_HIDDEN = 100
X = tf.placeholder("float", [None, NUM_DIGITS])
Y = tf.placeholder("float", [None, 4])

def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))

#初始化
w_h = init_weights([NUM_DIGITS, NUM_HIDDEN])

#隐含层输出
def model(X, w_h, w_o):
h = tf.nn.relu(tf.matmul(X, w_h))
return tf.matmul(h, w_o)

py_x = model(X, w_h, w_o)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))

#定义网络输出
predict_op = tf.argmax(py_x, 1)
def fizz_buzz(i, prediction):
return [str(i), "fizz", "buzz", "fizzbuzz"][prediction]


BATCH_SIZE = 128

# Launch the graph in a session
with tf.Session() as sess:
tf.initialize_all_variables().run()

for epoch in range(10000):
# Shuffle the data before each training iteration.
p = np.random.permutation(range(len(trX)))
trX, trY = trX[p], trY[p]

# Train in batches of 128 inputs.
for start in range(0, len(trX), BATCH_SIZE):
end = start + BATCH_SIZE
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})

# And print the current accuracy on the training data.
print(epoch, np.mean(np.argmax(trY, axis=1) ==
sess.run(predict_op, feed_dict={X: trX, Y: trY})))

# And now for some fizz buzz
numbers = np.arange(1, 101)
teX = np.transpose(binary_encode(numbers, NUM_DIGITS))
teY = sess.run(predict_op, feed_dict={X: teX})
output = np.vectorize(fizz_buzz)(numbers, teY)

print(output)

个人资料
等级：
访问量： 7万+
积分： 1125
排名： 4万+
最新评论