在张量流,高级索引中更新矩阵变量的值 [英] Update values of a matrix variable in tensorflow, advanced indexing
问题描述
我想创建一个函数,对于给定数据X的每一行,仅将softmax函数应用于K个总计类中的某些采样类,比方说2个.在简单的python中,代码看起来像这样:
I would like to create 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. In simple python the code seems like 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)
for line in range(N):
for 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最终将在数组"samped_ind"为每一行定义的索引中包含零和非零值. 我想使用Tensorflow来实现这一点.问题在于它包含高级"索引,我找不到使用此库创建索引的方法.
S finally would contain zeros, and non_zero values in the indexes defined for every line by the array "samped_ind". I would like to implement this using Tensorflow. The problem is that it contains "advanced" indexing and i cannot find a way using this library to create that.
我正在尝试使用以下代码:
I am trying that using this 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)
...
for 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})
一切工作到这里为止,但是我找不到用python代码"S [line] [sampled_ind] = ar_to_sof"在矩阵S中进行更新的方法.
Everything works until here, but i cannot find a way to do the update that i want in the matrix S, in python code "S[line][sampled_ind] = ar_to_sof ".
我该如何做?
推荐答案
An answer to my problem was found in the comment of a solution of this problem. Suggests to reshape to 1d vector my matrix S. In that way, the code is working and it 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)
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))
那将返回我期望的结果.现在的问题是此实现速度太慢.比numpy版本慢得多.也许是for循环.有什么建议?
That returns the result that i was expecting. The problem now is that this implementation is too slow. Much slower than the numpy version. Maybe is the for loop. Any suggestions?
这篇关于在张量流,高级索引中更新矩阵变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!