pytorch
文章平均质量分 52
解析一些所遇到的pytorch问题
BierOne
SDU->CityU; I am now a Ph.D. candidate at HK CityU.
展开
-
Pytorch-RNN关于pack_padded_sequence之enforce_sorted详解
先说需求:input为一个batch,这个batch里由N个segment组成,这个segment的序列十分重要,不能打乱。因此,我不能把这个batch按照segment的长度,来从大到小进行排序但是我需要用LSTM来处理这个batch里面的每个segment而且我还需要保证,LSTM处理之后,这个batch还是保持原来的segment序列解决方法:调用pack_padded_sequence来处理input,并且传入参数enforce_sorted=False给出代码(一层的LSTM只需要原创 2021-04-25 17:54:19 · 1738 阅读 · 2 评论 -
Pytorch-RuntimeError: CUDA unknown error - this may be due to an incorrectly set up environment
刚装好GPU版本的Pytorch报了这个错,仔细核对了一下Pytorch的版本和我的cuda版本,是一致的。nvidia-smi运行没有问题错误如下:RuntimeError: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the availabl原创 2021-04-25 17:07:26 · 5485 阅读 · 3 评论 -
nn.utils.rnn.pack_padded_sequence: RuntimeError: ‘lengths‘ argument should be a 1D CPU int64 tensor
解决方法:对tensor调用.cpu()即可packed = rnn.pack_padded_sequence(x, x_len.cpu(), batch_first=True)原因:原创 2021-04-25 17:00:32 · 727 阅读 · 0 评论 -
Pytorch--检查是否正常运行(Code)
Code (refer to this link)import torchflag = torch.cuda.is_available() # check if the gpu is avaliableprint(flag)ngpu= 1# Decide which device we want to run ondevice = torch.device(“cuda:0” if (torch.cuda.is_available() and ngpu > 0) else “cpu”)p原创 2021-03-05 22:46:54 · 647 阅读 · 0 评论 -
Pytorch--判断model是否在训练模式下
module的内部包含一个training的属性(bool类型),调用training即可。if net.training xxxelse: yyy多说一句:一般我们调用net.eval() 或者net.train()都会对这个training属性进行改写。原创 2020-12-07 19:20:33 · 4634 阅读 · 0 评论 -
pytorch--BoolTensor进行逻辑运算操作
since pytorch 1.2, there is a dtype torch.bool and associated BoolTensor. The bitwise operators aka numpy (and &, or |, xor ^ and not ~) all work on these as logical operations.BoolTensor在pytor...原创 2020-04-29 22:10:23 · 20176 阅读 · 1 评论 -
pytorch--切断梯度的方法
文章目录方法验证data属性 与 detach()函数clone()函数验证参考资料方法调用tensor的data属性调用tensor的detach()函数调用clone()可行吗?不可行验证先写结论:.data 和.detach只取出本体tensor数据,舍弃了grad,grad_fn等额外反向图计算过程需保存的额外信息。但是.data所创建的tensor与原tensor的内存是...原创 2020-03-23 17:07:10 · 3697 阅读 · 0 评论 -
Spearman’s correlation coefficient--斯皮尔曼相关系数pytorch与numpy实现
文章目录Spearman’s correlation介绍Pytorch实现Numpy实现Spearman’s correlation介绍斯皮尔曼等级相关(Spearman’s correlation coefficient for ranked data)主要用于解决名称数据和顺序数据相关的问题。适用于两列变量,而且具有等级变量性质具有线性关系的资料。由英国心理学家、统计学家斯皮尔曼根据积差...原创 2020-03-23 14:18:42 · 4032 阅读 · 0 评论 -
js-divergence的pytorch实现
文章目录参考文档JSD实现代码一些注意事项参考文档pytorch 中的 kl_div 函数关于logsoftmax与softmax的区别KL散度、JS散度、Wasserstein距离–原理JSD实现代码若有纰漏,敬请指出,感谢!def js_div(p_logits, q_logits, get_softmax=True): """ Function that m...原创 2020-03-20 20:47:54 · 7496 阅读 · 8 评论 -
Pyorch-CUDA error: device-side assert triggered, THCTensorScatterGather, Assertion indexValue failed
文章目录ScatterGather的index溢出报错?个人分析与解决方法ScatterGather的index溢出报错?用pytorch在训练模型的时候,遇到了奇怪的报错。ScatterGather,一看似乎出错就在scatter或者gather的操作上。也就是index溢出的问题,看描述:indexValue >= 0 && indexValue < tens...原创 2020-03-03 22:00:41 · 1560 阅读 · 2 评论 -
pytorch-全面讲解函数topk, scatter, gather
这三个函数在pytorch中关于矩阵操作的非常实用的函数。我认为要想熟练的使用pytorch,能够灵活的使用这三个函数是至关重要的文章目录三者的相同点:维度->数据的映射方式topkgatherscatter参考链接三者的相同点:维度->数据的映射方式因为三者都存在相似的地方,所以我这里放在一起来讲。这个共同点就是index -> value的方式:这里以官方给的gathe...原创 2020-03-01 19:47:07 · 889 阅读 · 0 评论 -
pytorch-由python版本切换引起weight_norm计算不一致?
最近在修改一份code,需要进行如下迁移:python2.7 -> python3pytorch v0.3.1 -> pytorch v1.0.1但是performance相对出现了显著下滑。原创 2020-02-26 16:16:18 · 4164 阅读 · 0 评论 -
pytorch-1.0.1-RNN相关
这篇RNN文章一直准备写,今天终于动笔了。本文章不涉及RNN的原理,也就是运算公式等。所以阅读此文,最好已经对RNN有了较好的认识,这里只谈及pytorch的实现和使用目录RNN(GRU)模块定义pack与pad sequence操作输入与输出参考链接RNN(GRU)模块定义def __init__(self, mode, input_size, hidden_size, ...原创 2020-02-25 19:19:52 · 368 阅读 · 0 评论 -
pytorch-rnn.flatten_parameters作用
最近翻阅pytorch的代码,常常发现这样一句:rnn.flatten_parameters,但却不知道作用,遂作此文rnn.flatten_parameters作用Resets parameter data pointer so that they can use faster code paths翻译一下,就是重置参数的数据指针。其实就是contiguous(连续性)的问题,在pyt...原创 2020-02-25 16:27:00 · 12037 阅读 · 3 评论 -
pytorch-dropout相关
今天在修改代码时,发现对dropout的inplace操作不太明白,所以有了此文,介绍一下pytorch的dropout函数Dropout layers在pytorch的doc中nn.dropout类如下,与functional.dropout函数一致:CLASS: torch.nn.Dropout(p=0.5, inplace=False)Input: Any. Input can be...原创 2020-02-23 17:12:20 · 3223 阅读 · 2 评论 -
pytorch-1.0.1-embedding相关问题
目录Embedding源码解析精度导致cudnn error: cudnn_status_bad_paramEmbedding源码解析这部分代码相对比较简单,先附上官网的doc链接,这里重点介绍我们常用到的一些东西# 初始化函数 def __init__(self, num_embeddings, embedding_dim, padding_idx=None, ...原创 2020-02-22 14:23:24 · 1260 阅读 · 0 评论