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