Softmax classification


In [1]:
import tensorflow as tf
import numpy as np

In [2]:
xy = np.loadtxt('../data/softmax_data.txt',unpack=True, dtype='float32')
x_data = np.transpose(xy[0:3])
y_data = np.transpose(xy[3:])

Data


In [3]:
np.transpose(xy)


Out[3]:
array([[ 1.,  2.,  1.,  0.,  0.,  1.],
       [ 1.,  3.,  2.,  0.,  0.,  1.],
       [ 1.,  3.,  4.,  0.,  0.,  1.],
       [ 1.,  5.,  5.,  0.,  1.,  0.],
       [ 1.,  7.,  5.,  0.,  1.,  0.],
       [ 1.,  2.,  5.,  0.,  1.,  0.],
       [ 1.,  6.,  6.,  1.,  0.,  0.],
       [ 1.,  7.,  7.,  1.,  0.,  0.]], dtype=float32)

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

W = tf.Variable(tf.zeros([3,3]))
h = tf.nn.softmax(tf.matmul(X,W))
learning_rate = 0.001

cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(h),reduction_indices=1))
opt = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)
for step in range(2000):
    sess.run(opt, feed_dict={X:x_data, Y:y_data})
    if step % 200 == 0:
        print(step, sess.run(cost,feed_dict={X:x_data,Y:y_data}),sess.run(W))


0 1.09774 [[ -8.33333252e-05   4.16666626e-05   4.16666480e-05]
 [  1.66666694e-04   2.91666773e-04  -4.58333408e-04]
 [  1.66666636e-04   4.16666706e-04  -5.83333429e-04]]
200 1.05962 [[-0.02051384 -0.00103983  0.02155367]
 [ 0.01406438  0.01097753 -0.02504191]
 [ 0.01431208  0.03574873 -0.05006079]]
400 1.04985 [[-0.04282598 -0.00625899  0.04908497]
 [ 0.01747187  0.00156368 -0.01903554]
 [ 0.01831204  0.04954104 -0.06785304]]
600 1.0407 [[-0.06517859 -0.01176361  0.07694222]
 [ 0.01943521 -0.00848972 -0.01094548]
 [ 0.0211558   0.06118308 -0.0823388 ]]
800 1.03194 [[-0.08734013 -0.01729389  0.10463405]
 [ 0.0211172  -0.01796171 -0.00315548]
 [ 0.02396628  0.07198346 -0.09594974]]
1000 1.02354 [[-0.10928574 -0.02282181  0.13210757]
 [ 0.02266254 -0.02678034  0.00411783]
 [ 0.02685853  0.08210357 -0.10896212]]
1200 1.01547 [[-0.13101467 -0.02834092  0.15935561]
 [ 0.02409703 -0.03497621  0.01087923]
 [ 0.029832    0.091598   -0.12143001]]
1400 1.0077 [[-0.15252906 -0.03384715  0.18637618]
 [ 0.02543302 -0.04258838  0.01715542]
 [ 0.03287466  0.10050805 -0.13338271]]
1600 1.00021 [[-0.17383142 -0.03933692  0.21316831]
 [ 0.02668081 -0.04965464  0.0229739 ]
 [ 0.03597427  0.10887136 -0.14484565]]
1800 0.992979 [[-0.19492456 -0.04480688  0.23973146]
 [ 0.02784993 -0.05621058  0.02836074]
 [ 0.03911954  0.11672314 -0.15584265]]

Test & one-hot encoding


In [5]:
a = sess.run(h, feed_dict={X:[[1,11,7]]})
print(a,sess.run(tf.arg_max(a,1)))

b = sess.run(h, feed_dict={X:[[1,3,4]]})
print(b,sess.run(tf.arg_max(b,1)))

c = sess.run(h, feed_dict={X:[[1,1,0]]})
print(c,sess.run(tf.arg_max(c,1)))

all = sess.run(h, feed_dict={X:[[1,1,0],[1,3,4],[1,1,0]]})
print(all,sess.run(tf.arg_max(all,1)))


[[ 0.46269614  0.35486573  0.18243811]] [0]
[[ 0.33821005  0.42100322  0.24078675]] [1]
[[ 0.27005655  0.29087916  0.43906435]] [2]
[[ 0.27005655  0.29087916  0.43906435]
 [ 0.33821002  0.42100322  0.24078675]
 [ 0.27005655  0.29087916  0.43906435]] [2 1 2]