pytorch one-hot编码

2023-05-16

使用scatter_将标签转换为one-hot

import torch

num_class = 5
label = torch.tensor([0, 2, 1, 4, 1, 3])
one_hot = torch.zeros((len(label), num_class)).scatter_(1, label.long().reshape(-1, 1), 1)
print(one_hot)
"""
tensor([[1., 0., 0., 0., 0.],
        [0., 0., 1., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 1.],
        [0., 1., 0., 0., 0.],
        [0., 0., 0., 1., 0.]])
"""

又或者更简单的。。。

import torch.nn.functional as F
import torch

num_class = 5
label = torch.tensor([0, 2, 1, 4, 1, 3])
one_hot = F.one_hot(label, num_classes=num_class )
print(one_hot)
"""
tensor([[1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 1, 0]])
"""
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pytorch one-hot编码 的相关文章

  • 为什么自动编码器中的解码器在最后一层使用 sigmoid?

    我正在看this https github com L1aoXingyu pytorch beginner blob master 08 AutoEncoder Variational autoencoder py工作变分自动编码器 主要班
  • Pytorch:获取最终层的正确尺寸

    Pytorch 新手来了 我正在尝试微调 VGG16 模型来预测 3 个不同的类别 我的部分工作涉及将 FC 层转换为 CONV 层 但是 我的预测值不会落在 0 到 2 3 个类别 之间 有人可以向我指出有关如何计算最后一层的正确尺寸的好
  • Python中的嵌入层:如何正确使用Torchsummary?

    这是一个最低限度工作 可重现的示例 import torch import torch nn as nn from torchsummary import summary class Network nn Module def init s
  • .data 在 pytorch 中还有用吗?

    我是 pytorch 的新手 我读了很多大量使用张量的 pytorch 代码 data成员 但我搜索 data在官方文档和Google中 发现很少 我猜 data包含张量中的数据 但我不知道什么时候需要它 什么时候不需要 data是一个属性
  • Pytorch 说 CUDA 不可用(在 Ubuntu 上)

    我正在尝试在我拥有的笔记本电脑上运行 Pytorch 这是一个较旧的型号 但它确实有 Nvidia 显卡 我意识到这可能不足以实现真正的机器学习 但我正在尝试这样做 以便我可以了解安装 CUDA 的过程 我已按照上面的步骤操作安装指南 ht
  • 在 Pytorch 中执行优化时如何对变量应用界限?

    我正在尝试使用 Pytorch 进行非凸优化 试图最大化我的目标 因此在 SGD 中最小化 我想限制因变量 x gt 0 并且 x 值的总和小于 1000 我认为我已经以斜坡惩罚的形式正确实施了惩罚 但我正在努力解决 x 变量的边界问题 在
  • PyTorch:如何使用 DataLoaders 自定义数据集

    如何利用torch utils data Dataset and torch utils data DataLoader根据您自己的数据 不仅仅是torchvision datasets 有没有办法使用内置的DataLoaders他们使用的
  • Pytorch:了解 nn.Module 类内部如何工作

    一般来说 一个nn Module可以由子类继承 如下所示 def init weights m if type m nn Linear torch nn init xavier uniform m weight class LinearRe
  • RuntimeError:维度指定为 0 但张量没有维度

    我试图使用 MNIST 数据集实现简单的 NN 但我不断收到此错误 将 matplotlib pyplot 导入为 plt import torch from torchvision import models from torchvisi
  • 我可以使用逻辑索引或索引列表对张量进行切片吗?

    我正在尝试使用列上的逻辑索引对 PyTorch 张量进行切片 我想要与索引向量中的 1 值相对应的列 切片和逻辑索引都是可能的 但是它们可以一起吗 如果是这样 怎么办 我的尝试不断抛出无用的错误 类型错误 使用 ByteTensor 类型的
  • 在pytorch中使用tensorboard,但得到空白页面?

    我在pytorch 1 3 1中使用tensorboard 并且我在张量板的 pytorch 文档 https pytorch org docs stable tensorboard html 运行后tensorboard logdir r
  • 为什么我在这里遇到被零除的错误?

    所以我正在关注这个文档中的教程 https pytorch org tutorials beginner data loading tutorial html在自定义数据集上 我使用的是 MNIST 数据集 而不是教程中的奇特数据集 这是D
  • 为什么 RNN 需要两个偏置向量?

    In Pytorch RNN 实现 http pytorch org docs master nn html highlight rnn torch nn RNN 有两个偏差 b ih and b hh 为什么是这样 它与使用一种偏差有什么
  • 一次热编码期间出现 RunTimeError

    我有一个数据集 其中类值以 1 步从 2 到 2 i e 2 1 0 1 2 其中 9 标识未标记的数据 使用一种热编码 self one hot encode labels 我收到以下错误 RuntimeError index 1 is
  • 使 CUDA 内存不足

    我正在尝试训练网络 但我明白了 我将批量大小设置为 300 并收到此错误 但即使我将其减少到 100 我仍然收到此错误 更令人沮丧的是 在 1200 个图像上运行 10 epoch 大约需要 40 分钟 有什么建议吗 错了 我怎样才能加快这
  • 如何计算 CNN 第一个线性层的维度

    目前 我正在使用 CNN 其中附加了一个完全连接的层 并且我正在使用尺寸为 32x32 的 3 通道图像 我想知道是否有一个一致的公式可以用来计算第一个线性层的输入尺寸和最后一个卷积 最大池层的输入 我希望能够计算第一个线性层的尺寸 仅给出
  • 将 Keras (Tensorflow) 卷积神经网络转换为 PyTorch 卷积网络?

    Keras 和 PyTorch 使用不同的参数进行填充 Keras 需要输入字符串 而 PyTorch 使用数字 有什么区别 如何将一个转换为另一个 哪些代码在任一框架中获得相同的结果 PyTorch 还采用参数 in channels o
  • 如何有效地对一个数组中某个值在另一个数组中的位置出现的次数求和

    我正在寻找一种有效的 for 循环 避免解决方案来解决我遇到的数组相关问题 我想使用一个巨大的一维数组 A gt size 250 000 用于一维索引的 0 到 40 之间的值 以及用于第二维索引的具有 0 到 9995 之间的值的相同大
  • torch.stack() 和 torch.cat() 函数有什么区别?

    OpenAI 的强化学习 REINFORCE 和 actor critic 示例具有以下代码 加强 https github com pytorch examples blob master reinforcement learning r
  • Pytorch“展开”等价于 Tensorflow [重复]

    这个问题在这里已经有答案了 假设我有大小为 50 50 的灰度图像 在本例中批量大小为 2 并且我使用 Pytorch Unfold 函数 如下所示 import numpy as np from torch import nn from

随机推荐

  • 【字符串 递归】正则表达式匹配

    题目描述 请实现一个函数用来匹配包括 和 的正则表达式 模式中的字符 表示任意一个字符 xff0c 而 39 表示它前面的字符可以出现任意次 xff08 包含0次 xff09 在本题中 xff0c 匹配是指字符串的所有字符匹配整个模式 例如
  • 解决Linux下载较慢的问题

    修改源 xff0c 输入命令sudo gedit etc apt sources list xff0c 覆盖源文件中所有内容 deb http mirrors aliyun com ubuntu trusty main restricted
  • Linux配置ssh

    服务器主机安装ssh sudo apt get install openssh server xff0c 客户端使用putty等支持ssh的软件登录即可 xff0c 记住服务器的ip和密码
  • pytorch计算模型参数量

    total span class token operator 61 span span class token builtin sum span span class token punctuation span span class t
  • 【字符串】把字符串转换成整数

    题目描述 将一个字符串转换成一个整数 xff0c 要求不能使用字符串转换整数的库函数 数值为0或者字符串不是一个合法的数值则返回0 输入描述 输入一个字符串 包括数字字母符号 可以为空 返回值描述 如果是合法的数值表达则返回该数字 xff0
  • 【树】二叉树的镜像

    题目描述 操作给定的二叉树 xff0c 将其变换为源二叉树的镜像 思路很简单 xff0c 只需要递归遍历树 xff0c 然后将每个节点的左右子树调换即可 span class token keyword import span java s
  • 【树】树的子结构

    来自剑指offer 这题有点难度 xff0c 解题思想是 xff1a 若B是A的子树 xff0c 则子树的根节点可能为树A中的任意一个节点 xff0c 因此只需要遍历树A的每个节点 xff0c 判断以这个节点为根节点的树是否包含子树B xf
  • Docker 网络

    1 简介 容器网络实质上是由 Docker 为应用程序所创造的虚拟环境的一部分 xff0c 它能让应用从宿主机操作系统的网络环境中独立出来 xff0c 形成容器自有的网络设备 IP 协议栈 端口套接字 IP 路由表 防火墙等与网络相关的模块
  • Ubuntu 20安装Nvidia驱动 + cuda10.1 + Anaconda + pytorch 1.5

    安装Nvidia驱动 输入命令 ubuntu drivers devices查看显卡推荐的驱动选择recommend的版本进行安装 xff0c 例如我的是460 sudo apt install nvidia driver 460 安装完成
  • VScode ssh远程服务器解决试图写入的管道不存在

    解决方案 xff0c 在C盘用户目录下找到 ssh文件 xff0c 删除known hosts文件或打开known hosts删除对应的ip
  • S3DIS场景点云数据集

    S3DIS是常用的室内场景分割数据集 xff0c 包含6个Area xff0c 常用的数据格式如下 xff1a Stanford3dDataset v1 2 Aligned Version xff0c 百度网盘下载 xff0c 提取码0ac
  • jupyter远程连接服务器

    服务器终端输入命令 jupyter notebook no browser port 61 8889 本地终端输入命令 ssh N f L localhost 8888 localhost 8889 username 64 ip usern
  • win10远程Linux桌面

    在Linux服务器安装xrdp xff1a sudo apt install xrdp win10远程 xff0c win 43 R xff0c 输入mstsc xff0c 输入linux服务器ip和账户 具体参考 https www ma
  • python控制输出精度

    a span class token operator 61 span span class token number 3 1456 span b span class token operator 61 span span class t
  • 多分类混淆矩阵的理解

    借用其它博客的一张例子示意图 xff0c 该图为一个三分类问题的混淆矩阵 xff0c 对角线的值表示分类器对该类别预测正确的个数 xff0c 每一列纵轴表示这个类别真实的样本数 xff0c 例如从第一列可以得知猫一共有10 43 3 43
  • ERROR: Could not find a version that satisfies the requirement dateutil

    安装dateutil出错 xff0c 提示 ERROR Could not find a version that satisfies the requirement dateutil 解决办法 xff1a pip install pyth
  • RTX3090 + cuda 11.1 + torch1.9.0 安装 MinkowskiEngine

    创建conda环境 conda create n mink span class token assign left variable python span span class token operator 61 span span c
  • pytorch更新tensor中指定index位置的值scatter_add_

    使用scatter add 更新tensor张量中指定index位置的值 例子 span class token keyword import span torch a span class token operator 61 span t
  • Docker 私有仓库

    1 Registry 官方私有仓库 xff0c 优点 xff1a 简单 xff1b 缺点 xff1a 部署无法进行复杂的管理操作 1 1 镜像 docker pull registry 2 7 1 docker pull joxit doc
  • pytorch one-hot编码

    使用scatter 将标签转换为one hot span class token keyword import span torch num class span class token operator 61 span span clas