deeplog中输出某个 event 的概率

1 实现之后效果

# import DeepLog and Preprocessor
import numpy as np
from deeplog import DeepLog
import torch

# Create DeepLog object
deeplog = DeepLog(
    input_size  = 10, # Number of different events to expect
    hidden_size = 64 , # Hidden dimension, we suggest 64
    output_size = 10, # Number of different events to expect
)

# X数据维度 30×10
X = torch.randint(1,8, size=(30, 10))
# 标签
Y = np.random.randint(1,8, size=30)
# 输出每个标签的概率
result = deeplog.predict_prob(
    X = X,
    y = Y)

print(result.shape)
print(result)

输出结果:
在这里插入图片描述

2 实现步骤

step1 找到安装包位置,并打开文件
在这里插入图片描述

step2 DeepLog 类中添加如下函数

class DeepLog(Module):
	......
	......
	......
	def predict_prob(self, X, y, k=1, variable=False, verbose=True):
	"""Predict the k most likely output values
	
	   Parameters
	   ----------
	   X : torch.Tensor of shape=(n_samples, seq_len)
	       Input of sequences, these will be one-hot encoded to an array of
	       shape=(n_samples, seq_len, input_size)
	
	   y : Ignored
	       Ignored
	
	   k : int, default=1
	       Number of output items to generate
	
	   variable : boolean, default=False
	       If True, predict inputs of different sequence lengths
	
	   verbose : boolean, default=True
	       If True, print output
	
	   Returns
	   -------
	   result : torch.Tensor of shape=(n_samples, k)
	       k most likely outputs
	
	   confidence : torch.Tensor of shape=(n_samples, k)
	       Confidence levels for each output
	   """
	# Get the predictions
	result = super().predict(X, variable=variable, verbose=verbose)
	# Get the probabilities from the log probabilities
	result = result.exp()
	# return a given key's prob
	index_c = y
	index_r = torch.arange(y.shape[0])
	return result[index_r, index_c]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值