Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column-dependent bounds in torch.clamp

I would like to do something similar to np.clip on PyTorch tensors on a 2D array. More specifically, I would like to clip each column in a specific range of value (column-dependent). For example, in numpy, you could do:

x = np.array([-1,10,3])
low = np.array([0,0,1])
high = np.array([2,5,4])
clipped_x = np.clip(x, low, high)

clipped_x == np.array([0,5,3]) # True

I found torch.clamp, but unfortunately it does not support multidimensional bounds (only one scalar value for the entire tensor). Is there a "neat" way to extend that function to my case?

Thanks!

like image 266
Ben Avatar asked Feb 17 '19 21:02

Ben


People also ask

What is .clamp in PyTorch?

clamp(..., min, max) sets all elements in input to the value of max . input (Tensor) – the input tensor. min (Number or Tensor, optional) – lower-bound of the range to be clamped to. max (Number or Tensor, optional) – upper-bound of the range to be clamped to.

What is torch Tensor?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

Is torch part of PyTorch?

PyTorch is based on Torch, an early framework for deep learning. PyTorch just takes the deep learning potential of Torch and ports it into the Python environment. Python has become one of the most popular programming languages for web-related applications, alongside other modern programming languages like R.

What is a torch Python?

The torch package contains data structures for multi-dimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.


1 Answers

Not as neat as np.clip, but you can use torch.max and torch.min:

In [1]: x
Out[1]:
tensor([[0.9752, 0.5587, 0.0972],
        [0.9534, 0.2731, 0.6953]])

Setting the lower and upper bound per column

l = torch.tensor([[0.2, 0.3, 0.]])
u = torch.tensor([[0.8, 1., 0.65]])

Note that the lower bound l and upper bound u are 1-by-3 tensors (2D with singleton dimension). We need these dimensions for l and u to be broadcastable to the shape of x.
Now we can clip using min and max:

clipped_x = torch.max(torch.min(x, u), l)

Resulting with

tensor([[0.8000, 0.5587, 0.0972],
        [0.8000, 0.3000, 0.6500]])
like image 76
Shai Avatar answered Sep 30 '22 05:09

Shai