[PYTHON] Try to specify the axis with PyTorch's Softmax function

Introduction

The subject, the memo when I looked it up.

environment

How to specify the axis

When creating an instance of the nn.Softmax class, you can specify the axis with the argument dim.

let's try it

This time, let's take the following array as an example.

input = torch.randn(2, 3)
print(input)
tensor([[-0.2562, -1.2630, -0.1973],
        [ 0.8285, -0.9981,  0.3171]])

When dim is not specified

m = nn.Softmax()
print(m(input))

I get angry like this.

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.

When dim = 0 is specified

m = nn.Softmax(dim=0)
print(m(input))

It applies Softmax for each column.

tensor([[0.2526, 0.4342, 0.3742],
        [0.7474, 0.5658, 0.6258]])

Just in case, if you aggregate by column, the total of each column will be 1.

torch.sum(m(input), axis=0)
tensor([1., 1., 1.])

When dim = 1 is specified

m = nn.Softmax(dim=1)
print(m(input))

It applies Softmax line by line.

tensor([[0.4122, 0.1506, 0.4372],
        [0.5680, 0.0914, 0.3406]])

Just in case, if you aggregate by row, the total of each row will be 1.

torch.sum(m(input), axis=1)
tensor([1.0000, 1.0000])

Recommended Posts

Try to specify the axis with PyTorch's Softmax function
How to specify the NIC to scan with amazon-dash
Try to solve the man-machine chart with Python
How to try the friends-of-friends algorithm with pyfof
I tried to learn the sin function with chainer
Try to solve the programming challenge book with python3
[Introduction to Python] How to iterate with the range function?
Try to solve the internship assignment problem with Python
[Python] How to specify the download location with youtube-dl
[Neo4J] ④ Try to handle the graph structure with Cypher
Specify MinGW as the compiler to use with Python
Try function optimization with Optuna
How to increase the axis
Try to get the function list of Python> os package
Try to play with the uprobe that supports Systemtap directly
Try to automate the operation of network devices with Python
Try to decipher the garbled attachment file name with Python
[Introduction to Python] How to get data with the listdir function
Try to extract the features of the sensor data with CNN
Tokyo Corona: Try to make a simple prediction from open data with the exponential function curve_fit
How to use the zip function
[Introduction to Python] How to split a character string with the split function
Try to operate Facebook with Python
Try to solve the N Queens problem with SA of PyQUBO
Try to profile with ONNX Runtime
[Python] Explains how to use the format function with an example
Cython to try in the shortest
Try blurring the image with opencv2
Try to output audio with M5STACK
The fastest way to try EfficientNet
Try to solve the shortest path with Python + NetworkX + social data
From "drawing" to "writing" the configuration diagram: Try drawing the AWS configuration diagram with Diagrams
The easiest way to try PyQtGraph
When you want to adjust the axis scale interval with APLpy
Try to solve the function minimization problem using particle swarm optimization
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
[Python] Explains how to use the range function with a concrete example
If you want to include awsebcli with CircleCI, specify the python version
Try to image the elevation data of the Geographical Survey Institute with Python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Try to react only the carbon at the end of the chain with SMARTS
[Introduction to Python] How to write a character string with the format function
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to separate the background and moving object of the video with OpenCV
Try to reproduce color film with Python
Try logging in to qiita with Python
Compute the partition function with the sum-product algorithm
Try to face the integration by parts
Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
Try converting to tidy data with pandas
Quickly try to visualize datasets with pandas
Python amateurs try to summarize the list ①
First YDK to try with Cisco IOS-XE
Try rewriting the file with the less command
Match the colorbar to the figure with matplotlib
Try to generate an image with aliasing
The road to compiling to Python 3 with Thrift
Function to extract the maximum and minimum values ​​in a slice with Go
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
A simple workaround for bots to try to post tweets with the same content