【问题标题】:tf.cast equivalent in pytorch?pytorch 中的 tf.cast 等价物?
【发布时间】:2020-03-26 16:41:48
【问题描述】:

我是 PyTorch 的新手。 TensorFlow 有一个 API tf.cast()tf.shape()tf.cast 在 TensorFlow 中具有特定用途,在火炬中有什么等价物吗? 我有张量 x= tensor(shape(128,64,32,32)): tf.shape(x) 创建维度为 1 的张量 x.shape 创建真实的方面。我需要在火炬中使用 tf.shape(x)

tf.cast 的作用与仅仅改变torch 中的张量dtype 不同。

是否有人在 Torch/PyTorch 中有等效的 API。

【问题讨论】:

    标签: python tensorflow pytorch


    【解决方案1】:

    查看PyTorch Documentation

    正如他们所说:

    print(x.dtype) # Prints "torch.int64", currently 64-bit integer type
    x = x.type(torch.FloatTensor)
    print(x.dtype) # Prints "torch.float32", now 32-bit float
    print(x.float()) # Still "torch.float32"
    print(x.type(torch.DoubleTensor)) # Prints "tensor([0., 1., 2., 3.], dtype=torch.float64)"
    print(x.type(torch.LongTensor)) # Cast back to int-64, prints "tensor([0, 1, 2, 3])"
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2020-01-06
      • 2022-11-28
      • 2019-08-29
      • 2020-11-04
      • 2022-06-22
      • 2019-11-11
      • 2022-08-21
      • 2020-12-28
      相关资源
      最近更新 更多