Python深度学习:深度学习与PyTorch基础

Python深度学习:深度学习与PyTorch基础

1、深度学习与神经网络

  • 深度学习:机器学习的分支,人工神经网络为基础,对数据的特征进行学习的方法。
  • 深度学习与机器学习的区别:深度学习自动进行特征提取,数据多效果好

神经网络

  • 神经元:一个神经元的功能是求得输入向量与权向量的内积后,经一个非线性传递函数得到一个标量结果。
  • 单层神经网络:最简单的神经网络形式
  • 感知机:两层神经网络
  • 多层神经网络:输入层、隐藏层、全连接层(第N层和第N-1层中两两神经元之间都有连接,y=Wx+b)、输出层
  • 常见的激活函数:sigmoid:(0, 1),tanh:(-1, 1),relu:max(0, x),ELU:a(e^x - 1)

2、张量Tensor

  • 标量scaler:0-D Tensor
  • 向量vector:1-D Tensor
  • 矩阵matrix:2-D Tensor

3、创建Tensor

import torch
import numpy as np


t1 = torch.tensor([1, 2, 3])
print(t1)

t2 = torch.tensor(np.arange(12).reshape(3, 4))
print(t2)

t3 = torch.empty(3, 4)
print(t3)

t4 = torch.ones(3, 4)
print(t4)

t5 = torch.zeros(3, 4)
print(t5)

t6 = torch.rand(3, 4)
print(t6)

t7 = torch.randint(0, 10, [3, 4])
print(t7)

t8 = torch.randn(3, 4)
print(t8)

4、Tensor的方法和属性

获取只有一个值的Tensor:item()方法

t1 = torch.tensor([[[1]]])
print(t1.item())
t2 = torch.tensor([[[1, 2]]])
print(t2.numpy())  # 转numpy
print(t2.size())  # 获取Tensor的形状
print(t2.view(1, 2))  # 改变Tensor的形状
print(t2.dim())  # 阶
print(t2.max())
print(t2.std())
'''转置'''
print(t2.T)
print(t2.transpose(0, 1))
print(t2.permute(1, 2, 0))
print(t2[0, 0, 1])
print(t2[0])

5、Tensor数据类型

t1 = torch.ones([2, 3], dtype=torch.float32)
print(t1)
print(t1.type(torch.int32))
t2 = torch.tensor([1, 2], dtype=torch.float16)
print(t2)

6、其他方法

x = torch.rand([2, 3])
y = torch.rand([2, 3])
print(x + y)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值