pytorch中的torch.from_numpy()

https://blog.csdn.net/qq_26020233/article/details/89007762
Tensor中并不储存数据,而是存储有对应numpy数组的数据指针的拷贝

import numpy as np
import torch
a = np.array([1,2,3])
b = torch.from_numpy(a)
# b = torch.tensor(a)
print("b 变化前:",b)

b += 1

print("a 变化后:",a)
print("b 变化后:",b)
print("a的地址:",id(a))
print("b的地址:",id(b))

'''
b 变化前: tensor([1, 2, 3], dtype=torch.int32)
a 变化后: [2 3 4]
b 变化后: tensor([2, 3, 4], dtype=torch.int32)
a的地址: 1721197547312
b的地址: 1720812881144

'''

猜你喜欢

转载自blog.csdn.net/tailonh/article/details/114194015