tensorflow-interview-problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as np 
import pandas as pd

import tensorflow as tf


NUM_DIGITS = 10


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


train_X = np.array([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)])
train_y = np.array([fizz_buzz_encode(i) for i in range(101, 2 ** NUM_DIGITS)])


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


# Our model is a standard 1-hidden-layer multi-layer-perceptron with ReLU
# activation. The softmax (which turns arbitrary real-valued outputs into
# probabilities) gets applied in the cost function.
def model(X, w_h, w_o):
h = tf.nn.relu(tf.matmul(X, w_h))
return tf.matmul(h, w_o)

# Our variables. The input has width NUM_DIGITS, and the output has width 4.
X = tf.placeholder("float", [None, NUM_DIGITS])
Y = tf.placeholder("float", [None, 4])

# How many units in the hidden layer.
NUM_HIDDEN = 100

# Initialize the weights.
w_h = init_weights([NUM_DIGITS, NUM_HIDDEN])
w_o = init_weights([NUM_HIDDEN, 4])

# Predict y given x using the model.
py_x = model(X, w_h, w_o)

# We'll train our model by minimizing a cost function.
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
# print("cost is: ", cost)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

# And we'll make predictions by choosing the largest output.
predict_op = tf.argmax(py_x, 1)

# Finally, we need a way to turn a prediction (and an original number)
# into a fizz buzz output
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()

# how many times for train?
for epoch in range(10000):
# Shuffle the data before each training iteration.
p = np.random.permutation(range(len(train_X)))
train_X, train_y = train_X[p], train_y[p]

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

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

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