Skip to content

Commit

Permalink
fix: label and backprop (hunkim#21)
Browse files Browse the repository at this point in the history
* hotfix: label and backprop

* fix: data label must start from 0

tf.one_hot requires Y data labels begin with 0
  • Loading branch information
kkweon authored and hunkim committed Mar 1, 2017
1 parent de5eff1 commit de76473
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 142 deletions.
236 changes: 118 additions & 118 deletions data-04-zoo.csv
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
# https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data,,,,,,,,,,,,,,,,
# 1. animal name: (deleted),,,,,,,,,,,,,,,,
# 2. hair Boolean",,,,,,,,,,,,,,,,
# 3. feathers Boolean",,,,,,,,,,,,,,,,
# 4. eggs Boolean",,,,,,,,,,,,,,,,
# 5. milk Boolean",,,,,,,,,,,,,,,,
# 6. airborne Boolean",,,,,,,,,,,,,,,,
# 7. aquatic Boolean",,,,,,,,,,,,,,,,
# 8. predator Boolean",,,,,,,,,,,,,,,,
# 9. toothed Boolean",,,,,,,,,,,,,,,,
# 10. backbone Boolean",,,,,,,,,,,,,,,,
# 11. breathes Boolean",,,,,,,,,,,,,,,,
# 12. venomous Boolean",,,,,,,,,,,,,,,,
# 13. fins Boolean",,,,,,,,,,,,,,,,
# 14. legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,,
# 15. tail Boolean",,,,,,,,,,,,,,,,
# 16. domestic Boolean",,,,,,,,,,,,,,,,
# 17. catsize Boolean",,,,,,,,,,,,,,,,
# 18. type Numeric (integer values in range [1",7]),,,,,,,,,,,,,,,
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,1
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1
0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,4
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,1
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,1
1,0,0,1,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0,0,2,1,0,1,1
1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,6
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,1
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,7
0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,2
# 2. hair Boolean",,,,,,,,,,,,,,,,
# 3. feathers Boolean",,,,,,,,,,,,,,,,
# 4. eggs Boolean",,,,,,,,,,,,,,,,
# 5. milk Boolean",,,,,,,,,,,,,,,,
# 6. airborne Boolean",,,,,,,,,,,,,,,,
# 7. aquatic Boolean",,,,,,,,,,,,,,,,
# 8. predator Boolean",,,,,,,,,,,,,,,,
# 9. toothed Boolean",,,,,,,,,,,,,,,,
# 10. backbone Boolean",,,,,,,,,,,,,,,,
# 11. breathes Boolean",,,,,,,,,,,,,,,,
# 12. venomous Boolean",,,,,,,,,,,,,,,,
# 13. fins Boolean",,,,,,,,,,,,,,,,
# 14. legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,,
# 15. tail Boolean",,,,,,,,,,,,,,,,
# 16. domestic Boolean",,,,,,,,,,,,,,,,
# 17. catsize Boolean",,,,,,,,,,,,,,,,
# 18. type Numeric (integer values in range [0",6]),,,,,,,,,,,,,,,
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0
0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0
1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0
0,0,1,0,0,1,0,1,1,0,0,1,0,1,1,0,3
0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3
1,0,0,1,0,0,0,1,1,1,0,0,4,0,1,0,0
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0
0,1,1,0,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09-5-softmax_back_prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@
# Predicting animal type based on various features
xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
N = x_data.shape[0]
y_data = xy[:, [-1]]

print(x_data.shape, y_data.shape)
# y_data has labels from 0 ~ 6
# [0, 1, 2, 3, 4, 5, 6]
print("Y has one of the following values")
print(np.unique(y_data))

nb_classes = 7 # 1 ~ 7
# x_data.shape = (101, 16)
# 101 samples, 16 features
# y_data.shape = (101, 1)
# 101 samples, 1 label (0 ~ 6)
print("Shape of X data: ", x_data.shape)
print("Shape of Y data: ", y_data.shape)

X = tf.placeholder("float", [None, 16])
Y = tf.placeholder("int32", [None, 1]) # 1 ~ 7
nb_classes = 7 # 0 ~ 6

X = tf.placeholder(tf.float32, [None, 16])
Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6

Y_one_hot = tf.one_hot(Y, nb_classes) # one hot
Y_one_hot = tf.cast(tf.reshape(
Y_one_hot, [-1, nb_classes]), "float32") # one hot
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
Y_one_hot = tf.cast(Y_one_hot, tf.float32)

W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
Expand All @@ -40,20 +51,19 @@ def sigma_prime(x):
layer1 = tf.add(tf.matmul(X, W), b)
y_pred = sigma(layer1)

print(y_pred, Y_one_hot)


# diff
assert y_pred.shape.as_list() == Y_one_hot.shape.as_list()

diff = (y_pred - Y_one_hot)

# Back prop (chain rule)
d_layer1 = diff * sigma_prime(layer1)
d_layer1 = diff
d_b = 1 * d_layer1
d_w = tf.matmul(tf.transpose(X), d_layer1)
d_w = tf.matmul(tf.transpose(X), d_layer1) / N

# Updating network using gradients
learning_rate = 0.1
learning_rate = 0.5
train_step = [
tf.assign(W, W - learning_rate * d_w),
tf.assign(b, b - learning_rate *
Expand All @@ -69,24 +79,34 @@ def sigma_prime(x):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for step in range(2001):
for step in range(500):
sess.run(train_step, feed_dict={X: x_data, Y: y_data})
print(step, sess.run(acct_res, feed_dict={X: x_data, Y: y_data}))

if step % 10 == 0:
# At 450 Step, you should see an accuracy of 100%
acc = sess.run(acct_res, feed_dict={X: x_data, Y: y_data})
print("Step: {:5}\t Acc: {:.2%}".format(step, acc))

# Let's see if we can predict
pred = sess.run(prediction, feed_dict={X: x_data})
for p, y in zip(pred, y_data):
print("prediction: ", p, " true Y: ", y)
if p == y[0]:
msg = "Correct!\t Prediction: {:d}\t True Y: {:d}"
else:
msg = "Wrong! \t Prediction: {:d}\t True Y: {:d}"

print(msg.format(p, int(y[0])))

'''
prediction: 2 true Y: [ 3.]
prediction: 4 true Y: [ 4.]
prediction: 1 true Y: [ 1.]
prediction: 1 true Y: [ 1.]
prediction: 2 true Y: [ 2.]
prediction: 1 true Y: [ 1.]
prediction: 6 true Y: [ 6.]
prediction: 1 true Y: [ 1.]
prediction: 0 true Y: [ 7.]
prediction: 2 true Y: [ 2.]
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 3 True Y: 3
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 3 True Y: 3
Correct! Prediction: 3 True Y: 3
Correct! Prediction: 0 True Y: 0
Correct! Prediction: 0 True Y: 0
'''

0 comments on commit de76473

Please sign in to comment.