【问题标题】:How to convert tensor to one hot shape?如何将张量转换为一种热形状?
【发布时间】:2021-12-29 23:24:14
【问题描述】:

我是使用 Pytorch 的初学者,在使用过程中遇到了很多问题。 我将图像加载为大小 [3240, 1, 512, 512],这些图像每个都有 3 个标签。所以我想将图像大小转换为 [3240, 3, 512, 512]。

我的代码
y_train_h = F.one_hot(y_train, num_classes=3)

结果
RuntimeError: one_hot 仅适用于索引张量。

【问题讨论】:

    标签: pytorch one-hot-encoding


    【解决方案1】:

    我还没有测试,但你可以尝试验证它:

    y_train_h = torch.zeros(3240,3,512,512)
    y_train_h[y_train.long()] = 1
    

    【讨论】:

    • 我试过你的代码,它不起作用。我认为问题是y_train_hy_train_h = F.one_hot(y_train, num_classes=3) 当我运行这段代码时,它会出现 RuntimeError。如何使用torch.nn.functional.one_hot函数?
    • 什么是 RuntimeError?你能展示一下吗? @Code_B
    • 我遇到的错误代码是这样的:“RuntimeError: one_hot is only applicable to index tensor.”
    • @Code_B 不,请。我上面写的代码是你需要的 one_hot 函数。仅使用我的代码。
    • 为了更好地理解,我修改了我的答案@Code_B
    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 2021-10-08
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    相关资源
    最近更新 更多