对于给定的某个稀疏矩阵按行进行标准化softmax标准化操作
建立会话
A_values = tf.placeholder(tf.float32, shape=(None), name='A_values')
标准化函数
def sparse_softmax(adj, sess, temperature=0.1):
adj = adj.tocoo()
indices = np.mat([adj.row, adj.col]).transpose()
A_out = tf.sparse.softmax(tf.SparseTensor(indices, A_values / temperature, dense_shape=adj.shape))
spare_mat = sess.run(A_out, feed_dict={A_values: adj.data})
indices = spare_mat.indices
new_values = spare_mat.values
rows = indices[:, 0]
cols = indices[:, 1]
new_spare_mat = csr_matrix((new_values, (rows, cols)), shape=adj.shape)
return new_spare_mat.tocsr()
执行步骤
with tf.Session as sess:
new_mat=sparse_softmax(adj,sess=sess,temperature=1.)