【问题标题】:How I make pytorch read the numpy format?我如何让 pytorch 读取 numpy 格式?
【发布时间】:2020-03-28 17:31:55
【问题描述】:

我正在尝试使用 PyTorch 和 Keras 创建一个代码,该代码使用 BERT 算法来检测假新闻,但我得到一个错误告诉我:

can't convert np.ndarray of type numpy.bool_.
The only supported types are: double, float, float16, int64, int32, and uint8.

请访问我的Google Codelab 上的代码。错误可以在最后一个单元格中看到。

运行它的唯一要求是为训练过程下载一个CSV 文件。

【问题讨论】:

    标签: machine-learning keras deep-learning artificial-intelligence pytorch


    【解决方案1】:

    检查 numpy 数组的 dtype。

    我怀疑您必须将其转换为 改变它的dtype

    torch.from_numpy(np.array(someVar, dtype=np.uint8))
    

    【讨论】:

      【解决方案2】:

      您应该首先从 bool 转换为 uint8,然后使用 torch.from_numpy() 将 train_y 作为 Torch 的张量。

      【讨论】:

        【解决方案3】:

        我无法确认,但我相信你的问题会通过改变得到解决:

        train_y = np.array(train_labels) == 'fake'
        test_y = np.array(test_labels) == 'fake'
        

        到:

        train_y = (np.array(train_labels) == 'fake').astype(int)
        test_y = (np.array(test_labels) == 'fake').astype(int)
        

        train_y 数据当前是一个 Bool 类型的数组(True 或 False),张量需要和 int(0 或 1)。

        【讨论】:

        • 我收到一个错误:名称“设备”未定义,请您帮我解决这个问题。
        • 我对 Torch 不是很熟悉,我自己也是 TF 的人。话虽如此,我认为您可以通过索引获得设备上分配的内存:将设备替换为 0,如果您正在进行多 GPU 计算,您将获得设备 0 或设备 1 上的内存。 torch.cuda.memory_allocated(0)。 PS 看起来您正在使用我的答案,如果您不介意,我将不胜感激“接受”答案。谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-07
        • 2020-08-15
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        相关资源
        最近更新 更多