本文主要介紹pytorc檔(pytorch中文文檔pdf),下面一起看看pytorc檔(pytorch中文文檔pdf)相關(guān)資訊。
torch是_ tensoras _ tensors,從arraylike創(chuàng)建張量,共享內(nèi)存,默認(rèn)從_numpy開始梯度計(jì)算,從ndarray復(fù)制tensor zeros(size)zeros _ likeones(size)ones _ like隨機(jī)生成張量:
伯努利正態(tài)(mean,std)泊松\(out _ i \ simpoisson(input _ i)\)rand(size)u(0,1) rand _ like randn (0,1) randn _ like randperm。
和ndarray一致,可以互相轉(zhuǎn)化,但是張量可以在gpu上運(yùn)行。
通常使用不同數(shù)據(jù)類型的張量:
火炬。飛人,火炬。doubletensortorch。短張量,火炬。int張量,火炬。長(zhǎng)張量默認(rèn)為浮點(diǎn)張量。
建筑火炬。張量(數(shù)據(jù),數(shù)據(jù)類型,設(shè)備,要求_梯度,pin _存儲(chǔ)器)
參數(shù)列表:
數(shù)據(jù)列表、ruple或ndarray或tensor type(torch . dtype)張量的類型。如果留空,則默認(rèn)數(shù)據(jù)類型,選項(xiàng):torch.float,torch.int,其他設(shè)備的存儲(chǔ)位置(torch.device)張量,如果留空,則在同一個(gè)位置,否則默認(rèn)在cpu上。常見的值有torch . device( ;cuda : 0 ;).(作者 s注:該參數(shù)用于在cpu上訓(xùn)練模型)requires_grad(bool)用于親筆簽名,默認(rèn)值為false。如果需要,可以調(diào)用requires_grad_修改只對(duì)cpu類張量有效的值pin_memory(bool),將張量賦給pinned內(nèi)存?;蛘唔?yè)鎖內(nèi)存,則默認(rèn)值為false。開啟后張量的讀取速度增加,需要大內(nèi)存的電腦可以使用這個(gè)參數(shù)。對(duì)于張量的數(shù)值,使用torch.as_tensor避免數(shù)據(jù)復(fù)制。
tensor.item僅對(duì)具有一個(gè)元素的張量有效。用括號(hào)索引張量得到的結(jié)果還是張量,需要用item轉(zhuǎn)換成python數(shù)字。
x =火炬。張量([1.0])x . item1.0 data dataset . utils . data . dataset中的抽象類需要重載__len__和__getitem__。
映射樣式的數(shù)據(jù)集映射樣式的數(shù)據(jù)集實(shí)現(xiàn)__getitem__和__len__協(xié)議,并表示從(可能是非整數(shù))索引/鍵到數(shù)據(jù)樣本的映射。
例如,當(dāng)使用數(shù)據(jù)集[idx]訪問這樣的數(shù)據(jù)集時(shí),可以從磁盤上的文件夾中讀取第idx個(gè)圖像及其相應(yīng)的標(biāo)簽。
iterable-style dataset iterable-style dataset是iterabledataset的子類實(shí)例,它實(shí)現(xiàn)__iter__協(xié)議,并表示數(shù)據(jù)樣本上的iterable。這種類型的數(shù)據(jù)集特別適用于隨機(jī)讀取開銷很大甚至不可行的情況,以及批處理大小取決于獲取的數(shù)據(jù)的情況。
例如,當(dāng)這樣的數(shù)據(jù)集被稱為iter(dataset)時(shí),它可以返回從數(shù)據(jù)庫(kù)中讀取的數(shù)據(jù)流e、遠(yuǎn)程服務(wù)器,甚至實(shí)時(shí)生成的日志。
數(shù)據(jù)加載對(duì)于迭代數(shù)據(jù),數(shù)據(jù)加載順序由用戶自定義的__iter__決定。
對(duì)于映射數(shù)據(jù),請(qǐng)使用torch.utils.data.sampler加載數(shù)據(jù)。
注:torc件不明確,需要結(jié)合具體項(xiàng)目研究。
dataloaderdataloader(dataset,batch_size=1,shuffle=false,sampler=none,batch_sampler=none,num_workers=0,collate_fn=none,pin_memory=false,drop _ last = false,timeout = 0,worker _ init _ fn = none,*,prefetch _ factor = 2,persistent _ workers = false)dataset(dataset)–dataset batch _ size(int,可選)–數(shù)據(jù)批次,每個(gè)批次中有多少次shuffle (bool,可選)–是否中斷collate_fn(可調(diào)用,可選)–讀取時(shí)使用可以填寫模塊型號(hào)。模板:
導(dǎo)入torch.nn作為nni導(dǎo)入torch.nn.functional作為f class mod: d: super。__init__ self.conv1 = nn。conv2d(1,20,5) self.conv2 = nn。conv2d(20,20,5) d: x = f . relu(self。conv1 (x))返回f. relu (self。conv2 (x))焊炬。optim有很多優(yōu)化算法。您需要首先創(chuàng)建一個(gè)優(yōu)化器對(duì)象,然后它將根據(jù)計(jì)算的梯度更新參數(shù)。
optimizer = optim。sgd(model.parameters,lr=0.01,動(dòng)量= 0.9)optimizer = optim . adam([var 1,var 2],lr = 0.0001)支持算法:adadelta,adagrad,adam,adamw,sparseadam,adamax,asgd,lbfgs,nadam,radam,rmsprop,rprop,sgd。
模型加載和保存保存的對(duì)象是模型類。
torch.save(型號(hào) 。/model . pth ;)load _ model = torch . load( ; )標(biāo)簽:
數(shù)據(jù)加載
了解更多pytorc檔(pytorch中文文檔pdf)相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。