Row 3950
Content Data
This page contains data entry 3950 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.
Hi everyone, I'm trying to implement a LSTM in PyTorch but I have some doubts that I haven't been able to resolve by searching online:
First of all I saw from the documentation that the size parameters are `input_size` and `hidden_size` but I cannot understand how to control the size when I have more layers. Let's say I have 3 layers:
\[`input_size`\] `lstm1` \[`hidden_size`\] --> `lstm2` \[what about this size?\] --> `lstm3` \[what about this size?\]
Secondly I tried to use `nn.Sequential` but it doesn't work I think because the LSTM outputs a tensor and a tuple containing the memory and it cannot be passed to another layer. I managed to do this and it works but I wanted to know if there was another method, possibly using `nn.Sequential` . Here is my code:
import torch import torch.nn as nn class Model(nn.Module): def init(self): super().init() self.model = nn.ModuleDict({ 'lstm': nn.LSTM(input_size=300, hidden_size=200, num_layers=2), 'hidden_linear': nn.Linear(in_features=8 * 10 * 200, out_features=50), 'relu': nn.ReLU(inplace=True), 'output_linear': nn.Linear(in_features=50, out_features=3)}) def forward(self, x): out, memory = self.model['lstm'](x) out = out.view(-1) out = self.model['hidden_linear'](out) out = self.model["relu"](out) out = self.model["output_linear"](out) out = nn.functional.softmax(out, dim=0) return out input_tensor = torch.randn(8, 10, 300) model = Model() output = model(input_tensor)
Thank you for your help
| Field | Value |
|---|---|
| text | Hi everyone, I'm trying to implement a LSTM in PyTorch but I have some doubts that I haven't been able to resolve by searching online: First of all I saw from the documentation that the size parameters are `input_size` and `hidden_size` but I cannot understand how to control the size when I have more layers. Let's say I have 3 layers: \[`input_size`\] `lstm1` \[`hidden_size`\] --> `lstm2` \[what about this size?\] --> `lstm3` \[what about this size?\] Secondly I tried to use `nn.Sequential` … |
| label | r/pytorch |
| dataType | post |
| communityName | r/pytorch |
| datetime | 2024-03-30 |
| username_encoded | Z0FBQUFBQm5LakwxUzNWYTI4d09vTS1NTVhoZ0pkQkZ5dWprUHBFa0dQc0xET0RNWEZSdnBPaTRKOGY0RUhUNkowT1FyRG5ybndiYWVaMXFBMkhtTENpeVdtaFBMR0VzYmRnUktId1dpamJpTHlWTi1ZaVR1ZUE9 |
| url_encoded | Z0FBQUFBQm5Lak9GNVZXR2kzdjFtUlpQTEU0MlRheloxbWV4S2p5LWg3MmRydnNQM0xEb0M5TG04bVM0MXI2Q3hCOGpqcXZjbGlQVDk0WDhmT3FoTEJLOHFqRW1aamtkSVljWHN1TXk3alpzSm83NG5vYWJ3RnVZSGViQXJCd1lNNG5ZN0E5dTk4MEFWREI5dXZYT09pcmhOMDM4SzdoSEhnOVRiYnlFeFRJWWhXRzVIVlV4bjEwPQ== |
Raw Record
{
"text": "Hi everyone, I'm trying to implement a LSTM in PyTorch but I have some doubts that I haven't been able to resolve by searching online:\n\nFirst of all I saw from the documentation that the size parameters are `input_size` and `hidden_size` but I cannot understand how to control the size when I have more layers. Let's say I have 3 layers:\n\n\\[`input_size`\\] `lstm1` \\[`hidden_size`\\] --> `lstm2` \\[what about this size?\\] --> `lstm3` \\[what about this size?\\]\n\nSecondly I tried to use `nn.Sequential` but it doesn't work I think because the LSTM outputs a tensor and a tuple containing the memory and it cannot be passed to another layer. I managed to do this and it works but I wanted to know if there was another method, possibly using `nn.Sequential` . Here is my code:\n\n import torch\n import torch.nn as nn\n \n \n class Model(nn.Module):\n def init(self):\n super().init()\n self.model = nn.ModuleDict({\n 'lstm': nn.LSTM(input_size=300, hidden_size=200, num_layers=2),\n 'hidden_linear': nn.Linear(in_features=8 * 10 * 200, out_features=50),\n 'relu': nn.ReLU(inplace=True),\n 'output_linear': nn.Linear(in_features=50, out_features=3)})\n \n def forward(self, x):\n out, memory = self.model['lstm'](x)\n \n out = out.view(-1)\n \n out = self.model['hidden_linear'](out)\n \n out = self.model[\"relu\"](out)\n \n out = self.model[\"output_linear\"](out)\n \n out = nn.functional.softmax(out, dim=0)\n \n return out\n \n \n input_tensor = torch.randn(8, 10, 300)\n model = Model()\n output = model(input_tensor)\n\nThank you for your help",
"label": "r/pytorch",
"dataType": "post",
"communityName": "r/pytorch",
"datetime": "2024-03-30",
"username_encoded": "Z0FBQUFBQm5LakwxUzNWYTI4d09vTS1NTVhoZ0pkQkZ5dWprUHBFa0dQc0xET0RNWEZSdnBPaTRKOGY0RUhUNkowT1FyRG5ybndiYWVaMXFBMkhtTENpeVdtaFBMR0VzYmRnUktId1dpamJpTHlWTi1ZaVR1ZUE9",
"url_encoded": "Z0FBQUFBQm5Lak9GNVZXR2kzdjFtUlpQTEU0MlRheloxbWV4S2p5LWg3MmRydnNQM0xEb0M5TG04bVM0MXI2Q3hCOGpqcXZjbGlQVDk0WDhmT3FoTEJLOHFqRW1aamtkSVljWHN1TXk3alpzSm83NG5vYWJ3RnVZSGViQXJCd1lNNG5ZN0E5dTk4MEFWREI5dXZYT09pcmhOMDM4SzdoSEhnOVRiYnlFeFRJWWhXRzVIVlV4bjEwPQ=="
}
Entry Information
- Entry ID: 3950
- Repository: Axioma AXP
- Dataset: arrmlet/reddit_dataset_36
- Total Entries: 100,000