动手学习深度学习(一)

最近在B站上发现一个宝藏课程,动手学习深度学习,打算这个暑假把这个课程学完,为以后的学习先奠定一下基础。老师讲的很详细,在上课的时候还有答疑问题,挺好的,值得去学一学。老师的讲课是以PyTorch框架为基础的,我之前正好学过这方面的内容。

 

一、数据操作

#数据操作

#生成一维数据
x = torch.arange(12)
print(x)
print(x.shape)

#改变形状
y = x.reshape(3,4)
print(y)
print(y.shape)

#生成全0张量
print(torch.zeros(2,2))
#生成全1张量
print(torch.ones(2,2))

#按照行进行合并
new_x = torch.cat((y,y),dim=0)
print(new_x)
#按照列进行合并 传入的参数是一个元组或者列表
new_y = torch.cat((y,y),dim=1)
print(new_y)

#tensor和numpy互相转换
A = x.numpy()
print(A,type(A))
B = torch.tensor(A)
print(B,type(B))
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
torch.Size([12])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
torch.Size([3, 4])
tensor([[0., 0.],
        [0., 0.]])
tensor([[1., 1.],
        [1., 1.]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([[ 0,  1,  2,  3,  0,  1,  2,  3],
        [ 4,  5,  6,  7,  4,  5,  6,  7],
        [ 8,  9, 10, 11,  8,  9, 10, 11]])
[ 0  1  2  3  4  5  6  7  8  9 10 11] <class 'numpy.ndarray'>
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]) <class 'torch.Tensor'>

二、数据预处理

对于缺失的数据:删除 插入

#读取数据  用空格分隔csv文件
data = pd.read_csv('lesson1.csv',sep=' ')
print(data)

#对于缺失的数据  解决方式  插值 删除
input,output = data.iloc[:,0:2],data.iloc[:,2]
print(input)
print(output)
#对于数值列 如果这一列中有空的数据 则进行添加均值
input = input.fillna(input.mean())
print(input)

打印输出 

    aaa      bbb   ccc
0  11.0  string1  2011
1   NaN  string2  2002
2   NaN  string3  2002
3  22.0  string1  2003
    aaa      bbb
0  11.0  string1
1   NaN  string2
2   NaN  string3
3  22.0  string1
0    2011
1    2002
2    2002
3    2003
Name: ccc, dtype: int64
    aaa      bbb
0  11.0  string1
1  16.5  string2
2  16.5  string3
3  22.0  string1

 对于数据中的离散值 采用one-hot编码

input = pd.get_dummies(input,dummy_na=True)
print(input)
    aaa  bbb_string1  bbb_string2  bbb_string3  bbb_nan
0  11.0            1            0            0        0
1  16.5            0            1            0        0
2  16.5            0            0            1        0
3  22.0            1            0            0        0

三、线性代数相关计算

mport torch

a = torch.arange(12,dtype=float).reshape(3,4)
b = torch.arange(12,dtype=float).reshape(4,3)
print(a,a.shape)
print(b,b.shape)

#对应元素相乘
c = a*a
print(c,c.shape)
#torch只能用于一维的点积运算

#矩阵相乘 3*4 4*3 --> 3*3
d = torch.mm(a,b)
print(d,d.shape)

#范数  必须是float才能计算
print(torch.norm(a))
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]], dtype=torch.float64) torch.Size([3, 4])
tensor([[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [ 6.,  7.,  8.],
        [ 9., 10., 11.]], dtype=torch.float64) torch.Size([4, 3])
tensor([[  0.,   1.,   4.,   9.],
        [ 16.,  25.,  36.,  49.],
        [ 64.,  81., 100., 121.]], dtype=torch.float64) torch.Size([3, 4])
tensor([[ 42.,  48.,  54.],
        [114., 136., 158.],
        [186., 224., 262.]], dtype=torch.float64) torch.Size([3, 3])
tensor(22.4944, dtype=torch.float64)

在进行张量的求和时:

比如x是一个4*5的张量

如果axis=0进行求和时,则对不同的行进行求和,得到是一个[5]的向量

如果axis=1进行求和时,则对不同的列进行求和,得到是一个[4]的向量

aa = torch.arange(20).reshape(4,5)
bb = aa.sum(axis = 0)
print(bb,bb.shape)
bb = aa.sum(axis = 1)
print(bb,bb.shape)

 如果keepdims为True时:

aa = torch.arange(20).reshape(4,5)
bb = aa.sum(axis = 0,keepdims = True)
print(bb,bb.shape)
bb = aa.sum(axis = 1,keepdims = True)
print(bb,bb.shape)

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值