我想创建一个函数,对于给定数据X的每一行,仅对某些采样类应用softmax函数,比如说,总共K个类中的2。在简单的python中,代码似乎是这样的:
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
最后将包含零和由数组“samped_ind”为每一行定义的索引中的非零值。 我想使用Tensorflow实现这一点。问题是它包含“高级”索引,我无法找到使用此库创建它的方法。
我正在尝试使用此代码:
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})
一切正常,直到这里,但我找不到在矩阵S中进行更新的方法,在python代码“S [line] [sampled_ind] = ar_to_sof”。
我怎么能做这个工作?
答案 0 :(得分:0)
this problem解决方案的评论中找到了我的问题的答案。建议重塑为1d向量我的矩阵S.这样,代码工作,它看起来像:
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循环。有什么建议?