tag:www.digitalocean.com,2005:/community/tags/pytorch.atomDigitalOcean Community | PyTorch tutorials, questions and resources2022-09-27T15:09:29.964Z<![CDATA[How to remove this error([W NNPACK.cpp:51] Could not initialize NNPACK! Reason: Unsupported hardware.)]]>tag:www.digitalocean.com,2005:/community/questions/how-to-remove-this-error-w-nnpack-cpp-51-could-not-initialize-nnpack-reason-unsupported-hardware2022-09-07T16:36:16.392ZTinyGreenBlueUrchin2022-09-07T16:36:16.392Z<![CDATA[How to use the PyTorch torch.max()]]>tag:www.digitalocean.com,2005:/community/tutorials/pytorch-torch-max2022-08-03T20:14:25.240Z All the maximum elements of the Tensor.
- `max_indices` -> Indices corresponding to the maximum elements.
```python
max_elements, max_indices = torch.max(input_tensor, dim)
```
This will return a Tensor, which has the maximum elements along the dimension `dim`.
Let's now look at some examples.
```python
p = torch.randn([2, 3])
print(p)
# Get the maximum along dim = 0 (axis = 0)
max_elements, max_idxs = torch.max(p, dim=0)
print(max_elements)
print(max_idxs)
```
**Output**
```python
tensor([[-0.0665, 2.7976, 0.9753],
[ 0.0688, -1.0376, 1.4443]])
tensor([0.0688, 2.7976, 1.4443])
tensor([1, 0, 1])
```
As you can see, we find the maximum along the dimension 0 (maximum along columns).
Also, we get the indices corresponding to the elements. For example,`0.0688` has the index `1` along column 0
Similarly, if you want to find the maximum along the rows, use `dim=1`.
```python
# Get the maximum along dim = 1 (axis = 1)
max_elements, max_idxs = torch.max(p, dim=1)
print(max_elements)
print(max_idxs)
```
**Output**
```python
tensor([2.7976, 1.4443])
tensor([1, 2])
```
Indeed, we get the maximum elements along the row, and the corresponding index (along the row).
---
## Using torch.max() for comparison
We can also use `torch.max()` to get the maximum values between two Tensors.
```python
output_tensor = torch.max(a, b)
```
Here, `a` and `b` must have the same dimensions, or must be "broadcastable" Tensors.
Here is a simple example to compare two Tensors having the same dimensions.
```python
p = torch.randn([2, 3])
q = torch.randn([2, 3])
print("p =", p)
print("q =",q)
# Compare elements of p and q and get the maximum
max_elements = torch.max(p, q)
print(max_elements)
```
**Output**
```python
p = tensor([[-0.0665, 2.7976, 0.9753],
[ 0.0688, -1.0376, 1.4443]])
q = tensor([[-0.0678, 0.2042, 0.8254],
[-0.1530, 0.0581, -0.3694]])
tensor([[-0.0665, 2.7976, 0.9753],
[ 0.0688, 0.0581, 1.4443]])
```
Indeed, we get the output tensor having maximum elements between `p` and `q`.
---
## Conclusion
In this article, we learned about using the torch.max() function, to find out the maximum element of a Tensor.
We also used this function to compare two tensors and get the maximum among them.
For similar articles, do go through our content on our [PyTorch tutorials](/community/tutorials/pytorch)! Stay tuned for more!
## References
- [PyTorch Official Documentation](https://pytorch.org/docs/stable/torch.html#torch.max) on torch.max()
---]]>Vijaykrishna Ram2022-08-03T20:14:25.240Z