Tuesday, March 24, 2020

[tensorflow certification] 2-4. What's the multinomial classification(machine learning which algorithm is explained by python programming)



Hello. I'm DVM who talk about tensorflow certification
Today, I will explain the basic Concept about Multinomial classification.
If you haven't seen the previous lecture, 
I recommend that watch the previous lecture.


First. What is the Multinomial Classification
Wikipedia said that
Multinomial classification is the problem of classifying instances into one of three or more classes. 



 Second. How to make Multinomial Classification.
The softmax function is used for multinomial classification.
The softmax function converts the scores to probabilities

Third. What is the one-hot encoding
one-hot encoding is choosing the max value and represent as 1.
So It is possible to learn the id of the thing classified.


Fourth. .Binary Versus Multinomial classification
Genarally, 
Binary classification use sigmoid as hypothesis function and binary cross entropy as cost function


 However,
Multinomial classification use softmax as hypothesis function and cross entropy as cost function
The main difference is that binary classification uses sigmoid and multinomial classification use softmax as hypothesis function or output layer in deep learning. 

 FIfth. Practice using tensorflow, 
04_REAL_Multinomial_classification

Let's make multinomial classification

In [0]:
import tensorflow as tf

Let's make an train data

In [0]:
x_data = [[0,0,0],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7,7,7]]
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]]

Let's make a node

In [0]:
X = tf.placeholder("float",[None,3]) # 3 dimension
Y = tf.placeholder("float",[None,3]) # 3 dimension

Let's make a hyper parameter such as weight and bias

In [0]:
multiclass =3
w = tf.Variable(tf.random_normal([3,multiclass]),name="weight")
b = tf.Variable(tf.random_normal([multiclass]),name="bias")

Let's make a cost function of cross entropy for multinomial classification

In [0]:
hypothesis = tf.nn.softmax(tf.matmul(X,w)+b)
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis),axis=1))

Let's make a train method of gradient descent opimizer

In [0]:
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)

Let's launch the graph using tf.seesion() and optimize hyper parameter

In [86]:
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(10000):
  sess.run(optimizer,feed_dict = {X:x_data,Y:y_data})
  if step%100 ==0:
    print(step, sess.run(cost,feed_dict={X:x_data,Y:y_data}))
0 5.958807
100 3.903429
200 1.9800371
300 1.1528699
400 1.1217395
500 1.1124523
600 1.1034646
700 1.0945827
800 1.0858052
900 1.0771327
1000 1.0685654
1100 1.0601037
1200 1.0517473
1300 1.0434968
1400 1.0353522
1500 1.0273135
1600 1.0193805
1700 1.011553
1800 1.0038315
1900 0.9962152
2000 0.988704
2100 0.98129755
2200 0.97399545
2300 0.96679723
2400 0.9597026
2500 0.9527109
2600 0.9458215
2700 0.93903375
2800 0.9323468
2900 0.92576003
3000 0.91927266
3100 0.91288376
3200 0.9065926
3300 0.9003983
3400 0.89429975
3500 0.8882961
3600 0.8823864
3700 0.8765695
3800 0.87084436
3900 0.86521006
4000 0.8596651
4100 0.8542086
4200 0.84883946
4300 0.84355617
4400 0.8383578
4500 0.8332432
4600 0.82821095
4700 0.8232603
4800 0.81838953
4900 0.8135977
5000 0.8088833
5100 0.80424523
5200 0.79968226
5300 0.7951931
5400 0.79077667
5500 0.7864314
5600 0.78215647
5700 0.7779504
5800 0.77381223
5900 0.76974046
6000 0.7657339
6100 0.7617916
6200 0.75791216
6300 0.7540946
6400 0.7503374
6500 0.7466399
6600 0.74300086
6700 0.73941886
6800 0.7358932
6900 0.7324225
7000 0.7290057
7100 0.72564185
7200 0.7223299
7300 0.71906877
7400 0.7158574
7500 0.7126951
7600 0.7095808
7700 0.7065133
7800 0.7034919
7900 0.7005154
8000 0.6975831
8100 0.6946942
8200 0.6918476
8300 0.68904275
8400 0.6862786
8500 0.6835542
8600 0.6808691
8700 0.6782223
8800 0.6756128
8900 0.67304015
9000 0.6705035
9100 0.66800225
9200 0.66553557
9300 0.66310287
9400 0.6607032
9500 0.6583363
9600 0.65600127
9700 0.6536974
9800 0.65142417
9900 0.6491809

Let's see the accuracy using test data

In [87]:
answer = sess.run(hypothesis,feed_dict={X:test_x})
print(answer)
[[0.1103949  0.3494037  0.5402015 ]
 [0.3758098  0.49801904 0.12617113]
 [0.633759   0.3516427  0.01459827]]

Let's using the arg_max which express max value as 1

In [88]:
t_predict = sess.run(tf.arg_max(answer,1))
t_answer = sess.run(tf.arg_max(test_y,1))
print(t_predict)
print(t_answer)
[2 1 0]
[2 1 0]

Let's calculate the accuracy

In [89]:
accuracy=tf.reduce_mean(tf.cast(tf.equal(t_predict,t_answer),dtype=tf.float32))
print(sess.run(accuracy*100),"%")
100.0 %

Let`s make Multinomial Classification
sixth. Exam.
Let's solve the problem and if wrong, 
please watch this video again from the beginnig untill get 100 points.





First. What is the Multinomial Classification 
Multinomial classification is the problem of classifying instances into one of three or more classes.




Second.  The softmax function converts the probabilities to scores 
The softmax function converts the scores to probabilities

Third. What is called the largest value of probability as 1 and the rest as 0
one hot encoding


Fourth, Multinomial Classification used to sigmoid hypothesis function
Multinomial Classificaiton used to softmax hypothesis function.







No comments:

Post a Comment