教程如何從tensorflow轉入pytorch_第1頁
教程如何從tensorflow轉入pytorch_第2頁
教程如何從tensorflow轉入pytorch_第3頁
教程如何從tensorflow轉入pytorch_第4頁
教程如何從tensorflow轉入pytorch_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

選自作者:IllarionPyTorch時,沒幾天就放棄了TensorFlowPyTorch的核心要領但是隨后不PyTorch論壇,你可以在其中詢問相關的問題,并很快得到回復:/API,所以有時候你可以把它用作是NumPy的替代品PyTorch的開發(fā)者們這么做的原因是希望這種框架可以完全獲得importtorchimportnumpyasnpnumpy_tensor=np.random.randn(10,#convertnumpyarraytopytorcharraypytorch_tensor=torch.Tensor(numpy_tensor)#oranotherwaypytorch_tensor=#converttorchtensortonumpyrepresentation#ifwewanttousetensoronGPUprovideanothertypedtype=torch.cuda.FloatTensorgpu_tensor=torch.randn(10,20).type(dtype)#orjustcall`cuda()`methodgpu_tensor=pytorch_tensor.cuda()#callbacktotheCPUcpu_tensor=#definepytorchtensorsx=torch.randn(10,20)y=torch.ones(20,5)#`@`meanmatrixmultiplicationfrompython3.5,PEP-0465res=x@y#gettheres.shape#torch.Size([10,的importfromtorch.autogradimport#defineanx_tensor=torch.randn(10,20)y_tensor=torch.randn(10,5)x=Variable(x_tensor,requires_grad=False)y=Variable(y_tensor,requires_grad=False)#definesomeweightsw=Variable(torch.randn(20,5),#getvariabletensorprint(type(w.data))#torch.FloatTensor#getvariablegradientprint(w.grad)#loss=torch.mean((y-x@w)**#calculatethegradientsprint(w.grad)#somegradients#manuallyapplygradientsw.data-=0.01*w.grad.data#manuallyzerogradientsafterupdateimportfromtorch.autogradimportVariableimporttorch.nn.functionalasFx=Variable(torch.randn(10,20),requires_grad=False)y=Variable(torch.randn(10,3),requires_grad=False)#definesomeweightsw1=Variable(torch.randn(20,5),requires_grad=True)w2=Variable(torch.randn(5,3),requires_grad=True)learning_rate=loss_fn=optimizer=torch.optim.SGD([w1,w2],lr=learning_rate)forstepinrange(5):pred=F.sigmoid(x@w1)pred=F.sigmoid(pred@w2)loss=loss_fn(pred,y)#manuallyzeroallpreviousgradients#calculatenewgradients#applynewgradientsPyTorchTensorFlow的另一個主要區(qū)別在于其不同的計算圖表現形式TensorFlow使用靜態(tài)圖,這意味著我們是先定動態(tài)圖會在你希望調試代碼,或定義一些條件語句時顯現出自己的優(yōu)勢debugger一樣!importtensorflowasfirst_counter=tf.constant(0)second_counter=tf.constant(10)some_value=tf.Variable(15)#conditionshouldhandlealldefcond(first_counter,second_counter,*args):returnfirst_counter<second_counterdefbody(first_counter,second_counter,some_value):first_counter=tf.add(first_counter,2)second_counter=tf.add(second_counter,1)returnfirst_counter,second_counter,some_valuec1,c2,val=cond,body,[first_counter,second_counter,withtf.Session()assess:counter_1_res,counter_2_res=sess.run([c1,c2])importfirst_counter=torch.Tensor([0])second_counter=torch.Tensor([10])some_value=torch.Tensor(15)while(first_counter<second_counter)[0]:first_counter+=2second_counter+=Keras的即開即用的層構造函數:以保存內部狀態(tài),例如包含可學習參數的變量nn包還定義了一組在訓練神經網絡時常用的損失函數fromcollectionsimportOrderedDictimporttorch.nnasnn#ExampleofusingSequentialmodel=nn.Sequential(nn.Conv2d(1,20,5),nn.Conv2d(20,64,5),)#ExampleofusingSequentialwithOrderedDictmodel=nn.Sequential(OrderedDict([('conv1',nn.Conv2d(1,20,('relu1',('conv2',nn.Conv2d(20,64,('relu2',output=fromtorchimportnnclassModel(nn.Module): init(self):super().init()self.feature_extractor=nn.Conv2d(3,12,kernel_size=3,padding=1,stride=1),nn.Conv2d(12,24,kernel_size=3,padding=1,stride=1),)self.second_extractor=24,36,kernel_size=3,padding=1,stride=1)defforward(self,x):x=self.feature_extractor(x)x=self.second_extractor(x)#notethatwemaycallsamelayertwiceormodex=self.second_extractor(x)returnimportclassMyFunction(torch.autograd.Function):defforward(ctx,input):output=torch.sign(input)returnoutputdefbackward(ctx,#savedtensors-tupleoftensors,soweneedgetfirstinput,=ctx.saved_variablesgrad_output[input.ge(1)]=grad_output[input.le(-1)]=0returngrad_output#x=torch.randn(10,20)y=MyFunction.apply(x)#ormy_func=MyFunction.applyy=my_func(x)#andifwewanttouseinsidenn.ModuleclassMyFunctionModule(torch.nn.Module):defforward(self,returnward(input)的形式,并以input,_=self.saved_tensors的方式接入import###tensorx_cpu=torch.randn(10,20)w_cpu=torch.randn(20,10)#directtransfertotheGPUx_gpu=x_cpu.cuda()w_gpu=w_cpu.cuda()result_gpu=x_gpu@w_gpu#getbackfromGPUtoCPUresult_cpu=###modelexamplemodel=model.cuda()#trainstepinputs=Variable(inputs.cuda())outputs=model(inputs)#getbackfromGPUtoCPUoutputs=outputs.cpu()class init(self,model,use_cuda=False,self.use_cuda=use_cudaself.gpu_idx=gpu_idxself.model=self.to_gpu(model)defto_gpu(self,tensor):ifself.use_cuda:returntensor.cuda(self.gpu_idx)returndeffrom_gpu(self,tensor):ifself.use_cuda:returntensor.cpu()returndeftrain(self,inputs=self.to_gpu(inputs)outputs=self.model(inputs)outputs=self.from_gpu(outputs)改變張量的權重權重可以用調用torch.nn.init包中的多種方法初始化為直接訪問張量的屬性這個決定或許并不直接了importfromtorch.autogradimport#newwaywith`init`modulew=torch.Tensor(3,5)#workforVariablesalsow2=Variable(w)#oldstyleddirectaccesstotensorsdataattribute#exampleforsomemoduledefweights_init(m):classname=m.class.ifclassname.find('Conv')!=-1:m.weight.data.normal_(0.0,0.02)elifclassname.find('BatchNorm')!=-1:m.weight.data.normal_(1.0,0.02)#forloopapproachwithdirectaccessclassMyModel(nn.Module): initforminifisinstance(m,n=m.kernel_size[0]*m.kernel_size[1]*m.out_channelsm.weight.data.normal_(0,math.sqrt(2./n))elifisinstance(m,nn.BatchNorm2d):elifisinstance(m,nn.Linear):PyTorchflag:requires_gradvolatile第一個可以禁用當前層的梯度,但子節(jié)點仍然可以計算第二個可以importfromtorch.autogradimport#requires#Ifthere’sasingleinputtoanoperationthatrequiresgradient,#itsoutputwillalsorequiregradient.x=Variable(torch.randn(5,y=Variable(torch.randn(5,z=Variable(torch.randn(5,5),requires_grad=True)a=x+ya.requires_grad#Falseb=a+zb.requires_grad#True#Volatilediffersfromrequires_gradinhowtheflagpropagates.#Ifthere’sevenasinglevolatileinputtoanoperation,#itsoutputisalsogoingtobex=Variable(torch.randn(5,5),requires_grad=True)y=Variable(torch.randn(5,5),volatile=True)a=x+ya.requires_grad#False允許/禁止批規(guī)范層和dropout如果你想要做的話,讓CPU和GPU的隨機算子不同也是可以的#schedulerfromtorch.optimimportoptimizer=torch.optim.SGD(model.parameters(),scheduler=lr_scheduler.StepLR(optimizer,step_size=30,gamma=0.1)forepochinrange(100):#Trainflagcanbeupdatedwith#todisabledropoutandbatchnormlearning#executetrainstep#runinference#CPUseed#GPUseedfromcollectionsimportOrderedDictimporttorch.nnasnnmodelnn.Sequential(OrderedDict([('conv1',nn.Conv2d(1,20,5)),('relu1',('conv2',nn.Conv2d(20,64,('relu2',#Sequential (conv1):Conv2d(1,20,kernel_size=(5,5),stride=(1,1)) (relu1):ReLU() (conv2):Conv2d(20,64,kernel_size=(5,5),stride=(1,1)) (relu2):ReLU()##save/loadonlythemodelparameters(preferedsolution)torch.save(model.state_dict(),save_path)#savewholemodeltorch.save(model,save_path)model=torch.load(save_path)文本記錄Python了,你也可以試試一些第三方庫:logger:/oval-group/loggerCrayon:/torrvision/crayontensorboard_logger:pytorch:/lanpa/tensorboard-pytorchVisdom:importimporttorchvisionasclassImagesDataset(torch.utils.data.Dataset): init(self,df,transform=None,self.df=dfself.transform=transformself.loader=loader getitem(self,index):row=self.df.iloc[index]target=path=img=ifself.transformisnotNone:img=self.transform(img)returnimg, lenn,_=self.df.shapereturnn#whattransformationsshouldbedonewithourimagesdata_transforms=tv.transforms.Compose([tv.transforms.RandomCrop((64,64),padding=4),train_df=pd.read_csv('path/to/some.csv')#initializeourdatasetatf

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論