数据操作及其数据预处理

数据操作及其数据预处理

数据操作

基础知识
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实践

#第一节课-数据操作
'''
张量表示一个个数值组成的数组,数组的维度可能有多个
tensor的量支持:+,-,*,/,**(求幂运算)

用到的api
1.torch.arange()    根据数列级别的tensor
2.x.reshape         改变形状、 x.shape输出x的形状
3.x.numel()         统计所以元素的个数
4.torch.zeros()     生成0的矩阵
5.torch.ones()      生成1的矩阵
6.torch.tensor()    将传入的值变成tensor
7.torch.exp()       指数运算e
8.torch.cat()       把多个tesnor拼接在一起
9.x.sum()           对x里面的所以元素进行求和
10.x.numpy()        tensor转化为numpy  与6 torch.tensor()进行相互转换
11.x.item()         item得到元素值

易错的地方:广播机制
'''

import torch


#1 torch.arange()
x=torch.arange(12)
print(x)
#输出结果:tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])


#2 reshape,shape 
print(x.shape)
#访问张量的形状 torch.Size([12])
print(x.numel())
#元素的总数 12
x=x.reshape(3,4)
print(x)
'''
输出
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
'''

#4  torch.zeros()
x=torch.zeros(3,4)
print(x)
'''
输出全为0的矩阵
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
'''

#5  torch.ones()
x=torch.ones(3,4)
print(x)
'''
输出全为1的矩阵
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
'''


#6  torch.tensor()
x=[1,2,4]
print(x)
x=torch.tensor(x)
print(x)
'''
输出的区别
1 [1, 2, 4]
2 tensor([1, 2, 4])
'''


# torch.cat()
x=torch.arange(12,dtype=torch.float32).reshape((3,4))
y=torch.tensor([[1,2,3,4],[1,2,3,4],[1,2,3,4]])
out=torch.cat((x,y),dim=0)
print(out)
'''
dim=0是列拼接  dim=1是行拼接 因为shape(0,1)
dim==1
tensor([[ 0.,  1.,  2.,  3.,  1.,  2.,  3.,  4.],
        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
        [ 8.,  9., 10., 11.,  1.,  2.,  3.,  4.]])
dim==0
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.]])
'''

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值