自学
在读研究僧-深度学习
这个作者很懒,什么都没留下…
展开
-
pycharm运行run没问题,终端运行no moudle
需添加终端环境将python scripts的路径添加进去原创 2022-04-26 19:56:43 · 1189 阅读 · 0 评论 -
python把文本标签转换为数字标签
def label_num_list(train_path): train=pd.read_csv(train_path) n_classes = len(train) leaves_labels = sorted(list(set(train['label']))) class_to_num = dict(zip(leaves_labels, range(n_classes))) train_label=train['label'] num_train_l.原创 2022-02-07 10:22:57 · 3474 阅读 · 0 评论 -
pytorch本地图片数据集加载成字典
from torchvision.transforms import transformsimport pandas as pdfile_path=os.path.join(r'E:\python存储\leaves\images')Image_list=[]labels_list=[]a={}for i in range(0,18352): image_path=os.path.join(r'E:\python存储\leaves\images','{}.jpg'.format(i).原创 2022-02-06 20:55:30 · 1568 阅读 · 0 评论 -
AlexNet pytorch代码实现
import torchfrom torch import nnfrom d2l import torch as d2lnet=nn.Sequential( nn.Conv2d(1,96,kernel_size=11,stride=4,padding=1),nn.ReLU(), nn.MaxPool2d(kernel_size=3,stride=2), nn.Conv2d(96,128*2,kernel_size=5,padding=2),nn.ReLU(),.原创 2022-02-01 18:52:13 · 1497 阅读 · 0 评论 -
pytorch检查模型各部分输入与输出
import torchfrom torch import nnfrom d2l import torch as d2lclass Reshape(torch.nn.Module): def forward(selfself,x): return x.view(-1,1,28,28)net=torch.nn.Sequential(Reshape(), nn.Conv2d(1,6,kernel_size=5,padding=.原创 2022-01-30 21:01:20 · 3484 阅读 · 1 评论 -
torch池化层基本原理代码
# 池化层的正向传播import torchfrom torch import nnfrom d2l import torch as d2ldef pool2d(X,pool_size,mode='max'): p_h,p_w=pool_size Y=torch.zeros((X.shape[0]-p_h+1,X.sjape[1]-p_w+1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]):.原创 2022-01-29 23:13:26 · 733 阅读 · 1 评论 -
pytorch卷积层代码原理实现
import torchfrom torch import nnfrom d2l import torch as d2ldef corr2d(X,K): # 计算二维互相关运算 h,w=K.shape Y=torch.zeros(X.shape[0]-h+1,X.shape[1]-w+1) for i in range(Y.shape[0]): for j in range(Y.shape[1]): Y[i,j]=(X[i:i.原创 2022-01-28 16:15:15 · 1305 阅读 · 0 评论 -
python爬取携程评论
import requestsimport jsonimport timepagesize=300headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36',}posturl = "https://m.ctrip.com/restapi/soa2/13444/j.原创 2022-01-26 01:24:12 · 2007 阅读 · 8 评论 -
pytorch参数管理
import torchfrom torch import nnnet=nn.Sequential(nn.Linear(4,8),nn.ReLU(),nn.Linear(8,1))x=torch.rand(size=(2,4))y=net(x)print(y)print(net[2].state_dict())print(type(net[2].bias))print(net[2].bias)print(net[2].bias.data)print(net[2].weight.原创 2022-01-24 20:15:16 · 947 阅读 · 1 评论 -
pytorch神经网络基础
import torchfrom torch import nnfrom torch .nn import functional as F# 定义了一个特殊的module# net = nn.Sequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))x=torch.rand(2,20)# print(net(x))# 感知机class MLP(nn.Module): #继承module父类,module有两个非常重要的函数 .原创 2022-01-18 21:33:48 · 1033 阅读 · 0 评论 -
正则匹配表达式,批量加“ ”
1.ctrl+r2.(.*?):(.*)3.'$1':'$2',原创 2022-01-18 09:21:35 · 221 阅读 · 0 评论 -
dropout代码实现原理
def dropout_layer(x,dropout): assert 0 <= dropout<=1 if dropout==1: return torch.zeros_like(x) if dropout==0: return x mask=(torch.randn(x.shape)>dropout).float() return mask*x /(1.0-dropout)原创 2022-01-16 22:23:07 · 818 阅读 · 1 评论 -
多层感知机fashion mnist
import torchfrom matplotlib import pyplot as pltfrom torch import nnfrom d2l import torch as d2lnet=nn.Sequential(nn.Flatten(),nn.Linear(784,256),nn.ReLU(), nn.Linear(256,10))def init_weights(m): if type(m)==nn.Linear: .原创 2022-01-07 22:58:10 · 816 阅读 · 0 评论 -
softmax代码实现,代码原理供参考
import torchfrom IPython import displayfrom d2l import torch as d2limport torch.utils.data.dataloaderfrom matplotlib import pyplot as plt# import pylabbatch_size=64train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size) #训练集和测试集的迭代器num_inpu.原创 2022-01-06 23:23:21 · 1083 阅读 · 0 评论 -
线性回归与梯度下降的代码实现
import randomimport torchfrom d2l import torch as d2limport matplotlib.pyplot as plt# 构造数据集def syn_data(w,b,num_examples): x=torch.normal(0,1,(num_examples,len(w)))#均值为0,方差为一的随机数 y=torch.matmul(x,w)+b y+=torch.normal(0,0.01,y.shape) .原创 2022-01-04 22:27:43 · 443 阅读 · 0 评论 -
knn代码实现
from sklearn.neighbors import KNeighborsClassifierimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport numpy as npvalues=np.array([ [0.2,0.2], [0.3,0.3], [0.4,0.4], [0.8,0.8]])labels=['A','B','C','D']# df=.原创 2022-01-03 22:51:29 · 523 阅读 · 0 评论 -
pytorch-隐式自动求导
import torchx=torch.arange(4.0)print(x)x.requires_grad_(True)print(x.grad)y=2*torch.dot(x,x)print(y)y.backward()print(x.grad)print(x.grad==4*x)# 默认情况下,pytorch会累计梯度,我们需要清除之前的值x.grad.zero_()y=x.sum()y.backward()print(x.grad)x.grad.zero_.原创 2022-01-03 22:34:09 · 651 阅读 · 0 评论