当前位置:网站首页>pytorch one-hot tips
pytorch one-hot tips
2022-08-03 08:32:00 【OPTree412】
首先介绍一下np.eye()
numpy.eye(N,M=None,k=0,dtype=<class 'float'>,order='C)
| 参数 | 类型 | Value |
|---|---|---|
| N | int | Indicates the number of lines of output |
| M | int型 | 输出的列数,默认为N |
| k | int型 | 对角线的下标,默认为0表示的是主对角线,负数表示的是低对角,正数表示的是高对角. |
| dtype | $1 | 数据的类型,可选项,The data type of the returned data |
| order | {‘C’,‘F’} | 可选项,That is, the form of the output array is in accordance withClanguage line precedence’C’,还是按照FortranColumn-majority of the form‘F’存储在内存中 |
例子:
import numpy as np
labels = np.array([[1], [2], [0], [1]])
print("labels的大小:", labels.shape, "\n")
# Because our category is from0-2,所以这里是3个类
a = np.eye(3)[1]
print("If the corresponding category number is 1,那么转成one-hot的形式", a, "\n")
a = np.eye(3)[2]
print("If the corresponding category number is 2,那么转成one-hot的形式", a, "\n")
a = np.eye(3)[1, 0]
print("1转成one-hotThe first number of the array is :", a, "\n")
# The difference between the results here and above,注意!!!
a = np.eye(3)[[1, 2, 0, 1]]
print("If the corresponding category number is 1,2,0,1,那么转成one-hot的形式\n", a)
res = np.eye(3)[labels.reshape(-1)]
print("labels转成one-hot形式的结果:\n", res, "\n")
print("labels转化成one-hot后的大小:", res.shape)

边栏推荐
猜你喜欢
随机推荐
Unity关于编辑器扩展自定义标签,方便扩展Inspector
"Swordsman Offer" brush questions print from 1 to the largest n digits
Guava-字符串工具
浅析什么是伪类和伪元素?伪类和伪元素的区别解析
BOM系列之localStorage
0day_Topsec上网行为管理RCE
Evaluate: A detailed introduction to the introduction of huggingface evaluation indicator module
MySQL1
行业 SaaS 微服务稳定性保障实战
IDEA2021.2安装与配置(持续更新)
mysql的innodb存储引擎和myisam存储引擎的区别
window的供选数据流
【无标题】
英文语法-状语从句
day12---接口和协议
dflow入门1——HelloWorld!
frp: open source intranet penetration tool
mysql服务器上的mysql这个实例中表的介绍
HCIP实验(06)
“==”和equals的区别









