• 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

    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]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    1402. 做菜顺序 --力扣 --JAVA
    JavaScript面试刷题指南
    Redis 集合(Set)快速指南 | Navicat
    不要62——数位dp打卡
    230902-部署Gradio到已有FastAPI及服务器中
    动态规划思想
    失物招领|基于Web的校园失物招领系统的设计与实现
    vue、axios
    wireshark分析tcp协议(一)三次握手【理论 + 实操】
    1-JAVA-面向对象程序设计概论-笔记整理
  • 原文地址:https://blog.csdn.net/weixin_40994552/article/details/134314411