NNDL 实验二 pytorch入门

一. 概念:张量、算子

张量:数据经常用张量(Tensor)的形式来存储。张量是矩阵的扩展与延伸,可以认为是高阶的矩阵。1阶张量为向量,2阶张量为矩阵。如果你对Numpy熟悉,那么张量是类似于Numpy的多维数组(ndarray)的概念,可以具有任意多的维度。

在深度学习框架中数据一般使用张量来进行存储,例如一维张量就是标量二维张量就是向量等等,总的来说张量就是数据的总称,区分数据可以依靠不同维数的张量。

算子:如果我们实现每个基础函数的前向函数和反向函数,就可以非常方便地通过这些基础函数组合出复杂函数,并通过链式法则反向计算复杂函数的偏导数。 在深度学习框架中,这些基本函数的实现称为算子。

二. 使用pytorch实现张量运算

1.2 张量


1.2.1 创建张量


1.2.1.1 指定数据创建张量

通过指定的Python列表数据[2.0, 3.0, 4.0],创建一个一维张量。

# 导入torch包
import torch
# 通过指定的Python列表数据[2.0, 3.0, 4.0],创建一个一维张量
ndim_1_Tensor =torch.tensor([2.0, 3.0, 4.0])
print(ndim_1_Tensor)
tensor([2., 3., 4.])

Process finished with exit code 0

1.2.1.2 指定形状创建

通过指定的Python列表数据来创建类似矩阵(matrix)的二维张量。

# 导入torch包
import torch
# 创建二维Tensor
ndim_2_Tensor = torch.tensor([[1.0, 2.0, 3.0],
                              [4.0, 5.0, 6.0]])
print(ndim_2_Tensor)
tensor([[1., 2., 3.],
        [4., 5., 6.]])

Process finished with exit code -1073741749 (0xC000004B)

1.2.1.3 指定区间创建

同样地,还可以创建维度为3、4...N等更复杂的多维张量。

# 导入torch包
import torch
#创建多维数组
ndim_3_Tensor = torch.tensor([[[1, 2, 3, 4, 5],
                               [6, 7, 8, 9, 10]],
                              [[11, 12, 13, 14, 15],
                               [16, 17, 18, 19, 20]]])
print(ndim_3_Tensor)
tensor([[[ 1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10]],

        [[11, 12, 13, 14, 15],
         [16, 17, 18, 19, 20]]])

Process finished with exit code -1073741749 (0xC000004B)

须注意到使用张量在进行多维数组的创建时每个维度元素数量必须从相等若不遵循会出现如下报错

# 导入torch包
import torch
# 创建二维Tensor
ndim_2_Tensor = torch.tensor([[1.0, 2.0],
                              [4.0, 5.0, 6.0]])
print(ndim_2_Tensor)

如出现上述形式机器报错

    ndim_2_Tensor = torch.tensor([[1.0, 2.0],
ValueError: expected sequence of length 2 at dim 1 (got 3)

1.2.2 张量的属性


1.2.2.1 张量的形状

张量共有4种形状属性(ndim、shape、shape[n]、size)

Tensor.ndim:张量的维度,例如向量的维度为1,矩阵的维度为2。
Tensor.shape: 张量每个维度上元素的数量。
Tensor.shape[n]:张量第nnn维的大小。第nnn维也称为轴(axis)。
Tensor.size:张量中全部元素的个数。

例如:创建出如下的一个四维张量,并打印出shapendimshape[n]size属性。

import torch
ndim_4_Tensor = torch.tensor([2, 3, 4, 5])
print("Number of dimensions:", ndim_4_Tensor.ndim)
print("Shape of Tensor:", ndim_4_Tensor.shape)
print("Elements number along axis 0 of Tensor:", ndim_4_Tensor.shape[0])
print("Elements number along the last axis of Tensor:", ndim_4_Tensor.shape[-1])
print('Number of elements in Tensor: ', ndim_4_Tensor.size())
Number of dimensions: 1
Shape of Tensor: torch.Size([4])
Elements number along axis 0 of Tensor: 4
Elements number along the last axis of Tensor: 4
Number of elements in Tensor:  torch.Size([4])

Process finished with exit code 0

1.2.2.2 形状的改变

对张量形状的reshape并不会改变原始数据,只是对原数据进行平铺得到改变后的形状

import torch
#定义一个shape为[3,2,5]的三维Tensor
ndim_3_Tensor =torch.tensor([[[1, 2, 3, 4, 5],
                              [6, 7, 8, 9, 10]],
                             [[11, 12, 13, 14, 15],
                              [16, 17, 18, 19, 20]],
                             [[21, 22, 23, 24, 25],
                              [26, 27, 28, 29, 30]]])
print("the shape of ndim_3_Tensor:", ndim_3_Tensor.shape)

# paddle.reshape 可以保持在输入数据不变的情况下,改变数据形状。这里我们设置reshape为[2,5,3]
reshape_Tensor = torch.reshape(ndim_3_Tensor, [2, 5, 3])
print("After reshape:", reshape_Tensor)
the shape of ndim_3_Tensor: torch.Size([3, 2, 5])
After reshape: tensor([[[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9],
         [10, 11, 12],
         [13, 14, 15]],

        [[16, 17, 18],
         [19, 20, 21],
         [22, 23, 24],
         [25, 26, 27],
         [28, 29, 30]]])

Process finished with exit code -1073741749 (0xC000004B)

1.2.2.3 张量的数据类型

import torch
# 使用torch.tensor通过已知数据来创建一个Tensor
print("Tensor dtype from Python integers:", torch.tensor(1).dtype)
print("Tensor dtype from Python floating point:", torch.tensor(1.0).dtype)
Tensor dtype from Python integers: torch.int64
Tensor dtype from Python floating point: torch.float32

Process finished with exit code -1073741749 (0xC000004B)

1.2.2.4 张量的设备位置

#可以指定位置是处于cpu还是gpu,若有多个gpu,也可以将其确切的指定在哪一块上。
print(ndim_1_Tensor .device)
cpu

Process finished with exit code -1073741749 (0xC000004B)

1.2.3 张量与Numpy数组转换

1.2.4 张量的访问

import torch
ndim_1_Tensor =torch.tensor([1., 2.])
# 将当前 Tensor 转化为 numpy.ndarray
print('Tensor to convert: ', ndim_1_Tensor.numpy())
Tensor to convert:  [1. 2.]

Process finished with exit code 0


1.2.4.1 索引和切片

import torch
# 定义1个一维Tensor
ndim_1_Tensor =torch.tensor([2.0, 3.0, 4.0])
print(ndim_1_Tensor)
print("索引:",ndim_1_Tensor[-1])
print("切片:",ndim_1_Tensor[0:2])
tensor([2., 3., 4.])
索引: tensor(4.)
切片: tensor([2., 3.])

Process finished with exit code 0

1.2.4.2 访问张量

import torch
# 定义1个一维Tensor
ndim_1_Tensor = torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])

print("Origin Tensor:", ndim_1_Tensor)
print("First element:", ndim_1_Tensor[0])
print("Last element:", ndim_1_Tensor[-1])
print("All element:", ndim_1_Tensor[:])
print("Before 3:", ndim_1_Tensor[:3])
print("Interval of 3:", ndim_1_Tensor[::3])
Origin Tensor: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
First element: tensor(0)
Last element: tensor(8)
All element: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
Before 3: tensor([0, 1, 2])
Interval of 3: tensor([0, 3, 6])

Process finished with exit code -1073741749 (0xC000004B)

1.2.4.3 修改张量

import torch
ndim_1_Tensor =torch.tensor([2,3,1,4])
ndim_1_Tensor[1]=1
print("修改张量操作:",ndim_1_Tensor)

修改张量操作: tensor([2, 1, 1, 4])

Process finished with exit code -1073741749 (0xC000004B)

1.2.5 张量的运算


1.2.5.1 数学运算

import torch
ndim_1_Tensor =torch.tensor([2,4,6,8])
ndim_2_Tensor =torch.tensor([1,2,3,4])
print("张量的加法运算结果:",ndim_1_Tensor+ndim_2_Tensor)
print("张量的减法运算结果:",ndim_1_Tensor-ndim_2_Tensor)
print("张量的乘法运算结果:",ndim_1_Tensor*ndim_2_Tensor)
print("张量的除法运算结果:",ndim_1_Tensor/ndim_2_Tensor)

张量的加法运算结果: tensor([ 3,  6,  9, 12])
张量的减法运算结果: tensor([1, 2, 3, 4])
张量的乘法运算结果: tensor([ 2,  8, 18, 32])
张量的除法运算结果: tensor([2., 2., 2., 2.])

Process finished with exit code -1073741749 (0xC000004B)

1.2.5.2 逻辑运算

import torch
ndim_1_Tensor =torch.tensor([2,4,6,8])
ndim_2_Tensor =torch.tensor([1,2,3,4])
print("判断两个张量是否不同:",ndim_1_Tensor!=ndim_2_Tensor)

判断两个张量是否不同: tensor([True, True, True, True])

Process finished with exit code -1073741749 (0xC000004B)

1.2.5.3 矩阵运算

import torch
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
print("加法运算结果:", x + y)
print("减法运算结果:", x - y)
print("乘法运算结果:", x * y)
print("除法运算结果:", x / y)
print("幂运算结果:", x ** y)
import torch
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
print("加法运算结果:", x + y)
print("减法运算结果:", x - y)
print("乘法运算结果:", x * y)
print("除法运算结果:", x / y)
print("幂运算结果:", x ** y)

1.2.5.4 广播机制

import torch
a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))
a, b
print(a+b)
tensor([[0, 1],
        [1, 2],
        [2, 3]])

进程已结束,退出代码为 0

三. 使用pytorch实现数据预处理


1. 读取数据集 house_tiny.csv、boston_house_prices.csv、Iris.csv

#读取数据集house_tiny.csv、boston_house_prices.csv、Iris.csv
import pandas as pd
df1 =r"C:\Users\320\PycharmProjects\pythonProject\Iris.csv"
df2 =r"C:\Users\320\PycharmProjects\pythonProject\boston_house_prices.csv"
df3 =r"C:\Users\320\PycharmProjects\pythonProject\house_tiny.csv"
date1=pd.read_csv(df1)
date2=pd.read_csv(df2)
date3=pd.read_csv(df3)
print(date1)
#读取date2,date3同样方法

运算结果

      Id  SepalLengthCm  ...  PetalWidthCm         Species
0      1            5.1  ...           0.2     Iris-setosa
1      2            4.9  ...           0.2     Iris-setosa
2      3            4.7  ...           0.2     Iris-setosa
3      4            4.6  ...           0.2     Iris-setosa
4      5            5.0  ...           0.2     Iris-setosa
..   ...            ...  ...           ...             ...
145  146            6.7  ...           2.3  Iris-virginica
146  147            6.3  ...           1.9  Iris-virginica
147  148            6.5  ...           2.0  Iris-virginica
148  149            6.2  ...           2.3  Iris-virginica
149  150            5.9  ...           1.8  Iris-virginica

[150 rows x 6 columns]

Process finished with exit code 0

2. 处理缺失值

#对house_tiny.csv中的NAN进行赋值
inputs, outputs = date3.iloc[:, 0:1], date3.iloc[:, 1]
inputs = inputs.fillna(inputs.mean())
print(inputs)
   NumRooms
0       3.0
1       2.0
2       4.0
3       3.0

Process finished with exit code -1073741749 (0xC000004B)

3. 转换为张量格式

x,y=torch.tensor(inputs.values),torch.tensor(outputs.values)
print(x)
print(y)
tensor([5.1000, 4.9000, 4.7000, 4.6000, 5.0000, 5.4000, 4.6000, 5.0000, 4.4000,
        4.9000, 5.4000, 4.8000, 4.8000, 4.3000, 5.8000, 5.7000, 5.4000, 5.1000,
        5.7000, 5.1000, 5.4000, 5.1000, 4.6000, 5.1000, 4.8000, 5.0000, 5.0000,
        5.2000, 5.2000, 4.7000, 4.8000, 5.4000, 5.2000, 5.5000, 4.9000, 5.0000,
        5.5000, 4.9000, 4.4000, 5.1000, 5.0000, 4.5000, 4.4000, 5.0000, 5.1000,
        4.8000, 5.1000, 4.6000, 5.3000, 5.0000, 7.0000, 6.4000, 6.9000, 5.5000,
        6.5000, 5.7000, 6.3000, 4.9000, 6.6000, 5.2000, 5.0000, 5.9000, 6.0000,
        6.1000, 5.6000, 6.7000, 5.6000, 5.8000, 6.2000, 5.6000, 5.9000, 6.1000,
        6.3000, 6.1000, 6.4000, 6.6000, 6.8000, 6.7000, 6.0000, 5.7000, 5.5000,
        5.5000, 5.8000, 6.0000, 5.4000, 6.0000, 6.7000, 6.3000, 5.6000, 5.5000,
        5.5000, 6.1000, 5.8000, 5.0000, 5.6000, 5.7000, 5.7000, 6.2000, 5.1000,
        5.7000, 6.3000, 5.8000, 7.1000, 6.3000, 6.5000, 7.6000, 4.9000, 7.3000,
        6.7000, 7.2000, 6.5000, 6.4000, 6.8000, 5.7000, 5.8000, 6.4000, 6.5000,
        7.7000, 7.7000, 6.0000, 6.9000, 5.6000, 7.7000, 6.3000, 6.7000, 7.2000,
        6.2000, 6.1000, 6.4000, 7.2000, 7.4000, 7.9000, 6.4000, 6.3000, 6.1000,
        7.7000, 6.3000, 6.4000, 6.0000, 6.9000, 6.7000, 6.9000, 5.8000, 6.8000,
        6.7000, 6.7000, 6.3000, 6.5000, 6.2000, 5.9000], dtype=torch.float64)

进程已结束,退出代码为 0


总结:这次的实验包含对张量最基础的一些操作,和使用pytorch对实验数据预处理的一些简单处理,对paddlepaddle框架有了简单的了解,扩展了框架知识。

你可能感兴趣的:(pytorch,python,深度学习)