feat: add Jupyter notebook demonstrating Softmax and linear layer usage in PyTorch

This commit is contained in:
fada
2025-06-25 17:38:41 +08:00
parent 5caa98ea09
commit c2f1303514

108
17.ipynb Normal file
View File

@ -0,0 +1,108 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-06-25T06:29:17.879978Z",
"start_time": "2025-06-25T06:29:16.365981Z"
}
},
"source": [
"import torch\n",
"import torch.nn as nn\n",
"\n",
"y = torch.randn(2)\n",
"print(y)\n",
"\n",
"m = nn.Softmax(dim=0)\n",
"out = m(y)\n",
"print(out)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([-0.9296, -0.1961])\n",
"tensor([0.3244, 0.6756])\n"
]
}
],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-25T06:31:29.348989Z",
"start_time": "2025-06-25T06:31:29.328174Z"
}
},
"cell_type": "code",
"source": [
"x = torch.randint(0, 255, (1, 128 * 128), dtype=torch.float32)\n",
"fc = nn.Linear(128 * 128, 2)\n",
"y = fc(x)\n",
"print(y)"
],
"id": "7a0673da7cb52123",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[59.5330, 43.6760]], grad_fn=<AddmmBackward0>)\n"
]
}
],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-25T06:31:56.045399Z",
"start_time": "2025-06-25T06:31:56.041215Z"
}
},
"cell_type": "code",
"source": [
"output = nn.Softmax(dim=1)(y)\n",
"print(output)"
],
"id": "a7135e5b93ea1669",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[1.0000e+00, 1.2984e-07]], grad_fn=<SoftmaxBackward0>)\n"
]
}
],
"execution_count": 3
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}