GithubHelp home page GithubHelp logo

Comments (2)

ylsung avatar ylsung commented on May 30, 2024

You can replace clamp_ by torch.maximum and torch.minimum, which can do the element-wise comparison between tensors. The sample code might be like

transform_train = tv.transforms.Compose([ 
         tv.transforms.RandomCrop(32, padding=4, fill=0, padding_mode='constant'), 
         tv.transforms.RandomHorizontalFlip(), 
         tv.transforms.ToTensor(), 
         tv.transforms.Normalize(), # normalize by mean and std
     ]) 

min_tensor = torch.full((1, 3, 32, 32), 0)
max_tensor = torch.full((1, 3, 32, 32), 255)

min_tensor = transform_train(min_tensor)
max_tensor = transform_train(max_tensor)

def clamp(inputs, max, min):
    max = max.expand(inputs.shape[0], -1, -1, -1) # match the shape of max to be the same as which of inputs
    min = min.expand(inputs.shape[0], -1, -1, -1)
    
    return torch.maximum(torch.minimum(inputs, max), min)

# in fast_gradient_sign_untargeted.py
x = clamp(x, max_tensor, min_tensor)

The code snippet should fit in your needs.

from pytorch-adversarial-training.

chenmc1996 avatar chenmc1996 commented on May 30, 2024

Thanks for your reply. It solves my question.

from pytorch-adversarial-training.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.