pytorch 初学笔记

第一次用pytorch

环境安装:

-OS:Windows7

-Python 3.6.1

-Anaconda:4.4.0


在windows环境下安装pytorch详见:https://zhuanlan.zhihu.com/p/26871672


安装完以后写了最简单的代码测试能不能导入pytorch包:


报错from torch._C import * ImportError: numpy.core.multiarray failed to import #2731

解决办法如下:https://github.com/pytorch/pytorch/issues/2731

在cmd环境下写一句话 pip install numpy -I

C:\Uses\Yourname>pip install numpy -I

如果不知道怎么用cmd环境下pip安装包的同学可以看

http://www.cnblogs.com/yuanzm/p/4089856.html


安装好以后检查环境:

import numpy
import torch
if __name__ == '__main__':
    #CUDA Test
    x = torch.Tensor([1.0])
    xx=x.cuda()
    print(xx)
 
AssertionError: 
Found no NVIDIA driver on your system. Please check that you
have an NVIDIA GPU and installed a driver from
证明我的CUDA是不能用的因为我没有NVIDIA驱动


检查CUDNN Test能不能用:

# CUDNN TEST
from torch.backends import cudnn
print(cudnn.is_acceptable(xx))
在我这检查不了,毕竟cuda都用不了
 
pytorch 介绍:
np.ndarray<-> torch. Tensor
支持numpy中200多种操作
very fast acceleration on NVIDIA GPUs
 
 
如果你有一个CPU上的tensor,
你写 torch.cuda,它就会变成GPU的tensor
 
 
Varible是tensor的封装
 
import torch
from torch.autograd import Variable

tensor = torch.FloatTensor([[1,2],[3,4]])
variable=Variable(tensor,requires_grad=True)
print (tensor)
print(variable)

结果:
Running C:/Study/pytest/pytest.py
 1  2
 3  4
[torch.FloatTensor of size 2x2]
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]


 注意矩阵的点乘和矩阵的平方是不一样的: 

矩阵点乘:

# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
# correct method
print(
    '\nmatrix multiplication (matmul)',
    '\nnumpy: ', np.matmul(data, data),     # [[7, 10], [15, 22]]
    '\ntorch: ', torch.mm(tensor, tensor)   # [[7, 10], [15, 22]]
)
矩阵乘方:
t_out = torch.mean(tensor*tensor)       # x^2
v_out = torch.mean(variable*variable)   # x^2
print(t_out)
print(v_out)    # 7.5
结果是:
[torch.FloatTensor of size 2x2]
7.5
Variable containing:
 7.5000
乘方的意思就是
tensor*tensor
Out[1]: 
  1   4
  9  16

但是时刻记住, Variable 计算时, 它在背景幕布后面一步步默默地搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图是用来干嘛的? 原来是将所有的计算步骤 (节点) 都连接起来, 最后进行误差反向传递的时候, 一次性将所有 variable 里面的修改幅度 (梯度) 都计算出来, 而 tensor 就没有这个能力啦.


直接print(variable)只会输出 Variable 形式的数据, 在很多时候是用不了的(比如想要用 plt 画图), 所以我们要转换一下, 将它变成 tensor 形式.

(来自莫烦的教程)

print(variable)     #  Variable 形式
"""
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data)    # tensor 形式
"""
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

print(variable.data.numpy())    # numpy 形式
"""
[[ 1.  2.]
 [ 3.  4.]]
"""


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pytorch是机器学习中的一个重要框架,它与TensorFlow一起被认为是机器学习的两大框架。Pytorch的学习可以从以下几个方面入手: 1. Pytorch基本语法:了解Pytorch的基本语法和操作,包括张量(Tensors)的创建、导入torch库、基本运算等\[2\]。 2. Pytorch中的autograd:了解autograd的概念和使用方法,它是Pytorch中用于自动计算梯度的工具,可以方便地进行反向传播\[2\]。 3. 使用Pytorch构建一个神经网络:学习使用torch.nn库构建神经网络的典型流程,包括定义网络结构、损失函数、反向传播和更新网络参数等\[2\]。 4. 使用Pytorch构建一个分类器:了解如何使用Pytorch构建一个分类器,包括任务和数据介绍、训练分类器的步骤以及在GPU上进行训练等\[2\]。 5. Pytorch的安装:可以通过pip命令安装Pytorch,具体命令为"pip install torch torchvision torchaudio",这样就可以在Python环境中使用Pytorch了\[3\]。 以上是一些关于Pytorch学习的笔记,希望对你有帮助。如果你需要更详细的学习资料,可以参考引用\[1\]中提到的网上帖子,或者查阅Pytorch官方文档。 #### 引用[.reference_title] - *1* [pytorch自学笔记](https://blog.csdn.net/qq_41597915/article/details/123415393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Pytorch学习笔记](https://blog.csdn.net/pizm123/article/details/126748381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值