Pytorch Code積累


2017 Python最新面試題及答案16道題 

15個重要Python面試題 測測你適不適合做Python?

torch.squeeze()

Returns a tensor with all the dimensions of input of size 1 removed.

torch.unsqueeze(input, dim, out=None) → Tensor
Returns a new tensor with a dimension of size one inserted at the specified position.

Python 3:filter()

filter() 函數用於過濾序列,過濾掉不符合條件的元素,返回一個迭代器對象,如果要轉換為列表,可以使用 list() 來轉換。

該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。

 

Gensim

Gensim是一款開源的第三方Python工具包,用於從原始的非結構化的文本中,無監督地學習到文本隱層的主題向量表達。它支持包括TF-IDF,LSA,LDA,和word2vec在內的多種主題模型算法,支持流式訓練,並提供了諸如相似度計算,信息檢索等一些常用任務的API接口。簡單地說,Gensim主要處理文本數據,對文本數據進行建模挖掘。

https://blog.csdn.net/HuangZhang_123/article/details/80326363

traceback

捕獲並打印異常,可以輸出哪個文件哪個函數哪一行報的錯。

@classmethod,@staticmethod,@property

 

 torch.max()

 __call__

在類中實現該方法,一個類實例可以變成一個可調用對象。

代碼出處:https://www.cnblogs.com/superxuezhazha/p/5793536.html

更多特殊函數: https://www.cnblogs.com/xiao987334176/p/8884002.html#autoid-0-1-0

 

IoU(Intersection over Union)的計算

 def IOU(xywh1, xywh2):
    x1, y1, w1, h1 = xywh1
    x2, y2, w2, h2 = xywh2

    dx = min(x1+w1, x2+w2) - max(x1, x2)
    dy = min(y1+h1, y2+h2) - max(y1, y2)
    intersection = dx * dy if (dx >=0 and dy >= 0) else 0.
    
    union = w1 * h1 + w2 * h2 - intersection
    return (intersection / union)

其中(x1,y1),(x2,y2)分別為兩個矩陣左下角的頂點,w,h為寬和高。

 

xml解析

https://www.cnblogs.com/zqchen/articles/3936805.html

 

layer of model

model.children() returns an iterable of high-level layers present in model.

model.named_children() returns an iterable of two-element tuples, where the first element is the name of the high-level layer and the second element is the high-level layer.

inception loss

           if is_inception and phase == 'train':
                        # From https://discuss.pytorch.org/t/how-to-optimize-inception-model-with-auxiliary-classifiers/7958
                        outputs, aux_outputs = model(inputs)
                        loss1 = criterion(outputs, labels)
                        loss2 = criterion(aux_outputs, labels)
                        loss = loss1 + 0.4*loss2

inception_v3 requires the input size to be (299,299), whereas all of the other models expect (224,224).

 

詳解Pytorch中的網絡構造(nn.Module)

https://zhuanlan.zhihu.com/p/53927068

 

affine_grad和grid_sample

https://www.jianshu.com/p/723af68beb2e

 

torch.gather

torch.cat

.view() : reshape a tensor.

 By default, user created Tensors have 'requires_grad = False'

.requires_grad_() 和 .detach()

torch.nn.ReplicationPad2d(padding)

Pads the input tensor using replication of the input boundary.

torch.tensor(np_array):

 

.numpy():Converting a Torch Tensor to a NumPy Array

.from_numpy: Converting NumPy Array to Torch Tensor

tensor_b is a different view (interpretation) of the same data present in the underlying storage

torch.stack: 增加新的維度做堆疊

 

 torch.masked_select :在訓練階段,損失函數通常需要進行mask操作,因為一個batch中句子的長度通常是不一樣的,一個batch中不足長度的位置需要進行填充(pad)補0,最后生成句子計算loss時需要忽略那些原本是pad的位置的值,即只保留mask中值為1位置的值,忽略值為0位置的值

 

Python標准庫(3.x): itertools庫

https://www.cnblogs.com/tp1226/p/8453564.html

Python標准庫(3.x): 內建函數

https://www.cnblogs.com/tp1226/p/8446503.html

 

torch.einsum:

https://www.jqr.com/article/000481

 

Scikit-Learn中TF-IDF權重計算方法主要用到兩個類:CountVectorizerTfidfTransformer

 

select first/last N


select by specific index

index_select


select by mask 會把數據打平












 

select by flatten index



 

 

 

Tensor維度變換

view/reshape: 丟失維度信息

 

squeeze

unsqueeze:插入的index的取值范圍[-a.dim()-1, a.dim()+1)

 

transpose / .t() (only for 2D)

 

permute

expand: broadcasting (推薦)

repeat: memory copied

 

Broadcast

    Expand

 without copying data

   match from last dim

  

Merge or Split

cat: concate的維度可以不一樣,其它維度必須一樣.

 

 stack: create new dim at the dim value

所有的維度必須一樣(e.g. a和b).

 

split: by length

chunk: by num

 

*: element-wise

matmul:matrix 乘法, torch.mm(only for 2D)、torch.matmul、@

>2d tensor matmul

.floor() .ceil()

.round(): 四舍五入

.trunc() .frac()

 

clamp: gradient clipping, (min), (min, max)

 

statistics

norm: 范數用來衡量一個向量的大小

mean sum

prod

max, min,

argmin, argmax: 返回的是索引

dim, keepdim

kthvalue(返回第k小), topk

 

compare

torch.eq

torch.gt

 

Tensor advanced operation

where   (condition, a, b)

gather

 

Image Preprocessing:

Image Resize

Data Augumentation

Normalize

ToTensor

 

Numpy中數組索引為None

 

 

 

 

https://www.kaggle.com/gyani95/380000-lyrics-from-
metrolyrics


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM