How to use the torch.clamp function
This article introduces the knowledge of "how to use the torch.clamp function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Function: limit the values of all elements of a given tensor to a specified range
Examples of code experiments:
(base) PS C:\ Users\ chenxuqi > pythonPython 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]:: Anaconda, Inc. On win32Type "help", "copyright", "credits" or "license" for more information. > import torch > > torch.manual_seed (seed=20200910) > a = torch.randn (4) > > atensor ([0.2824,-0.3715, 0.9088,-1.7601]) > > torch.clamp (a, min=-0.5) Max=0.5) tensor ([0.2824,-0.3715, 0.5000,-0.5000]) > a.clamp (min=-0.5, max=0.5) tensor ([0.2824,-0.3715, 0.5000,-0.5000]) > atensor ([0.2824,-0.3715, 0.9088,-1.7601]) > a.min=-0.5, max=0.5) tensor ([0.2824,-0.3715,0.5000) > > atensor ([0.2824,-0.3715, 0.5000,-0.5000]) > torch.manual_seed (seed=20200910) > > a = torch.randn (4) > atensor ([0.2824,-0.3715, 0.9088,-1.7601]) > torch.clamp (a, min=0.5) tensor ([0.5000, 0.5000, 0.9088,0.5000]) > atensor ([0.2824,-0.3715,0.9088]) -1.7601]) > torch.clamp (a, max=0.5) tensor ([0.2824,-0.3715, 0.5000,-1.7601]) > atensor ([0.2824,-0.3715, 0.9088,-1.7601]) > "how to use torch.clamp function" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!