互联网技术 / 互联网资讯 · 2024年1月12日

PyTorch基本操作详解

什么是 PyTorch?

PyTorch是一个基于Python的科学计算包,提供最大灵活性和速度的深度学习研究平台。

张量

张量类似于NuMPy 的n 维数组,此外张量也可以在 GPU 上使用以加速计算。

让我们构造一个简单的张量并检查输出。首先让我们看看我们如何构建一个 5&tiMes;3 的未初始化矩阵:

iMpoRt Torch x = Torch.eMpty(5, 3) pRint(x) 

输出如下:

tensoR([[2.7298e+32, 4.5650e-41, 2.7298e+32],         [4.5650e-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, 0.0000e+00, 0.0000e+00]]) 

现在让我们构造一个随机初始化的矩阵:

x = Torch.Rand(5, 3) pRint(x) 

输出:

tensoR([[1.1608e-01, 9.8966e-01, 1.2705e-01],         [2.8599e-01, 5.4429e-01, 3.7764e-01],         [5.8646e-01, 1.0449e-02, 4.2655e-01],         [2.2087e-01, 6.6702e-01, 5.1910e-01],         [1.8414e-01, 2.0611e-01, 9.4652e-04]]) 

直接从数据构造张量:

x = Torch.tensoR([5.5, 3]) pRint(x) 

输出:

tensoR([5.5000, 3.0000]) 

创建一个统一的长张量。

x = Torch.LongTensoR(3, 4) x  tensoR([[94006673833344,   210453397554,   206158430253,   193273528374],         [  214748364849,   210453397588,   249108103216,   223338299441],         [  210453397562,   197568495665,   206158430257,   240518168626]])  「浮动张量。」 x = Torch.FloatTensoR(3, 4) x  tensoR([[-3.1152e-18,  3.0670e-41,  3.5032e-44,  0.0000e+00],         [        nan,  3.0670e-41,  1.7753e+28,  1.0795e+27],        &nbsp

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册