I would like to create in Tensorflow, a function that for every line of a given data X, is applying the softmax function only for some sampled classes, lets say 2, out of K total classes, and returns a matrix S, where S.shape = (N,K)
(N: the lines of the given Data and K the total classes).
The matrix S finally would contain zeros, and non_zero values in the indexes defined for every line by the sampled classes.
In simple python I use advanced indexing, but in Tensorflow I cannot figure out how to make it. My initial question was this, where I present the numpy code.
So I tried to find a solution in Tensorflow and the main idea was not to use the S as a 2-d matrix but as an 1-d array. The code looks like that:
num_samps = 2
S = tf.Variable(tf.zeros(shape=(N*K)))
W = tf.Variable(tf.random_uniform((K,D)))
tfx = tf.placeholder(tf.float32,shape=(None,D))
sampled_ind = tf.random_uniform(dtype=tf.int32, minval=0, maxval=K-1, shape=[num_samps])
ar_to_sof = tf.matmul(tfx,tf.gather(W,sampled_ind),transpose_b=True)
updates = tf.reshape(tf.nn.softmax(ar_to_sof),shape=(num_samps,))
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for line in range(N):
inds_new = sampled_ind + line*K
sess.run(tf.scatter_update(S,inds_new,updates), feed_dict={tfx: X[line:line+1]})
S = tf.reshape(S,shape=(N,K))
This is working, and the result is the expected. But it is running extremely slow. Why is that happening? How could I make that work faster?