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]