tensorflow - How do you trim the size of a one-hot label and still keep it valid? -


given batch of one-hot labels of length 10:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]   [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]  

...

i can trim 1 using tf.slice():  [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.]  [ 0.  0.  0.  0.  0.  0.  0.  0.  1.] 

but noticed first line no longer valid one-hot label (one of column has set 1). how make valid this:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  1.]   [ 0.  0.  0.  0.  0.  0.  0.  0.  1.] 

whereby if none of columns set 1, last place column in one-hot label set 1.

thank you.

edit: guess should clarify make more concrete. let's i'm using one-hot label mnist. , decided instead of 10 digits, i'm using 9 digits , want label 0-8 instead of 0-9. , want 9-label converted 6-label. so, want reduce shape of one-hot 10 9. , fix labels corresponding change.

for example: if original encoding is: (5, 0, 9) should change (5, 0, 6). [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] becomes [0. 0. 0. 0. 0. 0. 1. 0. 0.]

one way is:

# trim one_hot in tensor trimmed exists = tf.reduce_sum(trimmed, axis=1) zeros = tf.zeros_like(trimmed[:,:-1]) ones = tf.ones_like(trimmed[:,0:1]) zeros_ones = tf.concat((zeros,ones), axis=1) final = tf.where(exists>1, trimmed, zeros_ones) 

for example:

trimmed = [[0,0,1], [0,0,0], [1,0,0]] exists = [1,0,1] zeros_ones = [[0,0,1], [0,0,1], [0,0,1]] final = [[0,0,1],[0,0,1],[1,0,0]] 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -