论文写完了也不让实习,糟糕的环境就不用吐槽了,感觉被生活蹂躏的我身心疲惫,能做的只有忍下去,每天一个人这么将就的活着。没事我还能忍,既然如此,暑假的我还是踏踏实实学习,只希望将来能不辜负自己受的这些苦难,让我爱的人都能幸福的活着,加油。
#1.神将网络天气预测,线性回归 import numpy as np import pandas as pd import matplotlib.pyplot as plt import torch import torch.optim as optim import warnings warnings.filterwarnings('ignore') %matplotlib inline features=pd.read_csv('temps.csv') features.head() print('数据维度:',features.shape) #处理时间数据 import datetime years=features['year'] months=features['month'] days=featuures['day'] dates=[str(int(year))+'-'+str(int(month))+'-'+str(int(day)) for year,month,day in zip(years,month,days)] dates=[datetime.datetime.strptime(date,'%Y=%m-%d') for date in dates] #画图展示 plt.style.use('fivethirtyeight')#指定默认风格 fig,((ax1,ax2,ax3,ax4))=plt.subplot(nrows=2,ncols=2,figsize=(10,10)) fig.autofmt_xdate(rotation=45)#设置布局 #画出4个子图 ax1.plot(dates,features['actual']) ax1.set_xlabel('');ax1.set_ylabel('Temperature');ax1.set_title('Max Temp') ax2.plot(dates,features['temp_1']) ax2.set_xlabel('');ax2.set_ylabel('Temperature');ax2.set_title('Previous Max Temp') ax3.plot(dates,features['temp_2']) ax3.set_xlabel('Date');ax3.set_ylabel('Temperature');ax3.set_title('Two Days Prior Max Temp') ax4.plot(dates,features['friend']) ax4.set_xlabel('Date');ax4.set_ylabel('Temperature');ax4.set_title('Friend Estimate') plt.tight_layout(pad=2) #独热编码 features=pd.get_dummies(features)#自动将字符串形式的换成独热编码 labels=np.array(features['actual'])#标签 features=features.drop('actual',axis=1)#特征 features=np.array(features)#datafrom>>>>ndarray feature_list=list(features.columns)#名字 from sklearn import preprocessing input_features=preprocessing.StandardScalar().fit_transform(features)#特征数据标准化 #构建模型 x=torch.tensor(input_features,dtype=float) y=torch.tensor(labels,dtype=float) weights=torch.randn((14,128),dtype=float,requires_grad=True) biases=torch.randn(128,dtype=float,requires_grad=True) weights2=torch.randn((128,1),dtype=float,requires_grad=True) biases2=torch.randn(1,dtype=float,requires_grad=True) learning_rate=0.01 losses=[] for i in range(1000): hidden=x.mm(weights)+biases#计算隐层 hidden=torch.relu(hidden)#加入激活层 prediction=hidden.mm(weights)+biases2#预测结果 loss=torch.mean((predictions-y)**2)#计算损失 losses.append(loss.data.numpy()) if i%100==0: print('loss',loss) loss.backward()#反向传播 #更新参数 weights.data.add_(-learning_rate*weights.grad.data) biases.data.add_(-learning_rate*biases.grad.data) weights2.data.add_(-learning_rate*weights.grad.data) biases2.data.add_(-learning_rate*biases.grad.data) #每次迭代完都记得清空 weights.grad.data.zero_() biases.grad.data.zero_() weights2.grad.data.zero_() biases2.grad.data.zero_() #2.更简单的方式构建网络 input_size=input_features.shape[1] hidden_size=128 output_size=1 batch_size=16 my_nn=torch.nn.Sequential(torch.nn.Linear(input_size,hidden_size), torch.nn.Sigmoid(), torch.nn.Linear(hidden_size,out_size))#定义网络结构 cost=torch.nn.MSELoss(reduction='mean')#均方误差 optimizer=torch.optim.Adam(my_nn.parameters(),lr=0.001)#对网络参数进行优化的优化器设置 #训练网络 losses=[] for i in range(1000): batch_loss=[] for start in range(0.len(input_features),batch_size): end=start+batch_size if star+batch_size<len(inpu_features) else len(input_featurs) xx=torch.tensor(input_features[start:end],dtype=torch.flaot,requires_grad=True) yy=torch.tensor(labels[start:end],dtype=torch.float,requires_grad=True) prediction=my_nn(xx) loss=cost(prediction,yy) optimizer.zero_grad() loss.backward(retain_graph=True)#表示此代码可以重复执行 optimizer.step()#更新操作 batch_loss.append(loss,data.numpy()) if i%100==0: losses.append(np.mean(batch_loss)) print(i,np.mean(batch_loss))
#2.Mnist多分类任务 import matplotlib.pyplot as plt %matplotlib inline from pathlib import Path import requests DATA_PATH=Path('D:DeepLearningpytorch') PATH=DATA_PATH/'mnist' PATH.mkdir(parents=True,exist_ok=True) URL='http://deeplearning.net/data/mnist/' FILENAME='mnist.pkl.gz' if not (PATH/FILENAME).exists(): content=requests.get(URL+FILENAME).content (PATH/FILENAME).open('wb').write(content) import pickle import gzip with gzip.open((PATH/FILENAME).as_posix(),'rb')as f: ((x_train,y_train),(x_valid,y_valid),_)=pickle.load(f,encoding='latin-1') import numpy as np plt.imshow(x_train[0].reshape((28,28)),cmap='gray')#查看第一张灰度图 print(x_train.shape) import torch x_train,y_train,x_valid,y_valid=map(torch.tensor,(x_train,y_train,x_valid,y_valid)) n,c=x_train.shape#行50000列784 x_train,x_train.shape,y_train.min(),y_train.max()#torch.Size([50000, 784]), tensor(0), tensor(9)) import torch.nn.functional as F loss_func=F.cross_entropy#交叉熵损失函数 def model(xb): return xb.mm(weights)+bias bs=64 xb=x_train[0:bs] yb=y_train[0:bs] weights=torch.randn([784,10],dtype=torch.float,requires_grad=True) bs=64 bias=torch.zeros(10,requires_grad=True) print(loss_func(model(xb),yb)) #使用module from torch import nn class Mnist_NN(nn.Module): def __init__(self): super().__init__() self.hidden1=nn.Linear(784,128) self.hidden2=nn.Linear(128,256) self.out=nn.Linear(256,10) def forward(self,x): x=F.relu(self.hidden1(x)) x=F.relu(self.hidden2(x)) x=self.out(x) return x net=Mnist_NN() print(net) #打印网络层的名字和权重值与偏置值 for name,parameter in net.named_parameters(): print(name,parameter,parameter.size()) #使用TensorDataset和DataLoader来简化 from torch.utils.data import TensorDataset from torch.utils.data import DataLoader train_ds=TensorDataset(x_train,y_train) train_dl=DataLoader(train_ds,batch_size=bs,shuffle=True) valid_ds=TensorDataset(x_valid,y_valid) valid_dl=DataLoader(valid_ds,batch_size=bs*2) def get_data(train_ds,valid_ds,bs): return ( DataLoader(train_ds,batch_size=bs,shuffle=True), DataLoader(valid_ds,batch_size=bs*2) ) def loss_batch(model,loss_func,xb,yb,opt=None): loss=loss_func(model(xb),yb) if opt is not None: loss.backward() opt.step() opt.zero_grad() return loss.item(),len(xb) #一般在训练模型时加上model.train(),这样会正常使用Batch Normalization 和 Dropout #测试时一般选择model.eval(),这样不会使用Batch Normalization 和Dropout import numpy as np def fit(steps,model,loss_func,opt,train_dl,valid_dl): for step in range(steps): model.train() for xb,yb in train_dl: loss_batch(model,loss_func,xb,yb,opt) model.eval() with torch.no_grad(): losses,nums=zip(*[loss_batch(model,loss_func,xb,yb) for xb,yb in valid_dl]) val_loss=np.sum(np.multiply(losses,nums))/np.sum(nums) print('当前step:'+str(step),'验证集损失:'+str(val_loss)) from torch import optim def get_model(): model=Mnist_NN() return model,optim.SGD(model.parameters(),lr=0.001) #开始训练 train_dl,valid_dl=get_data(train_ds,valid_ds,bs) model,opt=get_model() fit(25,model,loss_func,opt,train_dl,valid_dl)
#3.Mnist卷积神经网络分类 import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torchvision import datasets,transforms import matplotlib.pyplot as plt import numpy as np %matplotlib inline #定义超参数 input_size=28#图像尺寸 num_classes=10 num_epochs=3 batch_size=64 train_dataset=datasets.MNIST(root='./',train=True,transform=transforms.ToTensor(),download=True)#训练集数据 test_dataset=datasets.MNIST(root='./',train=False,transform=transforms.ToTensor()) #构建batch数据 train_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True) test_loader=torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=True) class CNN(nn.Module): def __init__(self): super(CNN,self).__init__() self.conv1=nn.Sequential(nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5,stride=1,padding=2),nn.ReLU(),nn.MaxPool2d(kernel_size=2)) self.conv2=nn.Sequential(nn.Conv2d(16,32,5,1,2),nn.ReLU(),nn.MaxPool2d(2)) self.out=nn.Linear(32*7*7,10) def forward(self,x): x=self.conv1(x) x=self.conv2(x) x=x.view(x.size(0),-1)#flatten操作,结果为(batch_size,32*7*7),因为下一层是全连接层,所以需要把他们变成一维的 output=self.out(x) return output #评估标准:准确率 def accuracy(predictions,labels): pred=torch.max(predictions.data,1)[1] rights=pred.eq(labels.data.view_as(pred)).sum() return rights,len(labels) net=CNN()#实例化 criterion=nn.CrossEntropyLoss() optimizer=optim.Adam(net.parameters(),lr=0.001) #开始循环训练 for epoch in range(num_epochs): train_rights=[] for batch_idx,(data,target) in enumerate(train_loader): net.train() output=net(data) loss=criterion(output,target) optimizer.zero_grad() loss.backward() optimizer.step() right=accuracy(output,target) if batch_idx%100==0: net.eval() val_rights=[] for (data,target) in test_loader: output=net(data) right=accuracy(output,target) val_rights.append(right) #准确率计算 train_r=(sum([tup[0] for tup in train_rights]), sum([tup[1] for tup in train_rights])) val_r=(sum([tup[0] for tup in val_rights]),sum(tup[1] for tup in val_rights)) print('当前epoch:{}[{}/{} ({:.0f}%)] 损失:{:.6f} 训练集准确率:{:.2f}% 测试集准确率:{:.2f}%'.format( epoch,batch_idx*batch_size,len(train_loader.dataset), 100.*batch_idx/len(train_loader), loss.data, 100. *train_r[0].numpy()/train_r[1], 100. *val_r[0].numpy()/val_r[1] ))
import os import matplotlib.pyplot as plt %matplotlib inline import numpy as np import torch from torch import nn import torch.optim as optim import torchvision from torchvision import transforms ,models,datasets import imageio import time import warnings import random import sys import copy import json from PIL import Image data_dir='./flower_data' train_dir=data_dir+'/train' valid_dir=data_dir+'/valid' #data_transforms中指定了所有图像预处理操作 #ImageFolder假设所有的文件按文件夹保存好,每个文件下面存储同一类别的图片,文件夹的名字维分类的名字 data_transforms={ 'train':transforms.Compose([transforms.RandomRotation(45),#随机旋转,-45~45度之间随机 transforms.CenterCrop(224),#从中心开始裁剪,裁剪出边长224大小的中间区域 transforms.RandomHorizontalFlip(p=0.5),#随即水平翻转,选择一个概率 transforms.RandomVerticalFlip(p=0.5),#随机垂直翻转 transforms.ColorJitter(brightness=0.2,contrast=0.1,saturation=0.1,hue=0.1),#亮度,对比度,饱和度,色相 transforms.RandomGrayscale(p=0.025),#以某概率值转换成灰度率,3通道就R=G=B transforms.ToTensor(), transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])#均值,标准差 ]), 'valid':transorms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize[[0.485,0.456,0.406],[0.229,0.224,0.225]]]) } batch_size=8 image_datasets={x:datasets.ImageFolder(os.path.join(data_dir,x),data_transforms[x]) for x in ['train','valid']} dataloaders={x:torch.utils.data.DataLoader(image_datasets[x],batch_size,shuffle=True) for x in ['train','valid']} dataset_sizes={x:len(iamge_datasets[x]) for x in ['train','valid']} class_names=image_datasets['train'].classes