Skip to content

Instantly share code, notes, and snippets.

@jimexist
Created August 16, 2021 14:35
Show Gist options
  • Select an option

  • Save jimexist/f2c489fb19cd08a3a0908a0adb4d5efb to your computer and use it in GitHub Desktop.

Select an option

Save jimexist/f2c489fb19cd08a3a0908a0adb4d5efb to your computer and use it in GitHub Desktop.

Revisions

  1. jimexist created this gist Aug 16, 2021.
    130 changes: 130 additions & 0 deletions lstm_cell_output.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "id": "c1333286",
    "metadata": {},
    "outputs": [],
    "source": [
    "import torch\n",
    "from torch import nn"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "id": "4bd8e564",
    "metadata": {},
    "outputs": [],
    "source": [
    "input_size = 5\n",
    "hidden_size = 8\n",
    "cell = nn.LSTMCell(input_size=input_size, hidden_size=hidden_size)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "id": "e4c9ff01",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "tensor([[-1.4978, -1.0567, -1.9804, -0.0280, 0.4982],\n",
    " [ 0.0765, -0.7857, 0.6896, -0.8095, -0.2884],\n",
    " [-0.4736, 1.1645, -0.3312, 0.5085, 1.2523]])"
    ]
    },
    "execution_count": 3,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "input_tensor = torch.randn(3, input_size)\n",
    "input_tensor"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "id": "d3ad7340",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "(tensor([[-0.0045, 0.2381, -0.0528, -0.0542, 0.1259, -0.1155, -0.0484, 0.0413],\n",
    " [-0.0841, 0.0597, 0.0675, -0.1352, -0.0363, -0.0520, 0.0694, -0.0789],\n",
    " [ 0.0259, 0.1760, -0.1397, -0.0058, 0.0424, -0.1639, -0.0007, -0.0492]],\n",
    " grad_fn=<MulBackward0>),\n",
    " tensor([[-0.0072, 0.3069, -0.1259, -0.2533, 0.2088, -0.1996, -0.1047, 0.0992],\n",
    " [-0.1583, 0.0992, 0.1276, -0.2523, -0.0539, -0.0919, 0.1993, -0.1501],\n",
    " [ 0.0423, 0.3415, -0.2315, -0.0178, 0.0849, -0.3342, -0.0022, -0.1224]],\n",
    " grad_fn=<AddBackward0>))"
    ]
    },
    "execution_count": 4,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "output_tensor, state = cell(input_tensor)\n",
    "output_tensor, state"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 5,
    "id": "b7b5e77f",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "(torch.Size([3, 8]), torch.Size([3, 8]))"
    ]
    },
    "execution_count": 5,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "output_tensor.shape, state.shape"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "id": "7bd296a8",
    "metadata": {},
    "outputs": [],
    "source": []
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 3 (ipykernel)",
    "language": "python",
    "name": "python3"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 3
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
    "version": "3.8.11"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 5
    }