## 如何在pytorch中实现对抗性示例？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (186)

``````loss = criterion(outputs+r, labels)
loss.backward()
``````

``````loss = criterion(outputs+r, labels)
loss = loss + c * r.norm(2)
loss.backward()
``````

``````loss = loss + c * r.norm(2)
``````

### 2 个回答

``````#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 18 12:39:16 2016

@author: manojacharya
"""

import torch
import torch.nn as nn
from torchvision import models,transforms
import numpy as np
import os
import matplotlib.pyplot as plt
import torch.nn.functional as F
import json

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])

transform = transforms.Compose(
[transforms.ToTensor(),
normalize])

def imshow(inp, title=None):
"""Imshow for Tensor."""
plt.figure()
inp = inp.data[0]
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
plt.imshow(inp)
plt.axis('off')
if title is not None:
plt.title(title)

with open('imagenet.json') as f:

# In[model]:

model = models.vgg16(pretrained=True)
for param in model.parameters():

def predict(img):
pred_raw = model(img)
pred = F.softmax(pred_raw)
_,indices = torch.topk(pred,k=1)
for ind in indices.data.numpy().ravel():
print ("%.2f%% , class: %s , %s" %(100*pred.data[0][ind],str(ind),imagenet_labels[ind]))

# In[image ]:

img =  imresize(peppers,(224,224))
imgtensor = transform(img)
optimizer = torch.optim.Adam([imgvard], lr = 0.1)
loss_fn =  nn.CrossEntropyLoss()

label  =  torch.LongTensor(1)
#classify the object as this label
label[0] = 80
label = Variable(label)
eps = 2/255.0

#%%
Nepochs = 50
print ("Starting ...........",predict(imgvar))

for epoch in range(Nepochs):
pred_raw = model(imgvard)
loss  =  loss_fn(pred_raw,label)

diff = imgvard.data - imgvar.data
imgvard.data = torch.clamp(torch.abs(diff),max=eps) + imgvar.data

loss.backward()
optimizer.step()

print('epoch: {}/{}, loss: {}'.format(
epoch + 1,Nepochs, loss.data[0]))
predict(imgvard)
print('Finished Training')

#%%
imshow(imgvard)

#%%
plt.figure()
diffimg = diff[0].numpy()
diffimg = diffimg.transpose((1,2,0))
plt.imshow(diffimg)
``````