学习笔记(pytorch库 1)

前言

这学期在做一个标准智能化工具编写的项目,需要用到一些python的深度学习库。本人开此栏目仅记录一下个人的学习流程。

In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters.(介绍了张量的作用)

初始化一个张量的方法

#initialize a Tensor
import torch
data1=[[1,2],[3,4]]
x_data=torch.tensor(data1)
print(x_data)
#tensor的继承
x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")

shape

定义:a tuple that describes the dimensionality of the tensor

E.G.

shape=(2,3,)
tensor_rand=torch.rand(shape)
tensor_ones=torch.ones(shape)
tensor_zeros=torch.zeros(shape)


print(f"Random Tensor: \n {tensor_rand} \n")
print(f"Ones Tensor: \n {tensor_ones} \n")
print(f"Zeros Tensor: \n {tensor_zeros}\n")
Output:

Random Tensor: 
 tensor([[0.2582, 0.6818, 0.4922],
        [0.0537, 0.9487, 0.4187]]) 

Ones Tensor: 
 tensor([[1., 1., 1.],
        [1., 1., 1.]]) 

Zeros Tensor: 
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

以上是结果。

属性

shape:形状。

dtype:datatype即数据类型。

device:数据存储位置。

Operation

特性:

Each of these operations can be run on the GPU (at typically higher speeds than on a CPU). If you’re using Colab, allocate a GPU by going to Runtime > Change runtime type > GPU.

解释一下什么是Colab

  1. Google Colab 是一个免费的云服务并支持免费的 GPU。
  2. Colab 是Google的且服务器在国外。
  3. 只需要在 Google Drive 上,安装 colab 即可使用。如果需要上传数据,可以上传到 Google Drive 上,并在 colab 中挂载,就可以直接访问。

CPU 是为通用计算设计的,主要设计目标是低延迟。其中含有强大而少量的算术逻辑单元 ALU 和容量较大的缓存 cache。

GPU 目前主要的应用场景是:图像渲染、视频解码、深度学习、科学计算等,最适合多数据流单指令流的计算,主要设计目标是高带宽。其中含有大量算术逻辑单元 ALU,cache 较小。



作者:Y.Shu
链接:https://www.zhihu.com/question/19717029/answer/1081323191
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

By default, tensors are created on the CPU. We need to explicitly move tensors to the GPU using .to method (after checking for GPU availability). Keep in mind that copying large tensors across devices can be expensive in terms of time and memory!

在pytorch中,即使是有GPU的机器,它也不会自动使用GPU,而是需要在程序中显示指定。调用model.cuda(),可以将模型加载到GPU上去。这种方法不被提倡,而建议使用model.to(device)的方式,这样可以显示指定需要使用的计算资源,特别是有多个GPU的情况下。
————————————————
版权声明:本文为CSDN博主「Wanderer001」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_36670529/article/details/107144018

# We move our tensor to the GPU if available
if torch.cuda.is_available():
    tensor = tensor.to("cuda")

If you’re familiar with the NumPy API, you’ll find the Tensor API a breeze to use.

Standard numpy-like indexing and slicing:

可是我早忘了numpy是怎么切片的啊呜呜呜:

NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。

NumPy 通常与 SciPy(Scientific Python)和 Matplotlib(绘图库)一起使用。 这种组合广泛用于替代 MatLab,是一个流行的技术计算平台。 但是,Python 作为 MatLab 的替代方案,现在被视为一种更加现代和完整的编程语言。

tensor = torch.ones(4, 4)
print(f"First row: {tensor[0]}")
print(f"First column: {tensor[:, 0]}")
print(f"Last column: {tensor[..., -1]}")
tensor[:,1] = 0
print(tensor)
First row: tensor([1., 1., 1., 1.])
First column: tensor([1., 1., 1., 1.])
Last column: tensor([1., 1., 1., 1.])
tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值