Skip to content

Instantly share code, notes, and snippets.

@jimexist
Created August 16, 2021 14:43
Show Gist options
  • Save jimexist/8713524dd2728ba600322aefbce7df4e to your computer and use it in GitHub Desktop.
Save jimexist/8713524dd2728ba600322aefbce7df4e to your computer and use it in GitHub Desktop.
Stacked LSTM Output
Display the source blob
Display the rendered blob
Raw
{
"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": [
"batch_size = 4\n",
"input_size = 5\n",
"hidden_size = 8\n",
"seq_length = 31\n",
"num_layers = 3\n",
"rnn = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=True, num_layers=num_layers)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e4c9ff01",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([4, 31, 5])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_tensor = torch.randn(batch_size, seq_length, input_size)\n",
"input_tensor.shape"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d3ad7340",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(torch.Size([4, 31, 8]), torch.Size([3, 4, 8]), torch.Size([3, 4, 8]))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output_tensor, (hidden_state, cell_state) = rnn(input_tensor)\n",
"output_tensor.shape, hidden_state.shape, cell_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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment