# 使用PyTorch實作ResNet並提取指定層輸出之特徵

`(1)提取經過多層的特徵(使用list保存，可進行indexing)def feature_list(self, x):(2)提取經過N層後的特徵def intermediate_forward(self, x, layer_index):(3)提取倒數第二層特徵(常拿來來視覺化t-SNE)def penultimate_forward(self, x):`
`# function to extract the multiple featuresdef feature_list(self, x):    out_list = []    out = F.relu(self.bn1(self.conv1(x)))    out_list.append(out)    out = self.layer1(out)    out_list.append(out)    out = self.layer2(out)    out_list.append(out)    out = self.layer3(out)    out_list.append(out)    out = self.layer4(out)    out_list.append(out)    out = F.avg_pool2d(out, 4)    out = out.view(out.size(0), -1)    y = self.linear(out)    return y, out_list# function to extract a specific featuredef intermediate_forward(self, x, layer_index):    out = F.relu(self.bn1(self.conv1(x)))    if layer_index == 1:        out = self.layer1(out)    elif layer_index == 2:        out = self.layer1(out)        out = self.layer2(out)    elif layer_index == 3:        out = self.layer1(out)        out = self.layer2(out)        out = self.layer3(out)    elif layer_index == 4:        out = self.layer1(out)        out = self.layer2(out)        out = self.layer3(out)        out = self.layer4(out)                   return out# function to extract the penultimate featuresdef penultimate_forward(self, x):    out = F.relu(self.bn1(self.conv1(x)))    out = self.layer1(out)    out = self.layer2(out)    out = self.layer3(out)    penultimate = self.layer4(out)    out = F.avg_pool2d(penultimate, 4)    out = out.view(out.size(0), -1)    y = self.linear(out)    return y, penultimate`

--

--

--

## More from Yanwei Liu

Machine Learning | Deep Learning | https://linktr.ee/yanwei

Love podcasts or audiobooks? Learn on the go with our new app.

## Yanwei Liu

Machine Learning | Deep Learning | https://linktr.ee/yanwei