Reputation: 974
I implement classic image classification problem with tensorflow, I have 9 classes, first I use softmax_cross_entropy_with_logits
as classifier and train network, after some steps it gives to about 99% train accuracy,
Then test the same problem with sparse_softmax_cross_entropy_with_logits
this time it doesn't converge at all,(train accuracy is around 0.10 and 0.20)
Only for your information, for softmax_cross_entropy_with_logits
, I use [batch_size, num_classes] with dtype float32 for labels, and for sparse_softmax_cross_entropy_with_logits
I use [batch_size] with dtype int32 for labels.
Does anybody have any idea?
Update:
this is code:
def costFun(self):
self.y_ = tf.reshape(self.y_, [-1])
return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(self.score_, self.y_))
def updateFun(self):
return tf.train.AdamOptimizer(learning_rate = self.lr_).minimize(self.cost_)
def perfFun(self):
correct_pred = tf.equal(tf.argmax(self.score_,1), tf.argmax(y,1))
return(tf.reduce_mean(tf.cast(correct_pred, tf.float32)))
def __init__(self,x,y,lr,lyr1FilterNo,lyr2FilterNo,lyr3FilterNo,fcHidLyrSize,inLyrSize,outLyrSize, keepProb):
self.x_ = x
self.y_ = y
self.lr_ = lr
self.inLyrSize = inLyrSize
self.outLyrSize_ = outLyrSize
self.lyr1FilterNo_ = lyr1FilterNo
self.lyr2FilterNo_ = lyr2FilterNo
self.lyr3FilterNo_ = lyr3FilterNo
self.fcHidLyrSize_ = fcHidLyrSize
self.keepProb_ = keepProb
[self.params_w_, self.params_b_] = ConvNet.paramsFun(self)
self.score_, self.PackShow_ = ConvNet.scoreFun (self)
self.cost_ = ConvNet.costFun (self)
self.update_ = ConvNet.updateFun(self)
self.perf_ = ConvNet.perfFun (self)
main:
lyr1FilterNo = 32
lyr2FilterNo = 64
lyr3FilterNo = 128
fcHidLyrSize = 1024
inLyrSize = 32 * 32
outLyrSize = 9
lr = 0.001
batch_size = 300
dropout = 0.5
x = tf.placeholder(tf.float32, [None, inLyrSize ])
y = tf.placeholder(tf.int32, None )
ConvNet_class = ConvNet(x,y,lr,lyr1FilterNo,lyr2FilterNo,lyr3FilterNo,fcHidLyrSize,inLyrSize,outLyrSize, keepProb)
initVar = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(initVar)
for step in range(10000):
trData_i = np.reshape( trData_i , ( -1, 32 * 32 ) )
trLabel_i = np.reshape( trLabel_i, ( -1, 1 ) )
update_i, PackShow, wLyr1_i, wLyr2_i, wLyr3_i = sess.run([ConvNet_class.update_, ConvNet_class.PackShow_,
ConvNet_class.params_w_['wLyr1'], ConvNet_class.params_w_['wLyr2'], ConvNet_class.params_w_['wLyr3']],
feed_dict = { x:trData_i, y:trLabel_i, keepProb:dropout} )
Upvotes: 1
Views: 946
Reputation: 974
I found the problem, thanks to @mrry for helpful comment, actually I mistake about calculation of accuracy, in fact, "sparse_softmax" and "softmax" has the same loss(or cost) for input logits,
for computation accuracy, I change
correct_pred = tf.equal(tf.argmax(self.score_,1), tf.argmax(y,1))
to
correct_pred = tf.equal(tf.argmax(self.score_,1), y ))
since in "sparse_softmax" the ground truth labels are not in one-hot vector format, but real int32 or int64 numbers.
Upvotes: 3