什么是编程思维?简化问题的艺术

1、编程思维

编程思维 不是编写程序的技巧,而是一种解决问题的思维方式,它强调的是把问题分解成更小的部分,然后逐个解决。编程思维的核心是抽象模块化自动化

  • 抽象 是指将问题简化为更基本的概念。例如,在编写一个计算两个数之和的程序时,我们可以将问题抽象为“将两个数字相加”。

  • 模块化 是指将问题分解成更小的部分。例如,在编写一个绘制正方形的程序时,我们可以将问题分解为以下几个步骤:

    • 绘制一条线
    • 旋转 90 度
    • 再绘制一条线
    • 再旋转 90 度
    • 重复步骤 1 到 4,直到绘制完成
  • 自动化 是指使用计算机程序来解决问题。例如,在编写一个计算两个数之和的程序时,我们可以使用计算机程序自动完成计算过程。

阅读更多

AI训练没有GPU独显?教你使用魔搭免费提供的阿里云端 cuda GPU

魔搭 笔记本中,可免费使用阿里云提供的AI计算资源:100小时免费的GPU计算资源,长期免费的CPU计算资源。

提供资源的配置参数

1
2
3
CPU 8核
REM 32G
GPU 可选:无GPU、16G、24G

使用教程

  • 注册魔搭账号,关联阿里云账号

  • 打开我的Notebook

  • 选择计算环境(CPU/GPU),CPU 环境长期免费,GPU环境有100小时免费(启动后才计时,停止或后就不计时了)

  • 选择预装镜像,根据你需要的 cuda、torch、TensorFlow 版本来选择。

  • 点击“启动”按钮,等待一两分钟就启动好实例了

  • 点击“查看Notebook”就可以开始使用了。

  • 可以在笔记本中直接运行系统命令或python代码。如运行:!nvcc -V 查看 NVIDIA、CUDA 的版本信息。

  • 可以打开终端执行命令,比如运行ps -e fpython -V

评价

魔搭好的方面是:使用方便,注册登录账号就可以立即使用计算资源。
不好的地方主要是:

  • 实例关闭后,镜像数据会被清空。
  • 环境给予 Ubuntu,需要会一点 Linux 命令用起来才更熟练。

后续

魔搭给的免费GPU环境只有100个小时,关闭后数据还被清空,是不是不够用?接着看我们下一期《免费3个月的阿里云端 Cuda GPU 计算(使用教程)》。

SadTalker 安装笔记

环境

  • 阿里云PAI-DSW
  • ubuntu20.04
  • pytorch:1.12
  • gpu-cu113
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
git clone https://github.com/OpenTalker/SadTalker.git

cd SadTalker

conda create -n sadtalker python=3.8

conda activate sadtalker

conda info

# 把 pip 源设为腾讯源
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple

pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113

# cpu 版
# pip --default-timeout=1000 install torch==1.12.1+cpu torchvision==0.13.1+cpu torchaudio==0.12.1 -f https://download.pytorch.org/whl/torch_stable.html

conda install ffmpeg

pip install -r requirements.txt

### Coqui TTS is optional for gradio demo.
### pip install TTS

# 下载模型

python inference.py
–driven_audio ../ds1.wav
–source_image ../2.jpg
–enhancer gfpgan
–preprocess full
–still

python inference.py –driven_audio test/ds1.wav –source_image test/1.jpg –enhancer gfpgan –cpu

3D 版

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
git clone https://github.com/OpenTalker/SadTalker.git
cd SadTalker
conda create -n sadtalker3d python=3.8
source activate sadtalker3d

conda install ffmpeg
pip install fvcore iopath
conda install libgcc gmp

pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113

# insintall pytorch3d
pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py38_cu113_pyt1110/download.html

pip install -r requirements3d.txt

### install gpfgan for enhancer
pip install git+https://github.com/TencentARC/GFPGAN


### when occurs gcc version problem `from pytorch import _C` from pytorch3d, add the anaconda path to LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/$YOUR_ANACONDA_PATH/lib/

python inference.py \
--driven_audio ../yiyi-tts.wav \
--source_image ../3.jpg \
--still \
--preprocess full \
--enhancer gfpgan

配置 Vite 支持用 “@” 路径别名导入文件

需要做如下两步配置。

1、让 vite 能识别 @ 路径别名

vite.config.js 增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
import { resolve } from 'path';

export default defineConfig {
// ...
resolve: {
alias: {
"@": resolve(__dirname, 'src'), // vite 编译的路径别名
},
// extensions: ['.js', '.json', '.ts'], // 使用路径别名时省略后缀名编译时补全,默认值为 ['.js', '.json', '.ts']
}
// ...
}

2、让 IDE 能识别 @ 路径别名

用 @ 路径别名导入文件后,IDE (如 jetbrains 家的)要能识别导入路径,实现代码提示识别,点击导入变量或导入路径能跳转到文件。

在根目录下的 jsconfig.json 加入以下内容:

1
2
3
4
5
6
7
8
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}

Vue3 组合式 API 使用 i18n 实现本地化

Vue3 使用选项式 API 较容易,按文档上手就行,这里就不多说。组合式 API 上手会比较麻烦一点。

创建实例

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
// app.js
import { createApp } from 'vue';
import { createI18n } from 'vue-i18n';
import App from './App.vue';

const i18n = createI18n({
locale: 'cn', // 设置当前语言
legacy: false, // 要支持组合式API,此项必须设置为false;
// globalInjection: true, // 全局vue视图中注册$t方法,vue-i18n v9.2 之后默认是 true
messages: {
cn: {
message: {
hello: '你好',
},
},
en: {
message: {
hello: 'hello',
},
},
},
});

const app = createApp(App);
app.use(i18n);
app.mount('#app')

基本使用

1
2
3
4
5
6
7
8
9
10
<!-- App.vue -->
<script setup>
import { useI18n } from 'vue-i18n';
const { t } = useI18n(); // 组合式 API 使用 vue-i18n,关键是 createI18n 的 legacy 参数要设置成 false
console.log(t('message.hello'));
</script>

<template>
<div>在视图中使用语言: {{ $t('message.hello') }}</div>
</template>

自定义全局方法使用 vue-i18n

组合式 API 每次要 import 又要导出要用的函数,需要频繁使用,这样太麻烦了。
我们可以定义 window.$t 全局函数,这样在 setup 中也能直接用 $t()取得语言信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// app.js
import { createApp } from 'vue';
import { createI18n } from 'vue-i18n';
import App from './App.vue';

const i18n = createI18n({
locale: 'cn',
messages: {
cn: {
hello: '你好',
},
en: {
hello: 'hello',
},
},
});

window.$t = function() {
return i18n.global.t(...arguments);
}

const app = createApp(App);
app.use(i18n);
app.mount('#app')

备注

更多用法见官方文档: https://vue-i18n.intlify.dev/guide/

Git 设置 Socks5 代理

全局设置代理

1
2
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

取消全局代理

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

指定域名使用代理

1
2
3
4
5
#只对github.com
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

#取消代理
git config --global --unset http.https://github.com.proxy

在 .git/config 配置文件中设置代理

.git/config 文件中添加配置:

1
2
[https]
proxy = socks5://127.0.0.1:1080

ssh 代理连接(使用 ssh 访问 git 的解决方案)

前面设置的都是 https 代理, GitHub 都要求通过 ssh key 来提交,我们可以在 ~/.ssh/config 配置文件中设置使用代理连接 ssh

1
2
3
4
5
6
7
8
9
10
Host github.com
HostName github.com
User git
IdentityFile C:\Users\cmpan\.ssh\github
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect" -S 127.0.0.1:1080 -a none %h %p
``````

```sh
# MacOS
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

终极方案

如果你有一台外网的 Linux 服务器,你可以用该服务器作为代理。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 作为代理的ssh连接参数
Host pxy
HostName IP地址
Port 22
User pxy
IdentityFile ~/.ssh/pxy

# GitHub 连接参数
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_auth
ProxyCommand ssh pxy -W %h:%p # pxy 为代理ssh连接对应的 Host,在 Linux/Mac/Windows 上通用

WSL使用指南

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 查看已安装发行版
wsl -l -v

# 设置默认发行版
wsl --set-default <Distribution Name>

# 启动默认发行版
wsl

# 启动指定发行版
wsl -d <Distribution Name>

# 指定登录用户启动 wsl
wsl -u <User Name>

# 更改发行版的默认用户
<DistributionName> config --default-user <Username>

# 关闭 wsl 的运行
wsl --shutdown

# 关闭指定发行版
wsl --terminate <Distribution Name>

更多命令见 WSL 官网

访问宿主机网络服务

在 wsl 中,可使用虚拟宿主机IP访问宿主机服务。使用以下命令查询宿主机IP:

1
ip route show | grep -i default | awk '{ print $3}'

打开 WSL Hyper-V 防火墙时,得到的是WSL虚拟防火墙的IP,没打开是 WSL 虚拟的IP,可使用 powershell 命令 ipconfig 查看。

宿主机访问 wsl 网络服务

默认情况下(即WSL 配置 localhostForwarding=true),wsl 映射端口到宿主机,可直接用 localhost 访问 wsl 网络服务。

注:WSL2 配置 networkingMode=mirrored 时,localhostForwarding 配置无效,可在 wsl 中用 localhost 访问宿主网络服务。

压缩 WSL2 的 vhdx 文件

vhdx文件

vhdx文件是虚拟机的虚拟磁盘文件,随着使用时间变长,vhdx 内会产生冗余文件,可以使用 diskpart 工具进行压缩。

操作步骤

1、关掉使用vhdx文件的进程。
cmd 命令:

1
wsl --shutdown

2、命令行启动 diskpart 工具,cmd命令:

1
diskpart

3、选择vhdx文件

cmd 命令:

1
select vdisk file = "D:\WSL\Debian-12-ext4.vhdx"

4、执行压缩命令:

cmd 命令:

1
compact vdisk

等待压缩进度到100即可。

参考:

https://learn.microsoft.com/zh-cn/windows/wsl/disk-space

Debian 新系统部署

基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 更新
sudo apt update
sudo apt upgrade
# 添加新用户
sudo adduser 用户名 -g users
# 给用户添加 sudo 权限
sudo usermod -aG sudo 用户名
# 配置 ssh key 登录
sudo mkdir -p /home/用户名/.ssh
vim /home/用户名/.ssh/authorized_keys
# 贴入 ssh 公钥
chown -R 用户名:users /home/用户名

设置自定义命令

/etc/profile 文件添加一下配置

1
2
3
4
5
alias l="ls -lhF --color"
alias ll="ls -lAhF --color"
alias la="ls -A --color"
alias clean="history -c; history -w"
alias ws="cd ~/workspace"

source /etc/profile

ssh 登录设置

1
2
3
sudo vim /etc/ssh/sshd_config
PasswordAuthentication no # 禁用密码登录
PermitRootLogin no # 禁止root用户登录,可通过user0登录后切换到root

安装应用包

1
sudo apt install git

Windows 11 配置 ssh-agent,秘钥登录自动填写key口令

ssh-agent 可以保存 ssh 秘钥的口令,让我们用 ssh 秘钥登录时,不需要输入秘钥的口令。

Windows 安装 git 后, 其带的 OpenSSH 被安装到 C:\Windows\System32\OpenSSH 文件夹,但是 ssh-agent 服务没有启动。

打开管理员权限的 Power Shell,输入命令:

1
2
3
4
# 把 `ssh-agent` 添加为开机自启动的系统服务
Set-Service ssh-agent -StartupType Automatic
# 启动 ssh-agent 服务
Start-Service ssh-agent

这样我们就把 ssh-agent 添加为开机自启动的系统服务了。

1
2
3
4
5
# 把私钥加入 ssh-agent
ssh-add .ssh\id_rsa

# 查看已加入 ssh-agent 的私钥
ssh-add -l