acwing_zfw

9737

BeiHai
Behappyeveryday
Obscure
lexingsen
zjsqwq
2850g

Juicy
hyc_noi
yukino

Txoon
liuzixin123
knighz

WA_FOREVER

acwing_zfw
3个月前

# 三、PyTorch入门基础

### 4.Tensor创建编程实例

import torch

a = torch.Tensor([[1, 2], [3, 4]])
print(a)
print(a.type())

a = torch.Tensor(2, 3)
print(a)
print(a.type())

'''几种特殊的Tensor'''
a = torch.eye(2, 2)
print(a)
print(a.type())

b = torch.Tensor(2, 3)
b = torch.zeros_like(b)
b = torch.ones_like(b)
print(b)
print(b.type())

'''随机'''
a = torch.rand(2, 2)
print(a)
print(a.type())

a = torch.normal(mean=0.0, std=torch.rand(5))
print(a)
print(a.type())

a = torch.normal(mean=torch.rand(5), std=torch.rand(5))
print(a)
print(a.type())

a = torch.Tensor(2, 2).uniform_(-1, 1)
print(a)
print(a.type())

'''序列'''
a = torch.arange(0, 11, 3)
print(a)

a = torch.arange(2, 10, 3)
print(a)

a = torch.linspace(2, 10, 3) # 拿到等间隔的n个数字
print(a)

a = torch.linspace(2, 10, 4)
print(a)

a = torch.randperm(10)
print(a)

import numpy as np

a = np.array([[1, 2], [2, 3]])
print(a)


### 6.Tensor的属性-稀疏的张量的编程实践

import torch

dev = torch.device('cpu')
# dev = torch.device('cuda')
a = torch.tensor([2, 2], dtype=torch.float32, device=dev)
print(a)

# 将对角线设置为非零元素
i = torch.tensor([[0, 1, 2], [0, 1, 2]])
v = torch.tensor([1, 2, 3])
a = torch.sparse_coo_tensor(i, v, (4, 4), dtype=torch.float32, device=dev).to_dense()
print(a)


### 8.Tensor的算术运算编程实例

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)

print('a = ', a)
print('b = ', b)

print(a + b)
print(a)
print(a)

# sub
print('====== sub result ======')
print(a - b)
print(a)
print(a.sub_(b))
print(a)

# mul
print('====== mul result ======')
print(a * b)
print(torch.mul(a, b))
print(a.mul(b))
print(a)
print(a.mul_(b))
print(a)

# div

# matmul 矩阵运算
print('====== matmul result ======')
a = torch.ones(2, 1)
b = torch.ones(1, 2)
print(a @ b)
print(torch.matmul(a, b))
print(torch.mm(a, b))

# 高维tensor
a = torch.ones(1, 2, 3, 4)
b = torch.ones(1, 2, 4, 3)
print(a.shape)
print(b.shape)
print(a @ b)
print((a @ b).shape)

# pow
print('====== pow result ======')
a = torch.tensor([1, 2])
print(a.pow(3))
print(torch.pow(a, 3))
print(a**3)

# exp
print('====== exp result ======')
print(torch.exp(a))
print(a.exp())

# log
print('====== log result ======')
print(torch.log(a))
print(a.log())
print(torch.tensor(0.6931).exp())

# sqrt
print('====== sqrt result ======')
print(torch.sqrt(a))
print(a.sqrt())


### 9.in-place的概念和广播机制

import torch

a = torch.rand(2, 3)
b = torch.rand(3)
# a 2 * 1
# b 1 * 3
# c 2 * 3
c = a + b
print(a)
print(b)
print(c)
print(c.shape)

a = torch.rand(2, 1)
b = torch.rand(1, 2)
# a 2 * 1
# b 1 * 2
# c 2 * 2
c = a + b
print(a)
print(b)
print(c)
print(c.shape)

a = torch.rand(2, 1, 1, 3)
b = torch.rand(4, 2, 3)
# a 2 * 1 * 1 * 3
# b 4 * 2 * 3
# c 2 * 4 * 2 * 3
c = a + b
print(a)
print(b)
print(c)
print(c.shape)


### 10.取整/余

import torch

a = torch.rand(2, 2)
a *= 10
print(a)
print(a.floor())
print(torch.ceil(a))
print(torch.round(a))
print(a.trunc())    # 取整数部分
print(torch.frac(a))# 取小数部分
print(a % 2)


### 11.比较运算-排序-kthvalue-数据合法性校验

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)

print(a)
print(b)

'''比较'''
print(torch.eq(a, b))
print(torch.equal(a, b))

print(torch.ge(a, b))

'''排序'''
a = torch.tensor([[1, 4, 4, 3, 5],
[2, 3, 1, 3, 5]])
print(a.shape)
print(torch.sort(a, dim = 1, descending=False))

'''topk'''
a = torch.tensor([[2, 4, 3, 1, 5],
[2, 3, 5, 1, 4]])
print(a.shape)
print(torch.topk(a, k = 1, dim = 0))
print(torch.topk(a, k = 2, dim = 0))
print(torch.topk(a, k = 1, dim = 1))
print(torch.topk(a, k = 2, dim = 1))

print(torch.kthvalue(a, k = 2, dim = 0))    # 输出第二小的数
print(torch.kthvalue(a, k = 2, dim = 1))

a = torch.rand(2, 3)
print(a)
print(torch.isfinite(a))
print(torch.isfinite(a/0))
print(torch.isinf(a/0))
print(torch.isnan(a))

import numpy as np
a = torch.tensor([1, 2, np.nan])
print(torch.isnan(a))


### 12.三角函数

import torch

a = torch.zeros(2, 3)
b = torch.cos(a)
print(a)
print(b)


### 14.PyTorch与统计学方法

import torch

a = torch.rand(2, 3)

print(a)
print(torch.mean(a, dim=0))
print(torch.sum(a, dim=0))
print(torch.prod(a, dim=0))    # 计算所有元素的积

print(torch.argmax(a, dim=0))
print(torch.argmin(a, dim=0))

print(torch.std(a))     # 标准差
print(torch.var(a))     # 方差

print(torch.median(a))  # 中位数
print(torch.mode(a))    # 众数

a = torch.rand(2, 2) * 10
print(a)
print(torch.histc(a, 6, 0, 0))    # 计算a的直方图

a = torch.randint(0, 10, [10])
print(a)
print(torch.bincount(a))          # 返回每个值的频数
# bincount 可以用来统计某一类别样本的个数


### 16.PyTorch与随机抽样

import torch

torch.manual_seed(1)

mean = torch.rand(1, 2)
std  = torch.rand(1, 2)

print(torch.normal(mean, std))


### 17.PyTorch与线性代数运算

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)

print(a)
print(b)
print(torch.dist(a, b, p=1))
print(torch.dist(a, b, p=2))
print(torch.dist(a, b, p=3))

print(torch.norm(a))         # 打印出a的2范数
print(torch.norm(a, p=1))    # 打印出a的1范数
print(torch.norm(a, p='fro'))# 打印出a的核范数
print(torch.norm(a, p=0))


### 20.PyTorch与张量裁剪

import torch

a = torch.rand(3, 3) * 10
print(a)
a = a.clamp(2, 5)
print(a)

tensor([[7.3781, 6.8925, 8.6554],
[6.8215, 0.9148, 4.5870],
[5.7040, 6.4257, 3.9602]])
tensor([[5.0000, 5.0000, 5.0000],
[5.0000, 2.0000, 4.5870],
[5.0000, 5.0000, 3.9602]])


### 21.PyTorch与张量的索引与数据筛选

import torch

# torch.where
a = torch.rand(4, 4)
b = torch.rand(4, 4)

print(a)
print(b)

out = torch.where(a > 0.5, a, b)
print(out)

# torch.index_select
print("torch.index_select")
a = torch.rand(4, 4)
print(a)
out = torch.index_select(a, dim=0, index=torch.tensor([0, 3, 2]))
print(out, out.shape)

# torch.gather
print("torch.gather")
a = torch.linspace(1, 16, 16).view(4, 4)
print(a)
out = torch.gather(a, dim=0, index=torch.tensor([[0, 1, 1, 1],
[0, 1, 2, 2],
[0, 1, 3, 3]]))
print(out, out.shape)


acwing_zfw
3个月前

acwing_zfw
3个月前

acwing_zfw
4个月前

acwing_zfw
4个月前

# 七、SQL复杂查询

不存在有一种S1供应的零件该工程没用过

acwing_zfw
4个月前

#### 我配置好环境的网址：

http://81.70.27.226:8000/

#### 我的项目地址：

https://git.acwing.com/zfw/acapp

acwing_zfw
4个月前

#### [1] 将AC Terminal中的/var/lib/acwing/docker/images/docker_lesson_1_0.tar镜像上传到租好的服务器中

scp /val/lib/acwing/docker/images/docker_lesson_1_0.tar server1:


#### [2] 登录到租好的服务器

ssh server1


#### [3] 将上传的镜像提取出来

docker load -i docker_lesson_1_0.tar


#### [4] 创建该镜像的容器，设置端口映射：20000:22。

docker run -p 20000:22 --name my_docker_server -itd docker_lesson:1.0 # 创建并运行docker_lesson:1.0镜像


#### [6] 进入该容器，创建新账户acs

docker attach my_docker_server
usermod -aG sudo acs # 为新用户acs添加sudo权限


#### [7] 返回AC Terminal。配置容器中acs账户的别名及免密登录。

vim .ssh/config
***
Host myserver
HostName 123.57.47.211
User acs_2698

Host server1
HostName 81.70.27.226
User acs

Host server1_docker
HostName 81.70.27.226
User acs
Port 20000
***


#### [8] 将别名写在当前目录下的server_name.txt文件中

echo server1_docker > server_name.txt


acwing_zfw
4个月前

#### 在租好的云服务器中安装docker

docker教程

sudo apt-get update

sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io


docker --version


#### 转载

“Got permission denied while trying to connect to the Docker daemon socket at unix:///var/”

1. sudo gpasswd -a acs docker #将普通用户acs加入到docker组
2. newgrp docker #更新docker组


acwing_zfw
4个月前
##### 1. 登陆到自己的服务器（腾讯云）
ssh ubuntu@81.70.27.226

##### 2. 创建工作用户acs并赋予sudo权限
sudo adduser acs # 创建用户acs
sudo usermod -aG sudo acs

##### 3. 配置免密登陆
返回AC Terminal
vim .ssh/config

***
Host server1
HostName 81.70.27.226
User acs
***
ssh-copy-id server1


##### 5. 安装tmux和docker
sudo apt-get update
sudo apt-get install tmux

##### 6. 将AC Terminal的配置传到新服务器上
返回AC Terminal
scp .bashrc .vimrc .tmux.conf server1

##### 7. 升级Linux系统从ubuntu 18.04到ubuntu 20.04(我买的需要升级)

cat /proc/version

lsb_release -a
***
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal
***

##### 8.进入tmux根据教程安装docker

sudo apt install tree
sudo apt install ipython3

##### 9. 在ubuntu与acs用户之间切换
切换到ubuntu用户：su ubuntu



acwing_zfw
4个月前

#### [2] 配置服务器别名及免密登录

vim .ssh/config
***
Host server1
HostName 81.70.27.226
User acs
***
ssh-copy-id server1


#### [3] 将服务器别名写在当前目录下的文件中

vim server_name.txt
***
server1
***