pytorch张量

本文详细介绍PyTorch中张量的基本操作,包括张量的创建、数学运算、形状变化及设备转移等,同时展示了如何将PyTorch与Numpy进行有效转换,以及CUDA张量的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from __future__ import print_function
import torch
x = torch.empty(5,3)
print(x)
tensor([[9.1834e-41, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 1.0762e-42, 0.0000e+00],
        [0.0000e+00, 5.6718e-11, 0.0000e+00]])
x = torch.rand(5,3)
print(x)
tensor([[0.0186, 0.8807, 0.7894],
        [0.3810, 0.7405, 0.5440],
        [0.9247, 0.7272, 0.1362],
        [0.4587, 0.0767, 0.0761],
        [0.8206, 0.0725, 0.3316]])
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
x = torch.tensor([5.5,3])
print(x)
tensor([5.5000, 3.0000])
x = x.new_ones(5,3,dtype=torch.double) # new_*方法创建对象
print(x)

x=torch.randn_like(x,dtype=torch.float) # 覆盖 dtype
print(x)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[ 1.1492, -0.3027, -0.3889],
        [-0.0061, -0.8942,  2.5887],
        [-1.8800, -1.1619, -0.6853],
        [-0.2841,  0.1407,  0.0970],
        [ 0.0358, -0.4802,  0.7781]])
print(x.size())
torch.Size([5, 3])
y = torch.rand(5,3)
print(x)
print(y)
print(x+y)
tensor([[ 1.1492, -0.3027, -0.3889],
        [-0.0061, -0.8942,  2.5887],
        [-1.8800, -1.1619, -0.6853],
        [-0.2841,  0.1407,  0.0970],
        [ 0.0358, -0.4802,  0.7781]])
tensor([[0.2793, 0.7743, 0.4154],
        [0.5497, 0.4504, 0.7533],
        [0.1075, 0.5314, 0.1604],
        [0.6892, 0.6163, 0.0620],
        [0.7559, 0.3567, 0.2655]])
tensor([[ 1.4285,  0.4716,  0.0266],
        [ 0.5437, -0.4439,  3.3420],
        [-1.7725, -0.6305, -0.5249],
        [ 0.4050,  0.7570,  0.1590],
        [ 0.7917, -0.1235,  1.0436]])
print(torch.add(x,y))
tensor([[ 1.4285,  0.4716,  0.0266],
        [ 0.5437, -0.4439,  3.3420],
        [-1.7725, -0.6305, -0.5249],
        [ 0.4050,  0.7570,  0.1590],
        [ 0.7917, -0.1235,  1.0436]])
result = torch.empty(5,3)
torch.add(x,y,out = result)
print(result)
tensor([[ 1.4285,  0.4716,  0.0266],
        [ 0.5437, -0.4439,  3.3420],
        [-1.7725, -0.6305, -0.5249],
        [ 0.4050,  0.7570,  0.1590],
        [ 0.7917, -0.1235,  1.0436]])
y.add_(x)
print(y) # 以“_”结尾的操作会用结果替换原变量。如"x.copy_(y)","x.t_()"
tensor([[ 1.4285,  0.4716,  0.0266],
        [ 0.5437, -0.4439,  3.3420],
        [-1.7725, -0.6305, -0.5249],
        [ 0.4050,  0.7570,  0.1590],
        [ 0.7917, -0.1235,  1.0436]])
print(x)
print(x[:, 1]) # 索引第二列
tensor([[ 1.1492, -0.3027, -0.3889],
        [-0.0061, -0.8942,  2.5887],
        [-1.8800, -1.1619, -0.6853],
        [-0.2841,  0.1407,  0.0970],
        [ 0.0358, -0.4802,  0.7781]])
tensor([-0.3027, -0.8942, -1.1619,  0.1407, -0.4802])
# torch.view改变张量维度及大小,与Numpy的reshape类似
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # size -1 从其他维度推断
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
x = torch.randn(1)
print(x)
print(x.item()) # .item 转换为python数据类型
tensor([2.9229])
2.922903060913086
# Numpy转换
a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
[1. 1. 1. 1. 1.]
a.add_(1)
print(a,b)
tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a) # 使用.from_numpy()转换
np.add(a, 1, out=a)
print(a,b)
[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
# CUDA张量
# is_available 函数判断是否有cuda可以使用
# torch.device 将张量移动到指定设备
if torch.cuda.is_available():
    device = torch.device("cuda")
    y = torch.ones_like(x, device=device) # 直接从GPU创建张量
    x = x.to(device) # .to("cuda") 将张量移动到cuda中
z = x + y
print(z)
print(z.to("cpu", torch.double))
tensor([4.3497, 5.0759, 2.6086, 2.4112, 3.5776, 5.4971, 4.8984, 1.5604, 3.7739,
        3.1584, 2.7664, 0.9340, 2.6396, 2.8920, 2.3703, 1.5383])
tensor([4.3497, 5.0759, 2.6086, 2.4112, 3.5776, 5.4971, 4.8984, 1.5604, 3.7739,
        3.1584, 2.7664, 0.9340, 2.6396, 2.8920, 2.3703, 1.5383],
       dtype=torch.float64)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值