python对稀疏矩阵进行带温度的softmax方法

对于给定的某个稀疏矩阵按行进行标准化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.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值