14. Multinomial Classification with TensorFlow

Softmax regression with tensorflow

tensorflow

TOC

Multinomial Classification with TensorFlow

  • Hypothesis of multinomial classification is softmax function of affine function.

$$ H(C) = S(W \cdot X + b) $$

  • Labels are usually represented with one-hot encoding

import tensorflow as tf
import matplotlib.pyplot as plt

# Number of factors
nb_factors = 4
# Number of Labels
nb_labels = 3

# Input data
x_data = [[1,2,1,1,], [2,1,3,2], [3,1,3,4],\
          [4,1,5,5,], [1,7,5,5], [1,2,5,6],\
          [1,6,6,6,], [1,7,7,7]]
# Labels
y_data = [[0,0,1], [0,0,1], [0,0,1],\
          [0,1,0], [0,1,0], [0,1,0],\
          [1,0,0], [1,0,0]]

# Placeholders for Inputs and Labels
X = tf.placeholder("float", [None, nb_factors])
Y = tf.placeholder("float", [None, nb_labels])

# Weight
W = tf.Variable(\
        tf.random_normal([nb_factors, nb_labels]),\
        name="weight")
# Bias
b = tf.Variable(tf.random_normal([nb_labels]),\
        name="bias")

# Affine function
affine = tf.matmul(X, W) + b
# Hypothesis - Softmax(Affine)
hypothesis = tf.nn.softmax(affine)
# Cost function - Cross Entropy Error
cost = tf.reduce_mean(-tf.reduce_sum(Y* tf.log(hypothesis), axis=1))
# Optimizer - Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(\
                learning_rate=0.1).minimize(cost)

costs= []
# Launch graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for step in range(1001):
        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
        costs.append(sess.run(cost, feed_dict={X: x_data, Y: y_data}))

costs_beginnig = costs[:100]
plt.plot(costs_beginnig)
plt.xlabel("trial")
plt.ylabel("cost")
plt.title("Cost of Multinomial Classification (0 - 100)")
plt.show()

plt.plot(costs)
plt.xlabel("trial")
plt.ylabel("cost")
plt.title("Cost of Multinomial Classification (0 - 2000)")
plt.show()
output_1_0
Image 1. Cost from 0 to 100 trials
output_1_1
Image 2. Cost

Test for Trained Hypothesis

import tensorflow as tf
import matplotlib.pyplot as plt

# Number of factors
nb_factors = 4
# Number of Labels
nb_labels = 3

# Input data
x_data = [[1,2,1,1,], [2,1,3,2], [3,1,3,4],\
          [4,1,5,5,], [1,7,5,5], [1,2,5,6],\
          [1,6,6,6,], [1,7,7,7]]
# Labels
y_data = [[0,0,1], [0,0,1], [0,0,1],\
          [0,1,0], [0,1,0], [0,1,0],\
          [1,0,0], [1,0,0]]

# Placeholders for Inputs and Labels
X = tf.placeholder("float", [None, nb_factors])
Y = tf.placeholder("float", [None, nb_labels])

# Weight
W = tf.Variable(tf.random_normal([nb_factors, nb_labels]), name="weight")
# Bias
b = tf.Variable(tf.random_normal([nb_labels]), name="bias")

# Affine
affine = tf.matmul(X, W) + b
# Hypothesis - Softmax(Affine)
hypothesis = tf.nn.softmax(affine)
# Cost function - Cross Entropy Error
cost = tf.reduce_mean(-tf.reduce_sum(Y* tf.log(hypothesis), axis=1))
# Optimizer - Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(\
                learning_rate=0.1).minimize(cost)

# Launch graph
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(1001):
    sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
    
result = sess.run(hypothesis, feed_dict={X:[[1,11,7,9],\
                                         [1,3,4,3],\
                                         [1,1,0,1]]})
oneHotResult = sess.run(tf.arg_max(result, 1))

print("Item   Prob.Label1   Prob.Label2   Prob.Label3   Class")
for i in range(len(result)):
    print("{0:4}   {1:10.9f}   {2:10.9f}   {3:10.9f}   {4:5}".format(\
           i, result[i][0], result[i][1], result[i][2], oneHotResult[i]))
Item   Prob.Label1   Prob.Label2   Prob.Label3   Class
   0   0.063795552   0.936126411   0.000078112       1
   1   0.691038668   0.254544556   0.054416835       0
   2   0.000003016   0.003177815   0.996819139       2

Softmax Cross Entropy With Logits

  • Multinomial classification is one of the most popular problem set. Therefore, TensorFlow provides softmax_cross_entropy_with_logits method to calculate cost simply.
  • Logit function is the inverse of sigmoid function. - Wiki

$$ l(x) = \log \frac{1-x}{x} $$

Image 3. Logit function(src: SRC)
  • Logit function and softmax function are integrated to softmax_cross_entropy_logits in order to transform the result of softmax function labels.
nb_labels = 3
Y = [1, 0, 2] # Not one-hot encoding
Y_ONE_HOT = tf.one_hot(Y, nb_labels)
Y_ONE_HOT = tf.reshape(Y_ONE_HOT, [-1, nb_labels])
# Affine
affine = tf.matmul(X, W) + b
# Cost function - Cross Entropy Error
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=affine, \
                                                 labels=Y_ONE_HOT)
cost = tf.reduce_mean(cost_i)
  • To use softmax_cross_entropy_with_logits(), labels should be one-hot encoding format.
  • This transformation of one-hot encoding is done by one_hot function form TensorFlow.
  • Unfortunately, one_hot() added one more rank. To remove the additional rank, please use reshape(). -1 in reshape means everything. Therefore, the final rank is None, 3.
  • Softmax_cross_entropy_with_logits() returns list of costs, so average should be calculated manually.

Animal Classification

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# Number of factors
nb_factors = 16
# Number of Labels
nb_labels = 7  # 0 ~ 6

# Read data
xy = np.loadtxt("data-04-zoo.csv", delimiter=",", dtype=np.float32)
# Input data
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

# Placeholders for Inputs and Labels
X = tf.placeholder(tf.float32, [None, nb_factors])
Y = tf.placeholder(tf.int32, [None, 1])  # Before one-hot encoding

Y_ONE_HOT = tf.one_hot(Y, nb_labels)
Y_ONE_HOT = tf.reshape(Y_ONE_HOT, [-1, nb_labels])

# Weight
W = tf.Variable(tf.random_normal([nb_factors, nb_labels]),\
        name="weight")
# Bias
b = tf.Variable(tf.random_normal([nb_labels]), name="bias")

# Affine
affine = tf.matmul(X, W) + b
# Hypothesis - Softmax(Affine)
hypothesis = tf.nn.softmax(affine)
# Cost function - Cross Entropy Error
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=affine, \
                                                 labels=Y_ONE_HOT)
cost = tf.reduce_mean(cost_i)
# Optimizer - Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(\
                learning_rate=0.1).minimize(cost)

# Prediction
prediction = tf.argmax(hypothesis, 1)
# Compare predictions and given labels
correct_prediction = tf.equal(prediction, tf.argmax(Y_ONE_HOT, 1))
# Accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

costs = []
accs = []
# Launch graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    for step in range(2001):
        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
        
        cost_val, acc_val = sess.run([cost, accuracy],
                                feed_dict={X: x_data, Y: y_data})
        costs.append(cost_val)
        accs.append(acc_val)
    
plt.plot(costs)
plt.xlabel("trial")
plt.ylabel("cost")
plt.title("Cost")
plt.show()

plt.plot(accs)
plt.xlabel("trial")
plt.ylabel("accuracy")
plt.title("Accuracy")
plt.show()
output_10_0
Image 4. Cost
output_10_1
Image 5. Accuracy

COMMENTS

Name

0 weights,1,abstract class,1,active function,3,adam,2,Adapter,1,affine,2,argmax,1,back propagation,3,binary classification,3,blog,2,Bucket list,1,C++,11,Casting,1,cee,1,checkButton,1,cnn,3,col2im,1,columnspan,1,comboBox,1,concrete class,1,convolution,2,cost function,6,data preprocessing,2,data set,1,deep learning,31,Design Pattern,12,DIP,1,django,1,dnn,2,Don't Repeat Your code,1,drop out,2,ensemble,2,epoch,2,favicon,1,fcn,1,frame,1,gradient descent,5,gru,1,he,1,identify function,1,im2col,1,initialization,1,Lab,9,learning rate,2,LifeLog,1,linear regression,6,logistic function,1,logistic regression,3,logit,3,LSP,1,lstm,1,machine learning,31,matplotlib,1,menu,1,message box,1,mnist,3,mse,1,multinomial classification,3,mutli layer neural network,1,Non Virtual Interface,1,normalization,2,Note,21,numpy,4,one-hot encoding,3,OOP Principles,2,Open Close Principle,1,optimization,1,overfitting,1,padding,2,partial derivative,2,pooling,2,Prototype,1,pure virtual function,1,queue runner,1,radioButton,1,RBM,1,regularization,1,relu,2,reshape,1,restricted boltzmann machine,1,rnn,2,scrolledText,1,sigmoid,2,sigmoid function,1,single layer neural network,1,softmax,6,softmax classification,3,softmax cross entropy with logits,1,softmax function,2,softmax regression,3,softmax-with-loss,2,spinBox,1,SRP,1,standardization,1,sticky,1,stride,1,tab,1,Template Method,1,TensorFlow,31,testing data,1,this,2,tkinter,5,tooltip,1,Toplevel,1,training data,1,vanishing gradient,1,Virtual Copy Constructor,1,Virtual Destructor,1,Virtual Function,1,weight decay,1,xavier,2,xor,3,
ltr
item
Universe In Computer: 14. Multinomial Classification with TensorFlow
14. Multinomial Classification with TensorFlow
Softmax regression with tensorflow
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s0/
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s72-c/
Universe In Computer
https://kunicom.blogspot.com/2017/07/toc-multinimoial-classification-with.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2017/07/toc-multinimoial-classification-with.html
true
2543631451419919204
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy