编程经验积累

1

使用python初始化列表

a = [i  for i in range(1,1001)]  #1-1000的数字

2

使用conda创建环境时遇到

Anaconda: An unexpected error has occurred. Conda has prepared the above report.

解决方法

conda clean -i

3

查看conda环境

conda info --envs

4

linux环境下查看当前位置

pwd

5

linux下载网站上的文件

wget -c 网址

6

更改文件名字

mv 原名 改名

7

新建文件

tourch 文件名

新建目录

mkdir 目录名

8

git clone 和 wget 的区别

first: git clone下载后得到一个目录,可以使用cd进入目录;wget得到的是一个文件

9

linux里面"/"和波浪线的区别:“/”表示的是根目录,波浪线表示的是当前目录

10

Ubuntu "E: Unable to locate package"错误解决办法

sudo apt-get update

更新后即可

11

在wsl访问D盘(windows本地文件)

cd /mnt

然后你将看到本地的磁盘

12

linux下查看当前目录的指定文件(文件名指定)

find ./ -name 文件名

./为当前目录,-name为按照文件名查询

13

解决“ Permission denied”报错
在当前目录输入

 sudo chmod -R 777  /当前工作目录

之后输入用户密码就行了

14

使用vscode连接远程服务器

(1)跟着这篇文章完成前期的一些工作
https://zhuanlan.zhihu.com/p/141205262

(2)设置config

按照这种格式通过跳板机设置config

Host JumpMachine
    # 跳板机的ip地址
    HostName A
    # 你跳板机的用户名
    User username
    # 跳板机登录端口 
    Port 22
 
Host TargetMachine
    # 目标机的ip地址
    HostName B
    # 你目标机的用户名
    User username
    # 目标机登录端口 
    #Port 8080
    ProxyCommand "openssh的安装路径"\ssh.exe -W %h:%p JumpMachine

"openssh
这个可以参考这篇文章:https://www.freesion.com/article/6189421373/

15

使用Xshell连接远程服务器
https://www.xshellcn.com/xsh_column/lianjie-linux.html

16

通过Xshel连接远程服务器(使用跳板机)
https://jingyan.baidu.com/article/d5a880ebd69c2613f147ccbf.html

注:在使用跳板机连接服务器时,在设置连接内网服务器的隧道时,目标主机为要访问的服务器的ip地址,目标端口为22(固定)

17

linux查看文件大小

ls -sh 文件名

https://jingyan.baidu.com/article/ca2d939d7867e0eb6c31ce80.html

18

解决 wget:Unable to establish SSL connection.

加上跳过验证证书的参数“--no-check-certificate”

例如

wget --no-check-certificate https://nodejs.org/dist/v11.2.0/node-v11.2.0.tar.gz

19

linux解压文件

tar -zxv -f test.tar.gz

指定解压路径

tar -zxv -f test.tar.gz -C /home/user/target
#其中-C后面就是指定的解压路径

unzip file.zip

https://zhidao.baidu.com/question/1673600351651832147.html

20

conda查看环境下的包

conda list

21

conda删除环境

conda remove --name confgf --all

删除"confgf"环境

22

linux下查看服务器的cuda版本

cat /usr/local/cuda/version.txt
nvcc -V  (大写的V)

23

conda镜像

查看当前镜像:

conda config --show-sources

切换为默认镜像

conda config --remove-key channels

删除源

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

添加国内镜像,将以下代码复制到终端

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/main
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/free
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/mro
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/msys2
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/pro
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/r

上面的这个镜像好像有点问题,我在安装pytorch的时候,指定的是安装1.9.0,但安装的却是1.3.1,换了个镜像就可以了

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

上面这个镜像

24

linux下通过conda配置Pytorch环境

新建虚拟环境

conda create -n pytorch python=3.8.5

激活环境

conda activate pytorch

添加国内镜像

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/main
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/free
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/mro
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/msys2
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/pro
conda config --add channels https://anaconda.mirrors.sjtug.sjtu.edu.cn/pkgs/r

进入Pytorch官网,选择对应版本获得下载命令

官网:https://pytorch.org/get-started/locally/

验证是否安装成功:
在创建的环境中输入python,然后输入import torch,未报错说明安装成功

25

查看python安装路径

which python

26

建立 / 删除符号连接

建立

ln -s  /usr/local/python3/bin/pip3.5  /usr/bin/pip3

/usr/local/python3/bin/pip3.6是python3.6安装路径,/usr/bin/pip3是要链接的文件

删除(两种方法)

方法一

直接到链接目录去删除(后者地址)

方法二
rm -rf目标文件名全路径也就是rm -rf / var / www / html 看仔细,一定不要rm -rf / var / www / html /,这种是删除文件目录,到时源文件的东西也没了,大家在按标签自动补全的时候注意了,会默认将最后的/补上,自己要记得删 --------例子

27

报错: Torch not compiled with CUDA enabled

出现这一问题是由cuda版本不符合导致的,需要安装一个对应版本的cuda

服务器上的cuda版本是改不了的,把pytorch重新卸载了然后再装一个新的pytorch,使其cuda版本满足要求

28

如何安装低版本的pytorch(cuda的版本低)

注意:

1、创建环境的时候python的版本不能太高,不然安装pytorch的时候会出错

2、下载安装低版本的pytorch

30

linux下如何安装anconda

https://blog.csdn.net/zhuoyuezai/article/details/82913424?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162587454416780274149259%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=162587454416780274149259&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduend~default-3-82913424.pc_v2_rank_blog_default&utm_term=linux%E5%AE%89%E8%A3%85anconda&spm=1018.2226.3001.4450

31

解决问题:Permission denied

问题原因:

1、文件权限不足(更改文件权限)

chmod -R 755 /usr/local/....   文件夹的路径

2、用户权限不足(转成root用户)

32

Pycharm查找文件

ctrl+shift+a

33

linux将A目录下的文件拷贝到B目录

cp -r dir1 /. dir2

#如果dir2目录不存在,则
cp -r dir1 dir2

表示将目录dir1指定文件复制到目录dir2

参考资料:https://www.cnblogs.com/zdz8207/p/linux-cp-dir.html

34

解决问题:AssertionError: Torch not compiled with CUDA enabled

https://blog.csdn.net/dujuancao11/article/details/114006234

35

1、使用linux rz上传文件到服务器时,如果出现传输过程中不断提醒声,不断输出字符时,可以更换到最开始目录上传然后copy到指定目录
2、如果使用rz上传网上下载的压缩包失败,可以先将压缩包解压,上传其中一个小文件,让通道跑通,然后再上传整个压缩文件(我这里解压的是rar文件)

36

linux下查看pytorch版本,查看pytorch对应的cuda版本

查看pytorch版本

python

import torch

print(torch.__version__)

查看pytorch对应的cuda版本
print(torch.version.cuda)

37

linux查看文件的时间

stat 文件名

38

conda修改虚拟环境的名称

新建一个环境,克隆原环境
conda create --name newName(新环境名) --clone oldName(旧环境名)

删除原环境
conda remove --name oldName(旧环境名) --all 

39

linux查看文件权限

ls -l  文件所在目录

总共有10个字符,如-rw-rw-r–,第一个字符表示文件类型,'-'表示普通文件,后面9个字符分别表示文件拥有者、文件所属组、其他用户的权限,前三个表示文件拥有者,中间三个表示文件所属组,最后三个表示其他用户
https://www.cnblogs.com/sxdcgaq8080/p/7498906.html

40

try , catch , finally

try{
    //可能抛出异常
}catch(Exception e){
    //捕获异常
}finally{
    //不管有无异常都执行
}

分为几种情况:
(1)没有发生异常,try内代码执行结束后执行finally里的代码
(2)出现异常,且被catch捕捉,在catch内内容执行结束后执行finally内代码
(3)出现异常,但没有被捕捉,则先执行finally内代码,然后将异常抛给上层
try/catch/finally中catch不是必要的,没有catch表示不用捕捉,异常自动向上传递

def find_index(string):
    index_num = -1
    try:
        len(string)
    except IOError:
        index_num = -1
    else:
        for i in range(len(string)):
            index = string[i][0:6]
            if index == 'region':
                index_num = i
    finally:
        #print(index)
        return (index_num)


41

linux下查看文件的大小

 du -h --max-depth=1

42

linux下查看文件类型

file -L 文件名
file 文件所在路径

https://jingyan.baidu.com/article/c1a3101ee08d159e656debc6.html

43

linux下删除服务器下某用户的所有进程(给出用户名)

pkill -u 用户名

44

查看服务器下当前的python的解释路径

进入python环境

python

查看解释路径

import sys
sys.executable

45

python去掉字符串里面的所有空格
https://www.cnblogs.com/zywscq/p/5325604.html

46

numpy保存数据
np.save("要保存的路径”,要保存的数据)

47

使用python,在a.py文件里面调用b.py文件

要通过sys导入相应文件的路径才可以

import sys
sys.path.append("Path")
import ~~
```
其中Path是需要导入py文件的路径

https://blog.csdn.net/maka_uir/article/details/113624859
## 48
python读取txt文件

import numpy as np
import pandas as pd
df_news = pd.read_table(‘datingTestSet2.txt’,header = None)
df_news

## 49
python读取txt文件,按行读取

````
f = codecs.open('PATH', mode='r', encoding='GB2312')
list1 = []
while line:
    a = line.split()
    list1.append(a)
    line = f.readline()
f.close()

https://blog.csdn.net/qq_38984928/article/details/82998151?ops_request_misc=&request_id=&biz_id=102&utm_term=python%E8%AF%BB%E5%8F%96txt%E6%96%87%E4%BB%B6%E6%95%B4%E5%88%97%E6%95%B0%E6%8D%AE&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-7-.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187

50

linix统计某一目录下的文件个数

ls -l |grep "^-"|wc -l
#其中"^-"为查询条件,“.csv"这类的
ls -l |grep "^d"|wc -l

https://www.cnblogs.com/Allen-rg/p/12165698.html
https://zhuanlan.zhihu.com/p/377523024

51

python读取CSV文件

import pandas as pd
test_csv = pd.read_csv(Path_test_csv)

52

获取tensor对象的值

tensor.item()

53

转成tensor类型的量

predicted = torch.tensor(predicted)

54

python读取目录下的文件名

import os
filePath = 'C:\\myLearning\\pythonLearning201712\\carComments\\01\\'
os.listdir(filePath)

注意,这里的读取的是某一目录下的不同文件的名字,在一个文件下读取文件名

55

python提取字符串中的数据

import re
str = "123af3"
number = re.sub("\D","",str)

56

python保存数据为csv格式

import pandas as pd

data_pd = pd.DataFrame(data = Data)
data_pd.to_csv("path")

将数据转成DataFrame
path_1 = {‘path’,PATH}
path_2 = pd.DataFrame(path_1)
path_2.to_csv(save_path)

如果要连续地向csv文件追加数据

record_df.to_csv(path,mode='a')  #model = 'a'

57

linux下如何查看两个文件是否相同

md5sum 文件名1

md5sum 文件名2

会输出一个编号,如果编号相同,则两个文件相同

58

python打包文件

tar zcvf new_name.tar file_name

59

python使用with open 读写文件

with open('/path/to/file', 'r') as f:
    print(f.read())

https://blog.csdn.net/xrinosvip/article/details/82019844

60

python读取json文件

with open(path_ranking_debug,'r') as f:
        data = json.load(f)

61

python新建DataFrame

record_error={'sum':err_num,'ID':[ID],'name':[seq_name],'gene':[gene_name],'error':[Error]}
record_df=pd.DataFrame(record_error)

62

获取DataFrame数据的行数和列数
https://blog.csdn.net/lwgkzl/article/details/80988126

63

python删除csv文件的一行

import pandas as pd
train_csv = pd.read_csv(path_csv)
train_csv = train_csv.drop(0) #删除csv文件的0行(第一行)

64

python画heatmap图
https://blog.csdn.net/qq_23947237/article/details/89409309

65

将服务器上的代码上传到自己的private github项目
1、在github上private一个repository A
2、在服务器上git clone A
3、进入A
4、上传文件到 A
5、git add .
6、git commit -m "A的说明“
7、git push

66

python读取pkl文件

import pickle 
pkl = pickle.load(open( path, 'rb'))

67

使用vscode调试python程序

1、记得安装python extension
2、配置launch.json文件
3、debug程序

lanuch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "Path of python",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

其中Path of python通过在服务器上用which python查询

需要注意的是,如果在服务器上配置的环境,然后现在想用vscode在本地调试,本地是没有服务器上的环境的

当我i们想在创建的虚拟环境上调试时,需要将pythonPath改成虚拟环境下的python地址,首先进入虚拟环境,然后which python得到地址

68

使用pdb调试python程序

1、import pdb
2、在终端输入python -m pdb pdb_test1.py

相关参数:https://blog.csdn.net/violet_echo_0908/article/details/58601412?ops_request_misc=&request_id=&biz_id=&utm_medium=distribute.pc_search_result.none-task-blog-2alles_rank~default-1-58601412.pc_search_all_es&utm_term=python+%E4%BD%BF%E7%94%A8pdb%E8%AE%BE%E7%BD%AE%E6%96%AD%E7%82%B9&spm=1018.2226.3001.4187

69

conda复制虚拟环境

conda create -n conda-env2 --clone conda-env1
这里conda-env2是新创建的Conda环境,conda-env1是被复制的Conda环境,
复制完成后,两个环境的Python配置是完全相同的;

70

解决问题:nohup: redirecting stderr to stdout

把最后面的 “&” 改成 “2>&1 &”

71

R2(coefficient of determination)可以为负吗

https://stats.stackexchange.com/questions/12900/when-is-r-squared-negative

72

python创建全0或者全1的列表

List = [0]*n
List_1 = [1]*n

73

python获取二维数组的某一列

import numpy as np

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a = np.array(a)
print(a[:, -1]) # [3 6 9]

74

生成一定长度的横坐标

x_lable = list(range(1,num+1))

75

python完成矩阵拼接

import numpy as np
a = [[1,2],[3,4]]
b = [[5,6],[7,8]]
#按行拼接
c = np.append(a,b,axis=1)  #要求行的维度相同
"""
[[1 2 5 6]
 [3 4 7 8]]
"""
#按列拼接
c = np.append(a,b,axis=0) #要求列的维度相同
"""
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
"""

76

python得到矩阵的行列数

import numpy as np
a = [[1,2],[3,4],[5,6]]
print(np.array(a).shape[0])  #输出行数3
print(np.array(a).shape[1])  #输出列数2

77

python数组的一些操作,合并堆叠等
https://www.w3school.com.cn/python/numpy_array_join.asp

78

python 使用dataloader批次读取数据

import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
import dataset_padding


 
class My_dataset(Dataset):
    def __init__(self):
        super().__init__()
        # 使用sin函数返回10000个时间序列,如果不自己构造数据,就使用numpy,pandas等读取自己的数据为x即可。
        # 以下数据组织这块既可以放在init方法里,也可以放在getitem方法里
        self.x = dataset_padding.data_onehot
        self.y = dataset_padding.y_lable
        self.src,  self.trg = [], []
        for i in range(1706):
            self.src.append(self.x[i])
            self.trg.append(self.y[i])
           
    def __getitem__(self, index):
        return self.src[index], self.trg[index]

    def __len__(self):
        return len(self.src) 
        
 # 或者return len(self.trg), src和trg长度一样
 
data_train = My_dataset()
data_test = My_dataset()
data_loader_train = DataLoader(data_train, batch_size=1, shuffle=False)
#i_batch的多少根据batch size和def __len__(self)返回的长度确定
#batch_data返回的值根据def __getitem__(self, index)来确定
#对训练集:(不太清楚enumerate返回什么的时候就多print试试)
for i_batch, batch_data in enumerate(data_loader_train):
    print(i_batch)  # 打印batch编号
    print(batch_data[0])  # 打印该batch里面src
    print(batch_data[1])  # 打印该batch里面trg
````
## 79
python使数组行列互换
````
arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
print(arr)
ar2 = [ [row[i] for row in arr] for i in range(len(arr[0])) ]
print(ar2)

https://blog.csdn.net/qq284489030/article/details/88745540

80

电脑输入的繁简体转换
ctrl+shift+f

81

unsqueeze添加数组的维度

test = torch.Tensor(data_test[0]) #维度(a,b)
a = test.unsqueeze(0) #维度(1,a,b)

82

python进行二位数组的行列转换

arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
ar2 = [ [row[i] for row in arr] for i in range(len(arr[0])) ]
print(ar2)

https://blog.csdn.net/qq284489030/article/details/88745540

83

torch tensor求均值

a.mena()  #a是一个tensor类型的数据

https://blog.csdn.net/sinat_40624829/article/details/103806960?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163143441816780271544373%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163143441816780271544373&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-3-103806960.pc_search_ecpm_flag&utm_term=pytorch%E8%AE%A1%E7%AE%97tensor%E5%9D%87%E5%80%BC&spm=1018.2226.3001.4187

84

python将数据存入excel文件

#1. 创建文件对象
f = open('tesult_1.csv','w',encoding='utf-8')

#2. 基于文件对象构建 csv写入对象
csv_writer = csv.writer(f)

#3. 构建列表头
csv_writer.writerow(["epoch","loss","pre_test"])

#4. 写入csv文件内容
Epoch = range(1,501)
for i in range(500):
    csv_writer.writerow([[Epoch[i],loss_all[i],test_all[i]]])

#5. 关闭文件
f.close()

85

如何使用paddle的visualdl绘制模型的loss和acc曲线

callback机制结合VDL

callback = paddle.callbacks.VisualDL(log_dir=‘./log/train/visualdl_log_dir’)

训练保存并验证模型

#设置collback的路径并获取callback
callback = paddle.callbacks.VisualDL(log_dir='./log/train/visualdl_log_dir')
#训练模型的时候传入callback参数
model.fit(train_dataset,test_dataset,epochs=epoch,batch_size=batch_size,callbacks=callback)

######
#之后会得到一个log文件,把这个log文件下载到本地或者上传到服务器上,之后进入对应的目录,输入以下代码
visualdl --logdir ./

复制输出的网址在浏览器中打开

86

python计算莱文斯坦距离

import Levenshtein
s1 = 'kitten'
s2 = 'sitting'
s3 = 'kitten'
ratio = Levenshtein.ratio(s1, s3)
dist = Levenshtein.distance(s1, s3)
print('ratio={0}, dist={1}'.format(ratio, dist))

其中,tatio是两个字符串的相似度,dist是两个字符的莱文斯坦距离

87

python创建全为0的矩阵

import numpy as np
a = np.zeros((2,3))

88

划分训练集和测试集

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(data_train, data_train_tar,test_size=0.2, random_state=110)

89

在jupyter里面插入图片

from IPython.display import Image
Image(filename = 'Picture/train_acc.png', width=10, height=10)

90

将三维的Tensor向量转成二维的

import torch
if __name__ == '__main__':
    # a = torch.tensor([ [ [1,2,3,2,1],
    #                      [2,5,6,3,6] ],
    #                    [ [2,1,5,9,8],
    #                      [4,6,8,1,1] ] ],dtype=torch.float)

    a = torch.tensor([[[1,2,3,2,1],[2,5,6,3,6],[2,1,5,9,8],[4,6,8,1,1]]],dtype=torch.float)

    print(a.shape)
    a = torch.reshape(a,(1,20))
    print(a.shape)
    print(a)

91

win10获取本机的ip地址

ipconfig/all 

https://blog.csdn.net/weixin_44944193/article/details/107363777?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163245106516780271593703%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163245106516780271593703&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-3-107363777.pc_search_ecpm_flag&utm_term=%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E7%94%B5%E8%84%91ip%E5%9C%B0%E5%9D%80&spm=1018.2226.3001.4187

92

vscode设置字体
https://blog.csdn.net/weixin_43796685/article/details/102768307?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163249178716780274113451%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163249178716780274113451&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-102768307.pc_search_ecpm_flag&utm_term=vscode%E5%AD%97%E4%BD%93%E5%A4%A7%E5%B0%8F%E8%AE%BE%E7%BD%AE&spm=1018.2226.3001.4187

vscode如何设置字体格式
font family里面设置

93

xshell设置字体
点击会话,选择属性,然后点击外观更改页面设置

94

如何设置屏幕保护

在输入栏输入 “屏幕保护程序”
在这里插入图片描述
然后选择在恢复时显示登陆屏幕
在这里插入图片描述

95

如何查看服务器内存使用情况

free -m

Mem:内存的使用情况总览表

或者先
pip install glances
然后在命令行
glances
查看即可

96

如何在在训练模型的时候加进度条

from tqdm import tqdm
for epoch in range(6):
    for batch_data in tqdm(data_loader):
    ······
 #然后后面的for循环里的range换成trange
 from tqdm import trange
 for i in trange(len(validation_data)):
     ·····

97

Google浏览器选择默认下载地址

google浏览器里面选择设置,找到高级选项,然后可以更改地址了

98

服务器上查看当前的任务

jobs -l

99

win10如何设置锁屏后不断网
在下面这个框里输入电源和睡眠设置
在这里插入图片描述
然后把时间设为从不

100

pycharm进行全局搜索
ctrl+shift+F(注意这里时大写的F,小写的f没用,小写的f是切换中文繁简体的

生活就在眼前,为什么一定要去问莫扎特——荒原狼
不一样的生活,理解人们不同的心灵慰藉

second

1

paddle保存与加载模型

保存模型

model.save('./model')

保存后得到一个dpparams和dpopt文件,只需加载dpparams文件即可
加载模型

# 加载读取好的模型进行预测
from paddle.metric import Accuracy
momentum = 0.9
weight_decay = 5e-4
optim = paddle.optimizer.Momentum(learning_rate=0.001, parameters=model.parameters(), momentum=momentum,weight_decay=weight_decay)
model_path = "model/model.pdparams"
net_cls = LeNet()
model = paddle.Model(net_cls)
model.load(model_path)
model.prepare(optim,paddle.nn.CrossEntropyLoss(),Accuracy())
results = model.evaluate(test_dataset, batch_size=64, verbose=1)
print(results)

2

fit中的vebose

verbose:日志显示
verbose = 0 为不在标准输出流输出日志信息
verbose = 1 为输出进度条记录
verbose = 2 为每个epoch输出一行记录
注意: 默认为 1

verbose = 0,在控制台没有任何输出

verbose = 1 :显示进度条

3

python中没有switch语句,可以通过if-elseif来实现

4

Pytorch获取模型参数的数量

    # Find total parameters and trainable parameters
    total_params = sum(p.numel() for p in model.parameters())
    print(f'{total_params:,} total parameters.')
    total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(f'{total_trainable_params:,} training parameters.')

5

取模型参数的数值

parameters = (model.parameters()[0].numpy())

https://blog.csdn.net/HaoZiHuang/article/details/107175271

6

python读取json文件

7

使用Xshell上传文件的时候,记得关闭所要上传的文件,不然会导致上传失败

8

DataFrame删除第一列

df.drop([df.columns[0]], axis=1,inplace=True)

其中,df就是那个DataFrame数据

9

python 把数据写入csv文件

# #1. 创建文件对象
# f = open('poly_minsssN.csv','w',encoding='utf-8')

# #2. 基于文件对象构建 csv写入对象
# csv_writer = csv.writer(f)

# #3. 构建列表头
# csv_writer.writerow(["minsssN","pIC50"])

# #4. 写入csv文件内容
# for i in range(len(pre_poly)):
#     csv_writer.writerow([pre_poly[i],yyy[i]])

# #5. 关闭文件
# f.close()
```
## 10

将数据存储为fasta个数

f = open(‘test.fasta’,‘w’,encoding=‘utf-8’)
f.write(‘>a’)
f.write(“\r\n”);
f.write(“test_1”)
f.write(“\r\n”);

f.write(‘>b’)
f.write(“\r\n”);
f.write(“test_2”)
f.write(“\r\n”);

f.write(‘>c’)
f.write(“\r\n”);
f.write(“test_3”)
f.write(“\r\n”);

f.write(‘>d’)
f.write(“\r\n”);
f.write(“test_4”)
#5. 关闭文件
f.close()

#读取fasta文件

f=open('/home/songyd/Disorder/test.fasta')
ls=[]
for line in f:
        if not line.startswith('>'):    
                ls.append(line.replace('\n',''))    #去掉行尾的换行符真的很重要!  

print(ls)

11

将数据写入fasta文件

f = open('Sequence_fasta.fasta','w',encoding='utf-8')
for i in range(len(Sequence_npy)):
    f.write(">sequence_%d"%(i))
    f.write("\r\n"); 
    f.write(Sequence_npy[i])
    f.write("\r\n"); 

12

python压缩文件

zip -r -q -o shiyanlou.zip /home/shiyanlou/Desktop

其中,shiyanlou.zip为压缩后文件的名字,/home/shiyanlou/Desktop为要压缩的目录

13

XShell查看节点信息
yh

14

vscode如何设置自动换行
Edit->搜索WordWrap->Word Wrap改成on
或者在setting里面搜索

15

python获取两个数的中间值

import math
a = list(range(math.ceil(2), math.floor(5)))
print(a)

16

python查看数据维度

#List
len(data_list)
#DataFrame
data_dataframe.shape
#tensor
data_tensor.shape

17

python将多维数组转成一维数组

import numpy as np

mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print((np.array(mulArrays).flatten()))

18

python三维数组改变其中的第二维

def continuous_to_discrete(continuous,threshold):
    for i in range(len(continuous[0])):
       if  continuous[0][i]>threshold:
           continuous[0][i] = 1
       else:
           continuous[0][i] = 0
    return continuous
##这里continuous是一个三维的数据

19

带有sigmol的损失函数

#BCEWithLogitsLoss = Sigmoid+BCELoss
criterion = nn.BCEWithLogitsLoss()
#####
loss = criterion(pre, label)

20

pytorch保存和加载模型

torch.save(model, Path_of_Model)
model_load = torch.load(Path_of_model)

21

编程规范:将不同模块的代码分开写,data、model等,减少耦合

22

python进行路径拼接

import os
dir1 = "https://files.rcsb.org/download"
dir2 = str(file_)
path = os.path.join(dir1,dir2)

23

通过wget批量下载数据
wget -i txt文件路径
其中txt文件里面存了所有的下载链接,这些链接是之前用循环写入的

24

使用curl下载网页数据

import os
import time
import numpy as np

# print(ID)
for id in ID:
    print(id)
    cmd = f'curl -o /home/songyd/Disorder/Data/data_structure/{id}.res \'https://1d-coordinates.rcsb.org/graphql\' \
  -H \'Connection: keep-alive\' \
  -H \'sec-ch-ua: \"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"\' \
  -H \'accept: */*\' \
  -H \'content-type: application/json\' \
  -H \'sec-ch-ua-mobile: ?0\' \
  -H \'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36\' \
  -H \'sec-ch-ua-platform: \"Windows\"\' \
  -H \'Origin: https://www.rcsb.org\' \
  -H \'Sec-Fetch-Site: same-site\' \
  -H \'Sec-Fetch-Mode: cors\' \
  -H \'Sec-Fetch-Dest: empty\' \
  -H \'Referer: https://www.rcsb.org/\' \
  -H \'Accept-Language: zh-CN,zh;q=0.9\' \
  --data-raw $\'{{\"operationName\":\"QueryAnnotations\",\"variables\":{{\"queryId\":\"{id}\",\"reference\":\"PDB_INSTANCE\",\"sources\":[\"PDB_ENTITY\",\"PDB_INSTANCE\",\"UNIPROT\"]}},\"query\":\"query QueryAnnotations($queryId: String\u0021, $reference: SequenceReference, $sources: [Source], $range: [Int], $filters: [FilterInput]) {{\\n  annotations(\\n    queryId: $queryId\\n    reference: $reference\\n    sources: $sources\\n    range: $range\\n    filters: $filters\\n  ) {{\\n    source\\n    target_id\\n    features {{\\n      type\\n      provenance_source\\n      description\\n      feature_id\\n      name\\n      feature_positions {{\\n        beg_seq_id\\n        end_seq_id\\n        beg_ori_id\\n        end_ori_id\\n        range_id\\n        open_begin\\n        open_end\\n        value\\n        values\\n        __typename\\n      }}\\n      __typename\\n    }}\\n    __typename\\n  }}\\n}}\\n\"}}\' \
  --compressed'
    f=open('http2','w')
    f.write(cmd)
    f.close()
    os.popen('bash http2')
    time.sleep(5)
  

这里ID是下载中需要变化的,下载时只需要运行这个py文件即可

25

python报错:module ‘urllib’ has no attribute ‘urlopen’
原因是我有个py文件叫http.py,这与内部函数重名了,会导致一些意想不到的错误

26

linux查找
find -name 部分名字

27

python从pdb文件里面提取sequence
https://blog.csdn.net/u012325865/article/details/88784510

28

python列表取第n1个数到n2个数之间的内容
LIST[n1:n2]

29

python读取txt数据,并且以str格式读取

pdb_fasta = np.loadtxt(data_path,dtype=np.str)

30

python 提取出小数 提取数字
digital = re.findall(r"\d+.?\d*",txt_1)
其中tex_1是要提取的对象(str)

31

python读取文件

f = open(datapath,'r')
data = f.read()
dataline = f.readlines()  #按行读取

32

python 使用split对字符进行切片

str_1 = str.split("|")

上面是使用 | 来进行分割

33

python 判断是否为子集

if set(str1).issubset(set(['A','U','T','G','C'])):

34

当我们读取字符串的具体某一位是,如果读取的这一个字符串为空,那么后面加[i]会报错,这时可以扎样[i:i+1]

35

python得到列表中某一元素的重复数量

list = ['banana', 'apple', 'apple', 'orange', 'apple', 'melon', 'banana', 'orange']
ban = str(list.count('banana'))

36

python存储字典

np.save(Path,dict)
#读取
Dict = np.load(Path).item()

注意,这里要加item(),不然读出来的是一个numpy.ndarray,加上item()后读出来的是一个dict

37

python 删除文件中的空行

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/3/18 21:41
# @Author  : cunyu
# @Site    : cunyu1943.github.io
# @File    : deleteBlankLines.py
# @Software: PyCharm

"""
读取存在空行的文件,删除其中的空行,并将其保存到新的文件中
"""

with open('old.txt','r',encoding = 'utf-8') as fr,open('new.txt','w',encoding = 'utf-8') as fd:
        for text in fr.readlines():
                if text.split():
                        fd.write(text)
        print('输出成功....')

因为在使用按行读取时(readlines),文件中的空行会导致读取错位

38

python按行读取并去掉换行符\n

with open(Path,'r') as f1:
   train_name = f1.read().splitlines()

39

python去掉字符串里面的空格
str.strip():把头和尾的空格给去掉
https://www.cnblogs.com/zywscq/p/5325604.html

40

判断一个列表中是否有重复元素

a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5]
b = set(a)
for each_b in b:
   count = 0
   for each_a in a:
       if each_b == each_a:
           count += 1
   print(each_b, ": ", count)

41

python的dgl包下载
pip install dgl
用conda好像下不了

42

使用conda删除pytorch包
conda uninstall pytorch

43

vscode debug时配置json文件
在这里插入图片描述

44

Tensor.object类型数据取出其中的数值
data.numpy()

45

python中的文件路径规范
在处理本地文件的时候,使用cmd切换到当前文件的路径,然后如果需要使用路径读取文件的话,在windows上查看“属性”得到的路径并不能直接复制过去,要把左斜杠改成右斜杠,最前面的“盘”的那儿需要加":"
具体的可以参考一下这篇文章:
https://blog.csdn.net/qq_40223983/article/details/95984230?ops_request_misc=&request_id=&biz_id=102&utm_term=python%20shutil%20&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-95984230.pc_search_result_control_group&spm=1018.2226.3001.4187

46

python 遍历字典

for key in d.keys():
    peint(key)

47

python将str转成list
import ast
labels = ast.literal_eval(labels)

48

python去除文件后缀
name.split(“.”)[0]
#name是带后缀的文件名
注意这里如果用name = name.split(“.”)[0]来更新name的话是不行的,这样那么值是没改变的,需要将值重新存在另一个变量里面;name_1 = name.split(“.”)[0]

49

linux查看目录数量
http://www.360doc.com/content/19/1120/09/57605256_874289449.shtml

50

python加载模型报错:RuntimeError: ./Model_20/Fold1_best_model.pkl is a zip archive (did you mean to use torch.jit.load()?)
原因:
训练保存模型时,torch的版本是1.6.0(使用torch.__version__可以查看torch的版本号)

而加载模型时,torch的版本号低于1.6.0

解决方法:换用更高版本的torch就行

51

问题描述:
RuntimeError: CUDA error: out of memory
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

问题解决:
在运行的python文件里面加入

import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1'

52

查看GPU使用情况

1、查看GPU显存使用情况

nvidia-smi

参数说明:
https://www.cnblogs.com/weifeng1463/p/10025813.html

2、top命令
https://www.cnblogs.com/L-shuai/p/15484571.html
3、查看是哪些人在使用GPU
watch -c -n1 gpustat
4、python kill掉进程
https://blog.csdn.net/m0_37673307/article/details/81534690
5、linux下kill掉所有进程
killall python
6、使用服务器的GPU出现问题CUDA:out of memory
查看GPU上的使用情况,发现自己很多进程都没有关上,而且有些进程还很占内存,又由于实验室对每个人应该分配了一部分内存,所以我自己的已经满了,就容易出现out of memory
问题排查与解决:
1、watch -c -n1 gpustat
查看自己的GPU使用情况
2、killall -u user
删掉自己的一些没关的进程
7、用Ctrl+c不用Ctrl+z
Ctrl_c会释放掉占用的显存,一直用Ctrl+z的话会出现out of memory的情况
Ctrl+z是将程序挂起,程序放到后台,程序没有结束

https://blog.csdn.net/qq_39390545/article/details/103205943?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164039596516780269893844%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164039596516780269893844&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-103205943.pc_search_insert_es_download&utm_term=linux+kill%E6%8E%89%E6%89%80%E6%9C%89%E8%BF%9B%E7%A8%8B&spm=1018.2226.3001.4187

53

python plot画图

1、设置画图风格
plt.style.use(‘ggplot’)

查看风格
import matplotlib.pyplot as plt
plt.style.available

54

CPU使用情况

1、%cpu为什么会超过100

https://blog.csdn.net/github_36976474/article/details/115276405?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164039763116780265437831%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164039763116780265437831&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-14-115276405.pc_search_insert_es_download&utm_term=%25cpu%E4%B8%BA%E4%BB%80%E4%B9%88100%E5%A4%9A&spm=1018.2226.3001.4187

55

查看目录大小
du -sh

56

退出python环境
exit()

57

./;…/;/表示什么
./表示当前目录
. ./表示父目录
/表示根目录

58

查看某以用户的进程
ps -aux | grep username
删掉某个进程
kill -s 9 ID
top查看进程
top | grep username

59

遍历字典:

for key in dict:
   Data.append(dict[key])

查找列表的最大值并返回最大值所对应的索引
max_index, max_number = max(enumerate(Data), key=operator.itemgetter(1))
这里key=operator.itemgetter(1)表示的是要比较哪个维度的值,这里是比较第1个维度的值,维度是从0开始的

在字符里面加入变量
return(‘relaxed_model_%d’% (max_index+1)+‘.pdb’)

60

读取某一目录下的所有文件名
import os
name = os.listdir(dirpath)

存储数据成npy格式
np.save(path,data)

61

np.save保存结果会覆盖掉之前相同的数据

62

ls -l |grep “^-” |wc -l
查询的是该目录有多少文件

ls -l |grep “^d” |wc -l
查询的是该目录有多少目录

63

python 创建字典dict

keys = ['a','b','c','d']
values = [1,2,3,4]
dict = dict(zip(keys,values))

64

shutil 复制、移动文件

shutil.copyfile("oldfile","newfile") #oldfile和newfile都只能是文件
shutil.copytree("olddir","newdir") #olddir和newdir都只能是目录,且newdir必须不存在
shutil.move("oldpos","newpos")

65

这种写法会造成索引越界

for i in range(len(y_true)-1):
            if y_true[i] == -1:
                del y_true[i]

因为我们for循环的长度是len(y_true)-1 这是固定了的;但是后面有del y_true,把y_true列表的元素删除了一部分;本来原来的y_true是可以访问不会越界的,但是现在y_true的大小变小了,就导致有部分数组索引是越界的

代码实现将一个列表里面的某一元素去掉,比如说去掉列表里面的所有的-1

index = np.where(y_true!=-1)
y_true = y_true[index]
y_pred = y_pred[index]

66

for循环里使用pass,后面的语句会执行;使用continue后面的语句不执行

67

python报错:*.pth is a zip archive (did you mean to use torch.jit.load()?)

RuntimeError: CUDA error: device-side assert triggered‘

上面的错误是在加载模型的时候遇到的
model.load_state_dict(torch.load(Model_Path + model_name))

根据报错的提示感觉是pytorch版本的问题,然而并不是;我这次的问题的原因是在加载模型的时候,已经训练好的模型和现在我model里面的参数不一致导致的

68

python读取excel文件报错:
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+‘; not supported’)
xlrd.biffh.XLRDError: Excel xlsx file; not supported

解决方法:
在对应的环境,conda install pyexcel-xls

69

现在遇到一个问题,在服务器上跑程序的时候cpu占满了(指标%cpu很高);查其原因,是python在调用sklearn的时候没有控制好并发
最终解决:cpulimit -l 500 -p 67858& 其中500是将cpu利用率限制在500以下,67858是进程PID号,可通过top命令查询,这个命令需要保持终端窗口不关闭,如果想关闭终端窗口,可设置在后台运行 :
nohup cpulimit -l 500 -p 67858&

70

ps -aux|grep name 查找特定进程,其中那么是进程名或者进程名的一部分

71

nohup一般和&命令一起使用,nohup instruction &
nohup是将程序放在后台去跑,而只使用&的话当终端关闭后程序也会中断

72

python 输出日志

import logging
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.INFO)
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
 
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")

73

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat1 in method wrapper_addmm)

在使用模型时加上下面语句就可解决

if torch.cuda.is_available():
            model.cuda()

74

如何将一个已有的conda环境复制到其他地方
https://zhuanlan.zhihu.com/p/87344422

#复制环境
conda env export > environment.yml

#重现环境
conda env create -f environment.yml

changshi

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聆一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值