PyTorch中是否有tf.cast函数的等价物?

8

我对PyTorch还不熟悉。TensorFlow有API tf.cast()tf.shape()。在TensorFlow中,tf.cast具有特定的用途,那么在torch中是否有等效的功能? 我有一个张量x=tensor(shape(128,64,32,32)):tf.shape(x)创建1维张量,而x.shape创建真实的维度。我需要在torch中使用tf.shape(x)

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

是否有人知道在torch/PyTorch中是否有相应的API?

1个回答

12

查看PyTorch文档

如他们所述:

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])"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接