python - Update values of a matrix variable in tensorflow, advanced indexing -
i create function every line of given data x, applying softmax function sampled classes, lets 2, out of k total classes. in simple python code seems that:
def softy(x,w, num_samples): n = x.shape[0] k = w.shape[0] s = np.zeros((n,k)) ar_to_sof = np.zeros(num_samples) sampled_ind = np.zeros(num_samples, dtype = int) line in range(n): samp in range(num_samples): sampled_ind[samp] = randint(0,k-1) ar_to_sof[samp] = np.dot(x[line],np.transpose(w[sampled_ind[samp]])) ar_to_sof = softmax(ar_to_sof) s[line][sampled_ind] = ar_to_sof return s
s contain zeros, , non_zero values in indexes defined every line array "samped_ind". implement using tensorflow. problem contains "advanced" indexing , cannot find way using library create that.
i trying using code:
s = tf.variable(tf.zeros((n,k))) tfx = tf.placeholder(tf.float32,shape=(none,d)) wsampled = tf.placeholder(tf.float32, shape = (none,d)) ar_to_sof = tf.matmul(tfx,wsampled,transpose_b=true) softy = tf.nn.softmax(ar_to_sof) r = tf.random_uniform(shape=(), minval=0,maxval=k, dtype=tf.int32) ... line in range(n): sampled_ind = tf.constant(value=[sess.run(r),sess.run(r)],dtype= tf.int32) wsampled = sess.run(tf.gather(w,sampled_ind)) sess.run(softy,feed_dict={tfx:x[line:line+1], wsampled:wsampled})
everything works until here, cannot find way update want in matrix s, in python code "s[line][sampled_ind] = ar_to_sof ".
how make work?
an answer problem found in comment of solution of this problem. suggests reshape 1d vector matrix s. in way, code working , looks like:
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) 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))
that returns result expecting. problem implementation slow. slower numpy version. maybe loop. suggestions?
Comments
Post a Comment