- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据集,其中类值从 -2 到 2 步 (i.e., -2,-1,0,1,2)
其中 9 标识未标记的数据。
使用一种热编码
self._one_hot_encode(labels)
我收到以下错误:
RuntimeError: index 1 is out of bounds for dimension 1 with size 1
由于
self.one_hot_labels = self.one_hot_labels.scatter(1, labels.unsqueeze(1), 1)
错误应该从
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1]
引发,其中我在映射设置中的 9 等于索引 9 到 1。我不清楚如何修复它,即使经过过去的问题和类似问题的答案(例如,
index 1 is out of bounds for dimension 0 with size 1 )。
def _one_hot_encode(self, labels):
# Get the number of classes
classes = torch.unique(labels)
classes = classes[classes != 9] # unlabelled
self.n_classes = classes.size(0)
# One-hot encode labeled data instances and zero rows corresponding to unlabeled instances
unlabeled_mask = (labels == 9)
labels = labels.clone() # defensive copying
labels[unlabeled_mask] = 0
self.one_hot_labels = torch.zeros((self.n_nodes, self.n_classes), dtype=torch.float)
self.one_hot_labels = self.one_hot_labels.scatter(1, labels.unsqueeze(1), 1)
self.one_hot_labels[unlabeled_mask, 0] = 0
self.labeled_mask = ~unlabeled_mask
def fit(self, labels, max_iter, tol):
self._one_hot_encode(labels)
self.predictions = self.one_hot_labels.clone()
prev_predictions = torch.zeros((self.n_nodes, self.n_classes), dtype=torch.float)
for i in range(max_iter):
# Stop iterations if the system is considered at a steady state
variation = torch.abs(self.predictions - prev_predictions).sum().item()
prev_predictions = self.predictions
self._propagate()
数据集示例:
ID Target Weight Label Score Scale_Cat Scale_num
0 A D 65.1 1 87 Up 1
1 A X 35.8 1 87 Up 1
2 B C 34.7 1 37.5 Down -2
3 B P 33.4 1 37.5 Down -2
4 C B 33.1 1 37.5 Down -2
5 S X 21.4 0 12.5 NA 9
我用作引用的源代码在这里:
https://mybinder.org/v2/gh/thibaudmartinez/label-propagation/master?filepath=notebook.ipynb
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-126-792a234f63dd> in <module>
4 label_propagation = LabelPropagation(adj_matrix_t)
----> 6 label_propagation.fit(labels_t) # causing error
7 label_propagation_output_labels = label_propagation.predict_classes()
8
<ipython-input-115-54a7dbc30bd1> in fit(self, labels, max_iter, tol)
100
101 def fit(self, labels, max_iter=1000, tol=1e-3):
--> 102 super().fit(labels, max_iter, tol)
103
104 ## Label spreading
<ipython-input-115-54a7dbc30bd1> in fit(self, labels, max_iter, tol)
58 Convergence tolerance: threshold to consider the system at steady state.
59 """
---> 60 self._one_hot_encode(labels)
61
62 self.predictions = self.one_hot_labels.clone()
<ipython-input-115-54a7dbc30bd1> in _one_hot_encode(self, labels)
42 labels[unlabeled_mask] = 0
43 self.one_hot_labels = torch.zeros((self.n_nodes, self.n_classes), dtype=torch.float)
---> 44 self.one_hot_labels = self.one_hot_labels.scatter(1, labels.unsqueeze(1), 1)
45 self.one_hot_labels[unlabeled_mask, 0] = 0
46
RuntimeError: index 1 is out of bounds for dimension 1 with size 1
最佳答案
我浏览了您的笔记本(我认为您将 9 更改为 -1 以便运行)并看到了这部分代码:
# Learn with Label Propagation
label_propagation = LabelPropagation(adj_matrix_t)
print("Label Propagation: ", end="")
label_propagation.fit(labels_t)
label_propagation_output_labels = label_propagation.predict_classes()
最终调用:
self.one_hot_labels = self.one_hot_labels.scatter(1, labels.unsqueeze(1), 1)
是哪里出错了。
index = torch.tensor([[5],[0],[3],[5],[1],[4]]); print(index.shape); print(index)
result = torch.zeros(6, 6, dtype=src.dtype).scatter_(1, index, src); print(result.shape); print(result)
这会给我们
torch.Size([6, 1])
tensor([[5],
[0],
[3],
[5],
[1],
[4]])
torch.Size([6, 6])
tensor([[0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
索引矩阵是 6 个观测值和 1 个观测值(类别)
self[i][index[i][j][k]][k] = src[i][j][k]
因此,在您的情况下,您试图将 1 的值应用到 self[40,1] 中 index[0] 列(等于 1)的一行中。给你问题中的错误。虽然我检查了你的笔记本,错误是
classes = list([-2,-1,0,1,2,9])
labels = list()
for i in range(0,40):
labels.append(list([(lambda x: x+2 if x !=9 else 5)(random.sample(classes,1)[0])]))
index_aka_labels = torch.tensor(labels)
print(index_aka_labels)
print(index_aka_labels.shape)
torch.zeros(40, 6, dtype=src.dtype).scatter_(1, index_aka_labels, 1)
最后,我们实现了我们想要的OHE结果:
tensor([[0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0],
... (40 observations)
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
关于python - 一种热编码期间的 RunTimeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68045496/
运行 server.py 文件时出现错误 File "C:\Users\nawin\AppData\Local\Programs\Python\Python38\lib\site-packages\s
我在我的 PC 上运行 cifar10 网络,在完成训练和运行评估脚本后出现以下错误: 2016-06-01 14:37:14.238317: precision @ 1 = 0.000 Traceb
我已经编写了一段代码来训练Guassian过程回归模型来预测年龄。我已经编写了以下代码,并且运行良好:。但我注意到,每个纪元都输入了相同的数据,我认为这可能会导致过度拟合,所以我想使用Mini Bat
我有一个数据集,其中类值从 -2 到 2 步 (i.e., -2,-1,0,1,2)其中 9 标识未标记的数据。 使用一种热编码 self._one_hot_encode(labels) 我收到以下错
我是 Phoenix Framework 的新用户,我正在尝试设置一个简单的 HTTP POST 服务,该服务对传入数据执行计算并返回结果,但出现以下错误: ** (RuntimeError) exp
为什么这段代码运行良好并且不抛出异常? def myzip(*args): iters = [iter(arg) for arg in args] try: while
今天,当我开始编写我的网页时,它工作正常,但突然我的 css 文件无法工作。我的更改没有更新。读了一点之后,我读到我应该清理我的缓存。我这样做了,之后当我运行我的网页并单击任何按钮时,我会在我的控制台
我使用描述符编写了一个 Circle 类,允许用户设置圆的 x、y 和 r 的值,并检查 x 和 y 的值是否为整数。如果用户输入非整数,则会引发 TypeError,现在我想制作另一个描述符类,允许
代码: # callee.py import signal import sys import time def int_handler(*args): for i in range(10):
将 RuntimeError 异常用于一般应用程序是否可以接受? raise RuntimeError('config file is missing host address') 我有一些代码会遇到
Pytorch测试神经网络时出现“RuntimeError: Error(s) in loading state_dict for Net” 解决方法: ?
得到标题中提到的错误。下面提到的函数由通过POST api调用的另一个函数调用。 打印语句下方的行上有错误。不知道该错误意味着什么,为什么会出现。一周前使用的相同代码。 def remove_indi
我正在尝试计算 PyTorch 中变量的梯度。然而,有一个运行时错误告诉我输出和梯度的形状必须相同。然而,就我而言,输出和梯度的形状不能相同。这是我要重现的代码: import numpy as np
我正在尝试在 ExpandableListView 中查看数据库中的数据(我首先尝试让它使用硬编码字符串)。 我使用了以下示例:CodeWiki ExpandableListView 但是当我点击一个
import asyncio import json import websockets from mongodb import * class WebSocketRe
我正在尝试同时使用多处理和 matplotlib。 我正在创建一个标准的 Pool,添加与 apply_async 的工作,并使用 apply_async 的回调函数更新 GUI,它运行于Pool 的
我正在阅读 Python 3 的文档 here : If a generator code directly or indirectly raises StopIteration, it is con
我无法使用 BeautifulSoup 避免最大递归深度 Python RuntimeError。 我正在尝试递归嵌套的代码部分并提取内容。美化后的 HTML 看起来像这样(不要问为什么它看起来像这样
我知道已经有人问过这个问题,但我仍然无法找到解决方案。 我想在自定义数据集上使用 gensim 的 word2vec,但现在我仍在弄清楚数据集必须采用什么格式。我看了this post其中输入基本上是
我在 Colab Pro+(使用高 RAM 选项)上运行神经网络时发现了这个问题。 运行时错误:CUDA 内存不足。尝试分配 8.00 GiB(GPU 0;15.90 GiB 总容量;12.04 Gi
我是一名优秀的程序员,十分优秀!