006-lab-002. one hot encoding,fancy softmax classification
@
lab-06-2-softmax_zoo_classifier.py
import tensorflow as tf
import numpy as np
tf.set_random_seed(777)
# 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
# Model will predict animal type basedd on various features
xy_train_data=np.loadtxt('data-04-zoo.csv',delimiter=',',dtype=np.float32)
x_train_data=xy_train_data[:,0:-1]
y_train_data=xy_train_data[:,[-1]]
x_train_data.shape,y_train_data.shape
# ((101, 16), (101, 1))
# 0 ~ 6
nb_classes=7
# [n,16]
X_placeholder=tf.placeholder(tf.float32,[None,16])
# [n,1]
# you have to convert 0-6 classes into one hot encoding
Y_placeholder=tf.placeholder(tf.int32,[None,1])
# you use tf.one_hot()
Y_one_hot=tf.one_hot(Y_placeholder,nb_classes)
# Note
# you have y_train_data like this form [[0],[3],...,[0]],
# y_train_data[:10]
# array([[0.],
# [0.],
# [3.],
# [0.],
# [0.],
# [0.],
# [0.],
# [3.],
# [3.],
# [0.]], dtype=float32),
# this is 2 dimension, shape=(?,1)
# When you convert y_train_data into one hot encoding,
# shape becomes like this 3 dimension, shape=(?,1,7)
# [[[1 0 0 0 0 0 0]],
# [[0 0 0 1 0 0 0]],
# ...,
# [[1 0 0 0 0 0 0]]]
# But, you want shape=(?,7) not (?,1,7) with 3 dimension
# To achieve this, you can use reshape()
print("one_hot",Y_one_hot)
# Y_one_hot: raw one hoot
# 1th argument of reshape(): what you want to reshape
# 2th argument of reshape(): shape you want
# -1 means None
# In conclusion, (-1,7)
# Before reshape(),
# shape is (?,1,7)
# [[[1 0 0 0 0 0 0]],
# [[0 0 0 1 0 0 0]],
# ...,
# [[1 0 0 0 0 0 0]]]
# After reshape(),
# shape is (-1,7)
# [[1 0 0 0 0 0 0],
# [0 0 0 1 0 0 0],
# ...,
# [1 0 0 0 0 0 0]]
# Shape we want is [-1,7]
reshaped_Y_one_hot=tf.reshape(Y_one_hot,[-1,nb_classes])
print("reshaped_Y_one_hot:",reshaped_Y_one_hot)
# [n,16][?,?]=[n,nb_classes]
# [?,?]=[16,nb_classes]
W_variable=tf.Variable(tf.random_normal([16,nb_classes]),name='weight')
b_variable=tf.Variable(tf.random_normal([nb_classes]),name='bias')
logits=tf.matmul(X_placeholder,W_variable)+b_variable
h_f_softmax_for_multinomial_classification=tf.nn.softmax(logits)
# loss_value function(cross entropy function)
# you will use tf.nn.softmax_cross_entropy_with_logits(),
# to find loss_value value
cost_value=tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=reshaped_Y_one_hot)
# This is identical,
# with cost_function=tf.reduce_mean(-tf.reduce_sum(Y_placeholder*tf.log(h_f_softmax_for_multinomial_classification),axis=1))
cost_function=tf.reduce_mean(cost_value)
gradient_descent_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.1)\
.minimize(cost_function)
prediction_value_from_argmax=tf.argmax(h_f_softmax_for_multinomial_classification,1)
compare_prediction_and_label=tf.equal(prediction_value_from_argmax,tf.argmax(reshaped_Y_one_hot,1))
accuracy=tf.reduce_mean(tf.cast(compare_prediction_and_label,tf.float32))
with tf.Session() as sess_object:
sess_object.run(tf.global_variables_initializer())
for step in range(2000):
sess_object.run(gradient_descent_optimizer,feed_dict={X_placeholder:x_train_data,Y_placeholder:y_train_data})
if step%100==0:
lossval,accval=sess_object.run([cost_function,accuracy],feed_dict={X_placeholder:x_train_data,Y_placeholder:y_train_data})
print("step:",step,"cost_value:",lossval,"accuracy:",accval)
# Let's see if you can predict well
prediction_from_test=sess_object.run(prediction_value_from_argmax\
,feed_dict={X_placeholder:x_train_data})
# flatten(): [[1],[0]] -> [1,0]
for prediction,ylabel in zip(prediction_from_test,y_train_data.flatten()):
print("[{}] Prediction: {} Y_placeholder: {}"\
.format(prediction==int(ylabel),prediction,int(ylabel)))
# step: 0 cost_value: 5.0335717 accuracy: 0.15841584
# step: 100 cost_value: 0.70060694 accuracy: 0.8019802
# step: 200 cost_value: 0.44047153 accuracy: 0.84158415
# step: 300 cost_value: 0.32050115 accuracy: 0.9207921
# step: 400 cost_value: 0.25219414 accuracy: 0.9306931
# step: 500 cost_value: 0.20821169 accuracy: 0.95049506
# step: 600 cost_value: 0.17746367 accuracy: 0.96039605
# step: 700 cost_value: 0.15468208 accuracy: 0.980198
# step: 800 cost_value: 0.13707595 accuracy: 0.980198
# step: 900 cost_value: 0.12303669 accuracy: 0.990099
# step: 1000 cost_value: 0.1115707 accuracy: 0.990099
# step: 1100 cost_value: 0.10202818 accuracy: 0.990099
# step: 1200 cost_value: 0.09396435 accuracy: 0.990099
# step: 1300 cost_value: 0.08706301 accuracy: 1.0
# step: 1400 cost_value: 0.08109256 accuracy: 1.0
# step: 1500 cost_value: 0.075879246 accuracy: 1.0
# step: 1600 cost_value: 0.071289755 accuracy: 1.0
# step: 1700 cost_value: 0.067220226 accuracy: 1.0
# step: 1800 cost_value: 0.063588396 accuracy: 1.0
# step: 1900 cost_value: 0.060328294 accuracy: 1.0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 4 Y_placeholder: 4
# [True] Prediction: 4 Y_placeholder: 4
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 4 Y_placeholder: 4
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 2 Y_placeholder: 2
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 2 Y_placeholder: 2
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 2 Y_placeholder: 2
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 4 Y_placeholder: 4
# [True] Prediction: 2 Y_placeholder: 2
# [True] Prediction: 2 Y_placeholder: 2
# [True] Prediction: 3 Y_placeholder: 3
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 1 Y_placeholder: 1
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 5 Y_placeholder: 5
# [True] Prediction: 0 Y_placeholder: 0
# [True] Prediction: 6 Y_placeholder: 6
# [True] Prediction: 1 Y_placeholder: 1