TorchOperator
TorchOperator
¶
Bases: Operator
Class to handle torch operations with a unified API
Source code in oodeel/utils/torch_operator.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
CrossEntropyLoss(reduction='mean')
staticmethod
¶
Cross Entropy Loss from logits
Source code in oodeel/utils/torch_operator.py
101 102 103 104 105 106 107 108 |
|
abs(tensor)
staticmethod
¶
compute absolute value
Source code in oodeel/utils/torch_operator.py
257 258 259 260 |
|
argmax(tensor, dim=None)
staticmethod
¶
Argmax function
Source code in oodeel/utils/torch_operator.py
66 67 68 69 |
|
cat(tensors, dim=0)
staticmethod
¶
Concatenate tensors in a given dimension
Source code in oodeel/utils/torch_operator.py
160 161 162 163 |
|
convert_from_tensorflow(tensor)
staticmethod
¶
Convert a tensorflow tensor into a torch tensor
Used when using a pytorch model on a dataset loaded from tensorflow datasets
Source code in oodeel/utils/torch_operator.py
120 121 122 123 124 125 126 |
|
convert_to_numpy(tensor)
staticmethod
¶
Convert tensor into a np.ndarray
Source code in oodeel/utils/torch_operator.py
128 129 130 131 132 133 |
|
diag(tensor)
staticmethod
¶
Diagonal function: return the diagonal of a 2D tensor
Source code in oodeel/utils/torch_operator.py
194 195 196 197 |
|
eigh(tensor)
staticmethod
¶
Computes the eigen decomposition of a self-adjoint matrix.
Source code in oodeel/utils/torch_operator.py
214 215 216 217 218 |
|
einsum(equation, *tensors)
staticmethod
¶
Computes the einsum between tensors following equation
Source code in oodeel/utils/torch_operator.py
236 237 238 239 |
|
equal(tensor, other)
staticmethod
¶
Computes element-wise equality
Source code in oodeel/utils/torch_operator.py
204 205 206 207 |
|
flatten(tensor)
staticmethod
¶
Flatten function
Source code in oodeel/utils/torch_operator.py
173 174 175 176 177 |
|
from_numpy(arr)
¶
Convert a NumPy array to a tensor
Source code in oodeel/utils/torch_operator.py
179 180 181 182 |
|
gradient(func, inputs, *args, **kwargs)
staticmethod
¶
Compute gradients for a batch of samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
Function used for computing gradient. Must be built with torch differentiable operations only, and return a scalar. |
required |
inputs |
Tensor
|
Input tensor wrt which the gradients are computed |
required |
*args |
Additional Args for func. |
()
|
|
**kwargs |
Additional Kwargs for func. |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Gradients computed, with the same shape as the inputs. |
Source code in oodeel/utils/torch_operator.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
matmul(tensor_1, tensor_2)
staticmethod
¶
Matmul operation
Source code in oodeel/utils/torch_operator.py
115 116 117 118 |
|
max(tensor, dim=None, keepdim=False)
staticmethod
¶
Max function
Source code in oodeel/utils/torch_operator.py
71 72 73 74 75 76 77 78 79 |
|
mean(tensor, dim=None)
staticmethod
¶
Mean function
Source code in oodeel/utils/torch_operator.py
165 166 167 168 169 170 171 |
|
min(tensor, dim=None, keepdim=False)
staticmethod
¶
Min function
Source code in oodeel/utils/torch_operator.py
81 82 83 84 85 86 87 88 89 |
|
norm(tensor, dim=None)
staticmethod
¶
Tensor Norm
Source code in oodeel/utils/torch_operator.py
110 111 112 113 |
|
one_hot(tensor, num_classes)
staticmethod
¶
One hot function
Source code in oodeel/utils/torch_operator.py
91 92 93 94 |
|
permute(tensor, dims)
staticmethod
¶
Transpose function for tensor of rank 2
Source code in oodeel/utils/torch_operator.py
189 190 191 192 |
|
pinv(tensor)
staticmethod
¶
Computes the pseudoinverse (Moore-Penrose inverse) of a matrix.
Source code in oodeel/utils/torch_operator.py
209 210 211 212 |
|
quantile(tensor, q, dim=None)
staticmethod
¶
Computes the quantile of a tensor's components. q in (0,1)
Source code in oodeel/utils/torch_operator.py
220 221 222 223 224 225 226 227 228 229 |
|
relu(tensor)
staticmethod
¶
Apply relu to a tensor
Source code in oodeel/utils/torch_operator.py
231 232 233 234 |
|
reshape(tensor, shape)
staticmethod
¶
Reshape function
Source code in oodeel/utils/torch_operator.py
199 200 201 202 |
|
sign(tensor)
staticmethod
¶
Sign function
Source code in oodeel/utils/torch_operator.py
96 97 98 99 |
|
softmax(tensor)
staticmethod
¶
Softmax function along the last dimension
Source code in oodeel/utils/torch_operator.py
61 62 63 64 |
|
stack(tensors, dim=0)
staticmethod
¶
Stack tensors along a new dimension
Source code in oodeel/utils/torch_operator.py
155 156 157 158 |
|
sum(tensor, dim=None)
staticmethod
¶
sum along dim
Source code in oodeel/utils/torch_operator.py
247 248 249 250 |
|
t(tensor)
staticmethod
¶
Transpose function for tensor of rank 2
Source code in oodeel/utils/torch_operator.py
184 185 186 187 |
|
tril(tensor, diagonal=0)
staticmethod
¶
Set the upper triangle of the matrix formed by the last two dimensions of
Source code in oodeel/utils/torch_operator.py
241 242 243 244 245 |
|
unsqueeze(tensor, dim)
staticmethod
¶
unsqueeze along dim
Source code in oodeel/utils/torch_operator.py
252 253 254 255 |
|
where(condition, input, other)
staticmethod
¶
Applies where function , to condition
Source code in oodeel/utils/torch_operator.py
262 263 264 265 266 267 268 269 |
|
sanitize_input(tensor_arg_func)
¶
ensures the decorated function receives a torch.Tensor
Source code in oodeel/utils/torch_operator.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|