I have the below data:
prop_tenure prop_12m prop_6m
0.00 0.00 0.00
0.00 0.00 0.00
0.06 0.06 0.10
0.38 0.38 0.25
0.61 0.61 0.66
0.01 0.01 0.02
0.10 0.10 0.12
0.04 0.04 0.04
0.22 0.22 0.22
Run Code Online (Sandbox Code Playgroud)
and I am doing a pairplot as below:
sns.pairplot(data)
plt.show()
Run Code Online (Sandbox Code Playgroud)
However I would like to display the correlation coefficient among the variables and if possible the skewness and kurtosis of each variable. I am not sure how to do that in seaborn. Can someone please …
有人知道为什么这query_set
不会为我返回任何值吗?单独使用过滤器,它工作得很好,所以似乎.filter().filter()
一起过滤“非此即彼”是错误的方法。
ticket_query = request.event.tickets.filter(status='on-sale').filter(status='paused').prefetch_related('ticket_tax')
Run Code Online (Sandbox Code Playgroud) 我正在使用基于 Resnet152 的迁移学习来训练模型。基于 PyTorch 教程,我在保存经过训练的模型并加载它进行推理方面没有问题。但是,加载模型所需的时间很慢。我不知道我做对了没有,这是我的代码:
将训练好的模型保存为状态字典:
torch.save(model.state_dict(), 'model.pkl')
Run Code Online (Sandbox Code Playgroud)
加载它以进行推理:
model = models.resnet152()
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, len(classes))
st = torch.load('model.pkl', map_location='cuda:0' if torch.cuda.is_available() else 'cpu')
model.load_state_dict(st)
model.eval()
Run Code Online (Sandbox Code Playgroud)
我对代码进行了计时,发现第一行model = models.resnet152()
加载时间最长。在 CPU 上,测试一张图像需要 10 秒。所以我的想法是这可能不是加载它的正确方法?
如果我像这样保存整个模型而不是 state.dict:
torch.save(model, 'model_entire.pkl')
Run Code Online (Sandbox Code Playgroud)
并像这样测试它:
model = torch.load('model_entire.pkl')
model.eval()
Run Code Online (Sandbox Code Playgroud)
在同一台机器上,测试一张图像只需 5 秒。
所以我的问题是:这是加载 state_dict 进行推理的正确方法吗?
我正在尝试绘制一个矩阵来比较一些数据。但情节的标题与子情节重叠:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn
def save_graph_cm(CMatrix):
# CMatrix is a dict with four 3x3 pandas DataFrame
k = 'Wine'
id = 0
cm = 1
plt.suptitle("#" + str(id) + " Confusion Matrix for " + k + " dataset")
for c_matrix in CMatrix:
plt.subplot(2, 2, cm)
sn.heatmap(CMatrix[c_matrix], annot=True, cmap="YlOrRd")
plt.title("CV - " + str(cm-1))
plt.xlabel("Predicted Classes")
plt.ylabel("Real Classes")
cm += 1
plt.tight_layout()
plt.show
Run Code Online (Sandbox Code Playgroud)
我现在得到的是:
你如何计算语义分割中的 top k 准确率?在分类中,我们可以将topk准确度计算为:
correct = output.eq(gt.view(1, -1).expand_as(output))
Run Code Online (Sandbox Code Playgroud) python computer-vision image-segmentation pytorch semantic-segmentation
在 networkx 中有一个函数可以使用径向布局(graphviz 的“twopi”)绘制树:
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
pos = graphviz_layout(G, prog='twopi', root=root, args='')
Run Code Online (Sandbox Code Playgroud)
可以使用参数指定根节点root
(它被添加到引擎盖下的 args 中args += f" -Groot={root}"
)。
但是,当图形由多个断开连接的组件组成时,如何指定多个根?即一片树林。
我在不提供根参数的情况下得到以下图:
正如你在视觉上看到的,虽然它正确地为 10 棵树选择了真正的根节点,但它为 12 选择了真正根节点的一个孩子作为中心(因此一些分支看起来比实际更浅,相对于其他分支)。
如何手动指定多棵树的根?
在 PyTorch 中,以下两种方法向 GPU 发送张量(或模型)有什么区别:
设置:
X = np.array([[1, 3, 2, 3], [2, 3, 5, 6], [1, 2, 3, 4]]) # X = model()
X = torch.DoubleTensor(X)
Run Code Online (Sandbox Code Playgroud)
方法一 | 方法二 |
---|---|
X.cuda() |
device = torch.device("cuda:0") X = X.to(device) |
(我真的不需要对后端发生的事情进行详细解释,只想知道它们是否本质上都在做同样的事情)
我正在使用以下代码使用 pytorch 查找 topk 匹配项:
def find_top(self, x, y, n_neighbors, unit_vectors=False, cuda=False):
if not unit_vectors:
x = __to_unit_torch__(x, cuda=cuda)
y = __to_unit_torch__(y, cuda=cuda)
with torch.no_grad():
d = 1. - torch.matmul(x, y.transpose(0, 1))
values, indices = torch.topk(d, n_neighbors, dim=1, largest=False, sorted=True)
return indices.cpu().numpy()
Run Code Online (Sandbox Code Playgroud)
不幸的是,它抛出以下错误:
values, indices = torch.topk(d, n_neighbors, dim=1, largest=False, sorted=True)
RuntimeError: invalid argument 5: k not in range for dimension at /pytorch/aten/src/THC/generic/THCTensorTopK.cu:23
Run Code Online (Sandbox Code Playgroud)
d 的大小是(1793,1)
。我错过了什么?
我有一个计算向量的神经网络u
。我想计算关于 input 的x
一阶和二阶雅可比,单个元素。
有人知道如何在 PyTorch 中做到这一点吗?下面是我项目中的代码片段:
import torch
import torch.nn as nn
class PINN(torch.nn.Module):
def __init__(self, layers:list):
super(PINN, self).__init__()
self.linears = nn.ModuleList([])
for i, dim in enumerate(layers[:-2]):
self.linears.append(nn.Linear(dim, layers[i+1]))
self.linears.append(nn.ReLU())
self.linears.append(nn.Linear(layers[-2], layers[-1]))
def forward(self, x):
for layer in self.linears:
x = layer(x)
return x
Run Code Online (Sandbox Code Playgroud)
然后我实例化我的网络:
n_in = 1
units = 50
q = 500
pinn = PINN([n_in, units, units, units, q+1])
pinn
Run Code Online (Sandbox Code Playgroud)
哪个返回
PINN(
(linears): ModuleList(
(0): Linear(in_features=1, out_features=50, bias=True)
(1): ReLU()
(2): Linear(in_features=50, out_features=50, bias=True) …
Run Code Online (Sandbox Code Playgroud) 我试图理解为什么变压器使用多个注意力头。我找到了以下引用:
Transformer 使用多个注意力头,而不是使用单个注意力函数(其中注意力可以由实际单词本身主导)。
“注意力由单词本身主导”是什么意思?使用多个头如何解决这个问题?
python ×9
pytorch ×5
python-3.x ×2
correlation ×1
django ×1
gpu ×1
gradient ×1
graphviz ×1
matplotlib ×1
networkx ×1
nlp ×1
seaborn ×1
top-n ×1