Chris Joo
Chris Joo

Reputation: 639

How to convert numpy ndarray values to probability values

The following results are numpy ndarray scores from linear regression[y = Wx + b].

scores = tf.nn.xw_plus_b(self.h_drop, W, b, name='scores')
~ ~ .....
all_scores = np.zeros(shape=(0,len(label_dict))) 
~~...    
all_scores = np.concatenate((all_scores, batch_scores) , axis=0) 

numpy ndarray numpy ndarray description How do I change above numpy ndarray values to ndarray probability values ?

Desired results:

      col0    col1      col2     col3   col4    col5 

Row1 0.02  |   0.123 | 0.678 | 0.067 | 0.0987 | 0.1089   : Sum(col0~5) = 1 
~ 

Upvotes: 1

Views: 1310

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

The simplest way is to apply to softmax function:

f(x) = e^x_i / sum_j e^x_j

This will transform values in any range to a vector that sums to one, which can be interpreted as probabilities. A TF function that does this is tf.nn.softmax.

Upvotes: 2

Related Questions