cnn-text-classification-tf icon indicating copy to clipboard operation
cnn-text-classification-tf copied to clipboard

Multiple output instead of one during multi-class clustering

Open MrMimic opened this issue 7 years ago • 1 comments

Hello all,

I modified Denny's code for TF 1.0 and multiclass classification following this: https://github.com/jiegzhan/multi-class-text-classification-cnn

Everything' s fine, output classification looks good. But instead of printing ONE category (the best fitting my input to classify), I would like to print the "weight" of every category. I mean, eg in positive/negative sentences classification, I want to print the probability for a sentence being positive or being negative.

"""Step 2: compute the predictions"""
	graph = tf.Graph()
	with graph.as_default():
		session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
		sess = tf.Session(config=session_conf)
		with sess.as_default():
			saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
			saver.restore(sess, checkpoint_file)
			input_x = graph.get_operation_by_name("input_x").outputs[0]
			dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
			predictions = graph.get_operation_by_name("output/predictions").outputs[0]
			batches = data_helper.batch_iter(list(x_test), params['batch_size'], 1, shuffle=False)
			all_predictions = []
			for x_test_batch in batches:		
				batch_predictions = sess.run(predictions, {input_x: x_test_batch, dropout_keep_prob: 1.0})
				all_predictions = np.concatenate([all_predictions, batch_predictions])
	if y_test is not None:
		y_test = np.argmax(y_test, axis=1)
		correct_predictions = sum(all_predictions == y_test)
		print("Total number of test examples: {}".format(len(y_test)))
		print("Accuracy: {:g}".format(correct_predictions/float(len(y_test))))
		for j in range(0,len(all_predictions)):
			print(test_examples[j]['consumer_complaint_narrative'])
			print("My prediction is: \t" + labels[int(all_predictions[j])])
			print('Label index: \t\t' + str(int(all_predictions[j])))
		logging.critical('The accuracy is: {}'.format(correct_predictions / float(len(y_test))))

Maybe I missed something, but this is my first project using Tensorflow and I really spent time looking for an answer .. Thanks !

MrMimic avatar Apr 25 '17 14:04 MrMimic

You can take the scores to calculate the probability using softmax function. Please have a look at my implementation to get the probability of each class: eval.py

cahya-wirawan avatar May 29 '17 13:05 cahya-wirawan