Anurag1734/cuda-error-resolution-analysis
07
1[2 {3 "post_stream": {4 "posts": [5 {6 "id": 401927,7 "name": "安石 张",8 "username": "1152768760",9 "avatar_template": "/user_avatar/discuss.pytorch.org/1152768760/{size}/60472_2.png",10 "created_at": "2023-05-14T18:23:05.143Z",11 "cooked": "<p>Hi. I am trying to load a model with:</p>\n<pre><code class=\"lang-auto\"># Save the model checkpoint\ntorch.save(model.state_dict(), 'model.ckpt')\n\n# 创建一个与原始模型具有相同结构的模型实例\nmodel = model(inputs)\n\n# 加载模型的状态字典\nmodel.load_state_dict(torch.load('model.ckpt'))\n\n</code></pre>\n<p>But it says:</p>\n<pre><code class=\"lang-auto\">AttributeError: 'Tensor' object has no attribute 'load_state_dict'\n</code></pre>",12 "post_number": 1,13 "post_type": 1,14 "posts_count": 2,15 "updated_at": "2023-05-14T18:23:33.946Z",16 "reply_count": 0,17 "reply_to_post_number": null,18 "quote_count": 0,19 "incoming_link_count": 162,20 "reads": 4,21 "readers_count": 3,22 "score": 805.8,23 "yours": false,24 "topic_id": 179895,25 "topic_slug": "attributeerror-tensor-object-has-no-attribute-load-state-dict",26 "display_username": "安石 张",27 "primary_group_name": null,28 "flair_name": null,29 "flair_url": null,30 "flair_bg_color": null,31 "flair_color": null,32 "flair_group_id": null,33 "badges_granted": [],34 "version": 2,35 "can_edit": false,36 "can_delete": false,37 "can_recover": false,38 "can_see_hidden_post": false,39 "can_wiki": false,40 "read": true,41 "user_title": null,42 "bookmarked": false,43 "actions_summary": [],44 "moderator": false,45 "admin": false,46 "staff": false,47 "user_id": 66154,48 "hidden": false,49 "trust_level": 1,50 "deleted_at": null,51 "user_deleted": false,52 "edit_reason": null,53 "can_view_edit_history": true,54 "wiki": false,55 "post_url": "/t/attributeerror-tensor-object-has-no-attribute-load-state-dict/179895/1",56 "can_accept_answer": false,57 "can_unaccept_answer": false,58 "accepted_answer": false,59 "topic_accepted_answer": null,60 "can_vote": false61 },62 {63 "id": 401929,64 "name": "",65 "username": "ptrblck",66 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",67 "created_at": "2023-05-14T18:24:53.871Z",68 "cooked": "<p><code>model(inputs)</code> will return a tensor in common use cases.<br>\nSince you are re-assigning the <code>model</code> variable to it:</p>\n<pre><code class=\"lang-python\">model = model(inputs)\n</code></pre>\n<p><code>model</code> will now be a tensor instead of an <code>nn.Module</code> instance and will thus fail in the next line of code.<br>\nAssign the output to another variable and it should work, e.g.:</p>\n<pre><code class=\"lang-python\">output = model(input)\n</code></pre>",69 "post_number": 2,70 "post_type": 1,71 "posts_count": 2,72 "updated_at": "2023-05-14T18:24:53.871Z",73 "reply_count": 0,74 "reply_to_post_number": null,75 "quote_count": 0,76 "incoming_link_count": 2,77 "reads": 4,78 "readers_count": 3,79 "score": 10.8,80 "yours": false,81 "topic_id": 179895,82 "topic_slug": "attributeerror-tensor-object-has-no-attribute-load-state-dict",83 "display_username": "",84 "primary_group_name": null,85 "flair_name": null,86 "flair_url": null,87 "flair_bg_color": null,88 "flair_color": null,89 "flair_group_id": null,90 "badges_granted": [],91 "version": 1,92 "can_edit": false,93 "can_delete": false,94 "can_recover": false,95 "can_see_hidden_post": false,96 "can_wiki": false,97 "read": true,98 "user_title": "",99 "bookmarked": false,100 "actions_summary": [],101 "moderator": true,102 "admin": true,103 "staff": true,104 "user_id": 3534,105 "hidden": false,106 "trust_level": 2,107 "deleted_at": null,108 "user_deleted": false,109 "edit_reason": null,110 "can_view_edit_history": true,111 "wiki": false,112 "post_url": "/t/attributeerror-tensor-object-has-no-attribute-load-state-dict/179895/2",113 "can_accept_answer": false,114 "can_unaccept_answer": false,115 "accepted_answer": false,116 "topic_accepted_answer": null117 }118 ],119 "stream": [120 401927,121 401929122 ]123 },124 "timeline_lookup": [125 [126 1,127 895128 ]129 ],130 "suggested_topics": [131 {132 "fancy_title": "[No CUDA GPUs are available] CUDA 12.2 w/ PyTorch 2.1.0+cu121",133 "id": 212823,134 "title": "[No CUDA GPUs are available] CUDA 12.2 w/ PyTorch 2.1.0+cu121",135 "slug": "no-cuda-gpus-are-available-cuda-12-2-w-pytorch-2-1-0-cu121",136 "posts_count": 2,137 "reply_count": 0,138 "highest_post_number": 2,139 "image_url": null,140 "created_at": "2024-11-11T22:49:55.086Z",141 "last_posted_at": "2024-11-12T13:27:34.905Z",142 "bumped": true,143 "bumped_at": "2024-11-12T13:27:34.905Z",144 "archetype": "regular",145 "unseen": false,146 "pinned": false,147 "unpinned": null,148 "visible": true,149 "closed": false,150 "archived": false,151 "bookmarked": null,152 "liked": null,153 "tags_descriptions": {},154 "like_count": 0,155 "views": 461,156 "category_id": 36,157 "featured_link": null,158 "has_accepted_answer": false,159 "posters": [160 {161 "extras": null,162 "description": "Original Poster",163 "user": {164 "id": 80831,165 "username": "James_Jang",166 "name": "James Jang",167 "avatar_template": "/user_avatar/discuss.pytorch.org/james_jang/{size}/73925_2.png",168 "trust_level": 1169 }170 },171 {172 "extras": "latest",173 "description": "Most Recent Poster",174 "user": {175 "id": 3534,176 "username": "ptrblck",177 "name": "",178 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",179 "admin": true,180 "moderator": true,181 "trust_level": 2182 }183 }184 ]185 },186 {187 "fancy_title": "Expected input batch_size (220) to match target batch_size (63)",188 "id": 216688,189 "title": "Expected input batch_size (220) to match target batch_size (63)",190 "slug": "expected-input-batch-size-220-to-match-target-batch-size-63",191 "posts_count": 3,192 "reply_count": 1,193 "highest_post_number": 3,194 "image_url": null,195 "created_at": "2025-02-14T18:46:48.450Z",196 "last_posted_at": "2025-02-15T12:16:42.116Z",197 "bumped": true,198 "bumped_at": "2025-02-15T12:16:42.116Z",199 "archetype": "regular",200 "unseen": false,201 "pinned": false,202 "unpinned": null,203 "visible": true,204 "closed": false,205 "archived": false,206 "bookmarked": null,207 "liked": null,208 "tags_descriptions": {},209 "like_count": 0,210 "views": 64,211 "category_id": 36,212 "featured_link": null,213 "has_accepted_answer": false,214 "posters": [215 {216 "extras": "latest",217 "description": "Original Poster, Most Recent Poster",218 "user": {219 "id": 82694,220 "username": "Hana_AlRasheed",221 "name": "Hana AlRasheed",222 "avatar_template": "/user_avatar/discuss.pytorch.org/hana_alrasheed/{size}/75664_2.png",223 "trust_level": 0224 }225 },226 {227 "extras": null,228 "description": "Frequent Poster",229 "user": {230 "id": 3534,231 "username": "ptrblck",232 "name": "",233 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",234 "admin": true,235 "moderator": true,236 "trust_level": 2237 }238 }239 ]240 },241 {242 "fancy_title": "Relevance score in ViT model",243 "id": 215470,244 "title": "Relevance score in ViT model",245 "slug": "relevance-score-in-vit-model",246 "posts_count": 1,247 "reply_count": 0,248 "highest_post_number": 1,249 "image_url": null,250 "created_at": "2025-01-16T11:46:40.044Z",251 "last_posted_at": "2025-01-16T11:46:40.090Z",252 "bumped": true,253 "bumped_at": "2025-01-16T11:46:40.090Z",254 "archetype": "regular",255 "unseen": false,256 "pinned": false,257 "unpinned": null,258 "visible": true,259 "closed": false,260 "archived": false,261 "bookmarked": null,262 "liked": null,263 "tags_descriptions": {},264 "like_count": 0,265 "views": 104,266 "category_id": 36,267 "featured_link": null,268 "has_accepted_answer": false,269 "posters": [270 {271 "extras": "latest single",272 "description": "Original Poster, Most Recent Poster",273 "user": {274 "id": 72886,275 "username": "Kallel_Mariem",276 "name": "Kallel Mariem",277 "avatar_template": "/user_avatar/discuss.pytorch.org/kallel_mariem/{size}/64790_2.png",278 "trust_level": 1279 }280 }281 ]282 },283 {284 "fancy_title": "train code with pyTorch",285 "id": 215559,286 "title": "train code with pyTorch",287 "slug": "train-code-with-pytorch",288 "posts_count": 3,289 "reply_count": 2,290 "highest_post_number": 4,291 "image_url": null,292 "created_at": "2025-01-18T14:30:46.124Z",293 "last_posted_at": "2025-01-18T22:47:04.671Z",294 "bumped": true,295 "bumped_at": "2025-01-18T22:47:04.671Z",296 "archetype": "regular",297 "unseen": false,298 "pinned": false,299 "unpinned": null,300 "visible": true,301 "closed": false,302 "archived": false,303 "bookmarked": null,304 "liked": null,305 "tags_descriptions": {},306 "like_count": 0,307 "views": 290,308 "category_id": 36,309 "featured_link": null,310 "has_accepted_answer": false,311 "posters": [312 {313 "extras": null,314 "description": "Original Poster",315 "user": {316 "id": 82152,317 "username": "stev",318 "name": null,319 "avatar_template": "/letter_avatar_proxy/v4/letter/s/a6a055/{size}.png",320 "trust_level": 0321 }322 },323 {324 "extras": "latest",325 "description": "Most Recent Poster",326 "user": {327 "id": 3534,328 "username": "ptrblck",329 "name": "",330 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",331 "admin": true,332 "moderator": true,333 "trust_level": 2334 }335 }336 ]337 },338 {339 "fancy_title": "Cuda 12.4 vs Pytorch ___ compatibility (latest)",340 "id": 217171,341 "title": "Cuda 12.4 vs Pytorch ___ compatibility (latest)",342 "slug": "cuda-12-4-vs-pytorch-compatibility-latest",343 "posts_count": 2,344 "reply_count": 0,345 "highest_post_number": 2,346 "image_url": null,347 "created_at": "2025-02-26T07:42:15.291Z",348 "last_posted_at": "2025-02-26T13:32:53.656Z",349 "bumped": true,350 "bumped_at": "2025-02-26T13:32:53.656Z",351 "archetype": "regular",352 "unseen": false,353 "pinned": false,354 "unpinned": null,355 "visible": true,356 "closed": false,357 "archived": false,358 "bookmarked": null,359 "liked": null,360 "tags_descriptions": {},361 "like_count": 0,362 "views": 518,363 "category_id": 36,364 "featured_link": null,365 "has_accepted_answer": false,366 "posters": [367 {368 "extras": null,369 "description": "Original Poster",370 "user": {371 "id": 82794,372 "username": "Mohan_Krishna",373 "name": "Mohan Krishna",374 "avatar_template": "/user_avatar/discuss.pytorch.org/mohan_krishna/{size}/75758_2.png",375 "trust_level": 1376 }377 },378 {379 "extras": "latest",380 "description": "Most Recent Poster",381 "user": {382 "id": 3534,383 "username": "ptrblck",384 "name": "",385 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",386 "admin": true,387 "moderator": true,388 "trust_level": 2389 }390 }391 ]392 }393 ],394 "tags_descriptions": {},395 "fancy_title": "AttributeError: ‘Tensor’ object has no attribute ‘load_state_dict’",396 "id": 179895,397 "title": "AttributeError: 'Tensor' object has no attribute 'load_state_dict'",398 "posts_count": 2,399 "created_at": "2023-05-14T18:23:05.060Z",400 "views": 867,401 "reply_count": 0,402 "like_count": 0,403 "last_posted_at": "2023-05-14T18:24:53.871Z",404 "visible": true,405 "closed": false,406 "archived": false,407 "has_summary": false,408 "archetype": "regular",409 "slug": "attributeerror-tensor-object-has-no-attribute-load-state-dict",410 "category_id": 36,411 "word_count": 102,412 "deleted_at": null,413 "user_id": 66154,414 "featured_link": null,415 "pinned_globally": false,416 "pinned_at": null,417 "pinned_until": null,418 "image_url": null,419 "slow_mode_seconds": 0,420 "draft": null,421 "draft_key": "topic_179895",422 "draft_sequence": null,423 "unpinned": null,424 "pinned": false,425 "current_post_number": 1,426 "highest_post_number": 2,427 "deleted_by": null,428 "actions_summary": [429 {430 "id": 4,431 "count": 0,432 "hidden": false,433 "can_act": false434 },435 {436 "id": 8,437 "count": 0,438 "hidden": false,439 "can_act": false440 },441 {442 "id": 10,443 "count": 0,444 "hidden": false,445 "can_act": false446 },447 {448 "id": 7,449 "count": 0,450 "hidden": false,451 "can_act": false452 }453 ],454 "chunk_size": 20,455 "bookmarked": false,456 "topic_timer": null,457 "message_bus_last_id": 0,458 "participant_count": 2,459 "show_read_indicator": false,460 "thumbnails": null,461 "slow_mode_enabled_until": null,462 "can_vote": false,463 "vote_count": 0,464 "user_voted": false,465 "discourse_zendesk_plugin_zendesk_id": null,466 "discourse_zendesk_plugin_zendesk_url": "https://your-url.zendesk.com/agent/tickets/",467 "details": {468 "can_edit": false,469 "notification_level": 1,470 "participants": [471 {472 "id": 3534,473 "username": "ptrblck",474 "name": "",475 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",476 "post_count": 1,477 "primary_group_name": null,478 "flair_name": null,479 "flair_url": null,480 "flair_color": null,481 "flair_bg_color": null,482 "flair_group_id": null,483 "admin": true,484 "moderator": true,485 "trust_level": 2486 },487 {488 "id": 66154,489 "username": "1152768760",490 "name": "安石 张",491 "avatar_template": "/user_avatar/discuss.pytorch.org/1152768760/{size}/60472_2.png",492 "post_count": 1,493 "primary_group_name": null,494 "flair_name": null,495 "flair_url": null,496 "flair_color": null,497 "flair_bg_color": null,498 "flair_group_id": null,499 "trust_level": 1500 }501 ],502 "created_by": {503 "id": 66154,504 "username": "1152768760",505 "name": "安石 张",506 "avatar_template": "/user_avatar/discuss.pytorch.org/1152768760/{size}/60472_2.png"507 },508 "last_poster": {509 "id": 3534,510 "username": "ptrblck",511 "name": "",512 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png"513 }514 },515 "bookmarks": []516 },517 {518 "post_stream": {519 "posts": [520 {521 "id": 401906,522 "name": "mahmoud ramadan muhammed",523 "username": "mahmoud_ramadan_muha",524 "avatar_template": "/user_avatar/discuss.pytorch.org/mahmoud_ramadan_muha/{size}/60492_2.png",525 "created_at": "2023-05-14T11:34:09.348Z",526 "cooked": "<p>Traceback (most recent call last):<br>\nFile “E:\\predict_ddi-master\\predict_ddi-master\\src\\run.py”, line 42, in <br>\nmodel.fit(dataloader, i)<br>\nFile “E:\\predict_ddi-master\\predict_ddi-master\\src\\model.py”, line 94, in fit<br>\nDNN_loss.backward()<br>\nFile “C:\\Users\\mahmoud\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\torch_tensor.py”, line 488, in backward<br>\ntorch.autograd.backward(<br>\nFile “C:\\Users\\mahmoud\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\torch\\autograd_<em>init</em>_.py”, line 197, in backward<br>\nVariable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass<br>\nRuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [200]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</p>\n<p>run.py code</p>\n<pre><code class=\"lang-auto\">import torch\nimport torch.nn as nn\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import RepeatedStratifiedKFold\nimport json\n\nfrom model import build_model\nfrom utils import index_data, convert_tensor\n\nSS_mat = pd.read_pickle('../data/structural_similarity_matrix.pkl')\nTS_mat = pd.read_pickle('../data/target_similarity_matrix.pkl')\nGS_mat = pd.read_pickle('../data/GO_similarity_matrix.pkl')\n\nmlb, _, idx2label, drugPair2effectIdx = index_data()\npd.to_pickle(mlb, '../data/mlb.pkl')\npd.to_pickle(idx2label, '../data/idx2label.pkl')\n \nx_idx = []\ny_idx = []\nfor k, v in drugPair2effectIdx.items():\n x_idx.append(k)\n y_idx.append(v)\nx_idx, y_idx = np.array(x_idx), np.array(y_idx)\n\nwith open('../data/hyperparameter.json') as fp:\n hparam = json.load(fp)\n\nkf = RepeatedStratifiedKFold(n_splits=hparam['n_splits'], n_repeats=hparam['n_repeats'], random_state=2020)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nfor i, (train_idx, test_idx) in enumerate(kf.split(x_idx, y_idx)): \n x_train = x_idx[train_idx]\n y_train = y_idx[train_idx] \n \n SS, TS, GS, y = convert_tensor(x_train, y_train, SS_mat, TS_mat, GS_mat, mlb, idx2label)\n dataset = torch.utils.data.TensorDataset(SS, TS, GS, y)\n dataloader = torch.utils.data.DataLoader(dataset, batch_size=256, shuffle=True)\n \n model = build_model(hparam)\n model.to(device)\n model.fit(dataloader, i)\n \n x_test = x_idx[test_idx]\n y_test = y_idx[test_idx]\n pd.to_pickle([x_test, y_test], model.path+'test_data.pkl')\n del x_test, y_test\n \n</code></pre>\n<p>model.py code</p>\n<pre><code class=\"lang-auto\">import torch\nimport torch.nn as nn\nimport os\nimport pandas as pd\nimport numpy as np\nfrom math import ceil\n\nclass build_model(nn.Module):\n def __init__(self, hyperparameter):\n super(build_model, self).__init__() \n \n input_size = hyperparameter['input_size']\n output_size = hyperparameter['output_size']\n code_size = hyperparameter['code_size']\n AE_lr = hyperparameter['AE_lr']\n DNN_lr = hyperparameter['DNN_lr']\n drop_rate = hyperparameter['drop_rate']\n \n self.epoch = hyperparameter['epoch']\n self.n_repeats = hyperparameter['n_repeats']\n self.n_splits = hyperparameter['n_splits']\n self.save_path = hyperparameter['save_path']\n self.patience = hyperparameter['patience']\n \n self.device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n \n self.encoder1 = self.build_encoder(input_size, code_size, drop_rate)\n self.decoder1 = self.build_decoder(input_size, code_size)\n AE_params1 = list(self.encoder1.parameters()) + list(self.decoder1.parameters())\n self.AE_opt1 = torch.optim.RMSprop(AE_params1, lr=AE_lr)\n \n self.encoder2 = self.build_encoder(input_size, code_size, drop_rate)\n self.decoder2 = self.build_decoder(input_size, code_size)\n AE_params2 = list(self.encoder2.parameters()) + list(self.decoder2.parameters())\n self.AE_opt2 = torch.optim.RMSprop(AE_params2, lr=AE_lr)\n \n self.encoder3 = self.build_encoder(input_size, code_size, drop_rate)\n self.decoder3 = self.build_decoder(input_size, code_size)\n AE_params3 = list(self.encoder3.parameters()) + list(self.decoder3.parameters())\n self.AE_opt3 = torch.optim.RMSprop(AE_params3, lr=AE_lr)\n \n self.AE_criterion = nn.MSELoss() \n \n self.DNN = self.build_DNN(code_size*3, output_size, drop_rate)\n DNN_params = list(self.encoder1.parameters()) + list(self.encoder2.parameters()) + list(self.encoder3.parameters()) + list(self.DNN.parameters()) \n self.DNN_opt = torch.optim.Adam(DNN_params, lr=DNN_lr)\n \n self.DNN_criterion = nn.BCEWithLogitsLoss()\n \n \n def forward(self, x1, x2, x3):\n x1 = self.encoder1(x1)\n x1_de = self.decoder1(x1)\n \n x2 = self.encoder2(x2)\n x2_de = self.decoder2(x2)\n \n x3 = self.encoder3(x3)\n x3_de = self.decoder3(x3)\n \n x_dnn = torch.cat((x1, x2, x3), 1)\n pred = self.DNN(x_dnn)\n \n return x1_de, x2_de, x3_de, pred\n \n def fit(self, dataloader, repeat): \n n = ceil(len(dataloader.dataset)/dataloader.batch_size)\n loss_per_epoch = [] # ['DNN', 'SSP', 'TSP', 'GSP'] \n previous = -1\n for i in range(1, self.epoch+1):\n dnn, ae1, ae2, ae3, j = 0, 0, 0, 0, 1\n \n for x1, x2, x3, y in dataloader:\n x1, x2, x3, y = x1.to(self.device), x2.to(self.device), x3.to(self.device), y.to(self.device)\n o1, o2, o3, pred = self(x1, x2, x3)\n \n self.AE_opt1.zero_grad()\n AE_loss1 = self.AE_criterion(o1, x1) \n AE_loss1.backward(retain_graph=True)\n self.AE_opt1.step()\n \n self.AE_opt2.zero_grad()\n AE_loss2 = self.AE_criterion(o2, x2)\n AE_loss2.backward(retain_graph=True)\n self.AE_opt2.step()\n \n self.AE_opt3.zero_grad()\n AE_loss3 = self.AE_criterion(o3, x3)\n AE_loss3.backward(retain_graph=True)\n self.AE_opt3.step()\n \n self.DNN_opt.zero_grad()\n DNN_loss = self.DNN_criterion(pred, y)\n DNN_loss.backward()\n self.DNN_opt.step()\n \n tmp_loss = list(map(lambda x: round(float(x), 6), [DNN_loss, AE_loss1, AE_loss2, AE_loss3]))\n dnn += tmp_loss[0]\n ae1 += tmp_loss[1]\n ae2 += tmp_loss[2]\n ae3 += tmp_loss[3]\n \n if j % 50 == 0:\n print(f'Repeat {repeat+1}/{self.n_repeats*self.n_splits} Epoch {i}/{self.epoch} Iter {j}/{n} \\n Loss: DNN {dnn/j:.6f} SSP {ae1/j:.6f} TSP {ae2/j:.6f} GSP {ae3/j:.6f}')\n print()\n \n j += 1\n \n j -= 1\n print(f'Repeat {repeat+1}/{self.n_repeats*self.n_splits} Epoch {i}/{self.epoch} Iter {j}/{n} \\n Loss: DNN {dnn/j:.6f} SSP {ae1/j:.6f} TSP {ae2/j:.6f} GSP {ae3/j:.6f}')\n print()\n loss_per_epoch.append(list(map(lambda x: x/(j), [dnn, ae1, ae2, ae3]))) \n \n \n if len(loss_per_epoch) > self.patience:\n sum_loss = np.sum(np.array(loss_per_epoch), 1)\n current = np.argmin(sum_loss)\n \n if previous != current:\n previous = current\n self.save_model(repeat, loss_per_epoch)\n \n elif (previous == current) and (previous + self.patience == len(loss_per_epoch)):\n print('===================Early Stopping===================')\n break\n \n \n def build_encoder(self, input_size, code_size, drop_rate): \n encoder = nn.Sequential( \n nn.Linear(input_size, 1000),\n nn.BatchNorm1d(1000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True), \n nn.Linear(1000, code_size),\n nn.BatchNorm1d(code_size),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True))\n \n return encoder\n \n def build_decoder(self, input_size, code_size):\n decoder = nn.Sequential(\n nn.Linear(code_size, 1000),\n nn.BatchNorm1d(1000),\n nn.ReLU(True), \n nn.Linear(1000, input_size), \n nn.Sigmoid())\n \n return decoder\n \n def build_DNN(self, input_size, output_size, drop_rate):\n DNN = nn.Sequential( \n nn.Linear(input_size, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True), \n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, 2000),\n nn.BatchNorm1d(2000),\n nn.Dropout(p=drop_rate, inplace=True),\n nn.ReLU(True),\n nn.Linear(2000, output_size))\n \n return DNN\n \n \n def save_model(self, repeat, loss_per_epoch):\n self.path = self.save_path + str(repeat) + '/'\n if not os.path.isdir(self.path):\n os.mkdir(self.path)\n torch.save(self.state_dict(), self.path+'model_checkpoint')\n pd.to_pickle(loss_per_epoch, self.path+'loss_per_epoch.pkl')\n \n def load_model(self, path):\n weights = torch.load(path, map_location=self.device)\n self.load_state_dict(weights) \n</code></pre>\n<p>utils.py code</p>\n<pre><code class=\"lang-auto\">import torch\nimport torch.nn as nn\nimport numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MultiLabelBinarizer\nfrom sklearn.metrics import recall_score, precision_score, accuracy_score\n\ndef index_data(): \n drugPair2effect = pd.read_pickle('../data/drugPair2effect_idx.pkl')\n y_all = list(drugPair2effect.values())\n \n mlb = MultiLabelBinarizer()\n mlb.fit_transform(y_all)\n \n labels = sorted(list(set(y_all)))\n \n label2idx = {}\n for i, j in enumerate(labels):\n label2idx[j] = i\n \n drugPair2effectIdx = {}\n for k, v in drugPair2effect.items():\n drugPair2effectIdx[k] = label2idx[v]\n \n idx2label = np.zeros(len(label2idx), dtype='O')\n for k, v in label2idx.items():\n idx2label[v] = np.array(k)\n \n return mlb, label2idx, idx2label, drugPair2effectIdx\n\n\ndef convert_tensor(x_idx, y_idx, SS_mat, TS_mat, GS_mat, mlb, idx2label): \n device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n \n SS = torch.tensor(SS_mat[x_idx].reshape(len(x_idx), len(SS_mat)*2)).float()\n TS = torch.tensor(TS_mat[x_idx].reshape(len(x_idx), len(TS_mat)*2)).float()\n GS = torch.tensor(GS_mat[x_idx].reshape(len(x_idx), len(GS_mat)*2)).float()\n y = torch.tensor(mlb.transform(idx2label[y_idx])).float()\n \n return SS, TS, GS, y\n\ndef evaluate_model(answer, prediction):\n accuracy = accuracy_score(answer, prediction)\n macro_recall = recall_score(answer, prediction, average='macro')\n macro_precision = precision_score(answer, prediction, average='macro')\n micro_recall = recall_score(answer, prediction, average='micro')\n micro_precision = precision_score(answer, prediction, average='micro')\n \n return accuracy, macro_recall, macro_precision, micro_recall, micro_precision\n\n</code></pre>\n<p>i need help please</p>",527 "post_number": 1,528 "post_type": 1,529 "posts_count": 2,530 "updated_at": "2023-05-14T11:34:09.348Z",531 "reply_count": 1,532 "reply_to_post_number": null,533 "quote_count": 0,534 "incoming_link_count": 35,535 "reads": 6,536 "readers_count": 5,537 "score": 181.2,538 "yours": false,539 "topic_id": 179879,540 "topic_slug": "runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-200-is-at-version-2-expected-version-1-instead-hint-enable-anomaly-detection-to-find-the-operation-that-failed-to-comput",541 "display_username": "mahmoud ramadan muhammed",542 "primary_group_name": null,543 "flair_name": null,544 "flair_url": null,545 "flair_bg_color": null,546 "flair_color": null,547 "flair_group_id": null,548 "badges_granted": [],549 "version": 1,550 "can_edit": false,551 "can_delete": false,552 "can_recover": false,553 "can_see_hidden_post": false,554 "can_wiki": false,555 "link_counts": [556 {557 "url": "https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-1-1-256-256-which-is-output-0-of-relubackward0-is-at-version-1-expected-version-0-instead/143490/7",558 "internal": true,559 "reflection": true,560 "title": "RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 1, 256, 256]], which is output 0 of ReluBackward0, is at version 1; expected version 0 instead",561 "clicks": 0562 }563 ],564 "read": true,565 "user_title": null,566 "bookmarked": false,567 "actions_summary": [],568 "moderator": false,569 "admin": false,570 "staff": false,571 "user_id": 66167,572 "hidden": false,573 "trust_level": 0,574 "deleted_at": null,575 "user_deleted": false,576 "edit_reason": null,577 "can_view_edit_history": true,578 "wiki": false,579 "post_url": "/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-200-is-at-version-2-expected-version-1-instead-hint-enable-anomaly-detection-to-find-the-operation-that-failed-to-comput/179879/1",580 "can_accept_answer": false,581 "can_unaccept_answer": false,582 "accepted_answer": false,583 "topic_accepted_answer": null,584 "can_vote": false585 },586 {587 "id": 401921,588 "name": "K. Frank",589 "username": "KFrank",590 "avatar_template": "/letter_avatar_proxy/v4/letter/k/ecb155/{size}.png",591 "created_at": "2023-05-14T16:52:32.714Z",592 "cooked": "<p>Hi Mahmoud!</p>\n<aside class=\"quote no-group\" data-username=\"mahmoud_ramadan_muha\" data-post=\"1\" data-topic=\"179879\" data-full=\"true\">\n<div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img loading=\"lazy\" alt=\"\" width=\"24\" height=\"24\" src=\"https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mahmoud_ramadan_muha/48/60492_2.png\" class=\"avatar\"> mahmoud_ramadan_muha:</div>\n<blockquote>\n<p>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation:</p>\n</blockquote>\n</aside>\n<p>For some suggestions about how to debug such inplace-modification errors,<br>\nsee this post:</p>\n<aside class=\"quote quote-modified\" data-post=\"7\" data-topic=\"171826\">\n <div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img loading=\"lazy\" alt=\"\" width=\"24\" height=\"24\" src=\"https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/48.png\" class=\"avatar\">\n <a href=\"https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-64-1-which-is-output-0-of-asstridedbackward0-is-at-version-3-expected-version-2-instead-hint-the-backtrace-further-a/171826/7\">\"RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64, 1]], which is output 0 of AsStridedBackward0, is at version 3; expected version 2 instead. Hint: the backtrace further a</a> <a class=\"badge-category__wrapper \" href=\"/c/autograd/7\"><span data-category-id=\"7\" data-drop-close=\"true\" class=\"badge-category \" title=\"A category of posts relating to the autograd engine itself.\"><span class=\"badge-category__name\">autograd</span></span></a>\n </div>\n <blockquote>\n Hi Fahmyadan and Sangyoon! \n\nHere are some suggestions about how to track down (and maybe fix) \ninplace-modification errors. Note that an inplace modification in the forward \npass is not necessarily* an error – it depends on whether and how the tensor \nthat was modified is used in the backward pass. Note that inplace operations \ncan be useful for saving memory – if you replace an innocent inplace operation \nwith an out-of-place equivalent, your training will use more memory (and, to a \nminor e…\n </blockquote>\n</aside>\n\n<aside class=\"quote no-group\">\n<blockquote>\n<pre><code class=\"lang-auto\"> self.AE_opt1.zero_grad()\n AE_loss1 = self.AE_criterion(o1, x1) \n AE_loss1.backward(retain_graph=True)\n self.AE_opt1.step()\n \n self.AE_opt2.zero_grad()\n AE_loss2 = self.AE_criterion(o2, x2)\n AE_loss2.backward(retain_graph=True)\n self.AE_opt2.step()\n \n self.AE_opt3.zero_grad()\n AE_loss3 = self.AE_criterion(o3, x3)\n AE_loss3.backward(retain_graph=True)\n self.AE_opt3.step()\n</code></pre>\n</blockquote>\n</aside>\n<p>As discussed in the linked post, these <code>backward (retain_graph=True)</code> calls<br>\noften cause inplace-modification errors, so you should make a particular point<br>\nof looking at them while debugging your issue.</p>\n<p>Best.</p>\n<p>K. Frank</p>",593 "post_number": 2,594 "post_type": 1,595 "posts_count": 2,596 "updated_at": "2023-05-14T16:52:32.714Z",597 "reply_count": 0,598 "reply_to_post_number": null,599 "quote_count": 1,600 "incoming_link_count": 3,601 "reads": 4,602 "readers_count": 3,603 "score": 15.8,604 "yours": false,605 "topic_id": 179879,606 "topic_slug": "runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-200-is-at-version-2-expected-version-1-instead-hint-enable-anomaly-detection-to-find-the-operation-that-failed-to-comput",607 "display_username": "K. Frank",608 "primary_group_name": null,609 "flair_name": null,610 "flair_url": null,611 "flair_bg_color": null,612 "flair_color": null,613 "flair_group_id": null,614 "badges_granted": [],615 "version": 1,616 "can_edit": false,617 "can_delete": false,618 "can_recover": false,619 "can_see_hidden_post": false,620 "can_wiki": false,621 "link_counts": [622 {623 "url": "https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-64-1-which-is-output-0-of-asstridedbackward0-is-at-version-3-expected-version-2-instead-hint-the-backtrace-further-a/171826/7",624 "internal": true,625 "reflection": false,626 "title": "\"RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64, 1]], which is output 0 of AsStridedBackward0, is at version 3; expected version 2 instead. Hint: the backtrace further a",627 "clicks": 0628 }629 ],630 "read": true,631 "user_title": null,632 "bookmarked": false,633 "actions_summary": [],634 "moderator": false,635 "admin": false,636 "staff": false,637 "user_id": 18088,638 "hidden": false,639 "trust_level": 2,640 "deleted_at": null,641 "user_deleted": false,642 "edit_reason": null,643 "can_view_edit_history": true,644 "wiki": false,645 "post_url": "/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-200-is-at-version-2-expected-version-1-instead-hint-enable-anomaly-detection-to-find-the-operation-that-failed-to-comput/179879/2",646 "can_accept_answer": false,647 "can_unaccept_answer": false,648 "accepted_answer": false,649 "topic_accepted_answer": null650 }651 ],652 "stream": [653 401906,654 401921655 ]656 },657 "timeline_lookup": [658 [659 1,660 895661 ]662 ],663 "suggested_topics": [664 {665 "fancy_title": "Custom Dataset __getitem__ is receiving a list from DataLoader",666 "id": 217592,667 "title": "Custom Dataset __getitem__ is receiving a list from DataLoader",668 "slug": "custom-dataset-getitem-is-receiving-a-list-from-dataloader",669 "posts_count": 4,670 "reply_count": 2,671 "highest_post_number": 4,672 "image_url": null,673 "created_at": "2025-03-08T12:27:38.663Z",674 "last_posted_at": "2025-03-14T05:32:05.342Z",675 "bumped": true,676 "bumped_at": "2025-03-14T05:32:05.342Z",677 "archetype": "regular",678 "unseen": false,679 "pinned": false,680 "unpinned": null,681 "visible": true,682 "closed": false,683 "archived": false,684 "bookmarked": null,685 "liked": null,686 "tags_descriptions": {},687 "like_count": 0,688 "views": 77,689 "category_id": 5,690 "featured_link": null,691 "has_accepted_answer": false,692 "posters": [693 {694 "extras": null,695 "description": "Original Poster",696 "user": {697 "id": 83139,698 "username": "Gabrichilleron",699 "name": "Gabriel Chilleron peinado",700 "avatar_template": "/user_avatar/discuss.pytorch.org/gabrichilleron/{size}/76040_2.png",701 "trust_level": 0702 }703 },704 {705 "extras": null,706 "description": "Frequent Poster",707 "user": {708 "id": 69167,709 "username": "lostalot",710 "name": "",711 "avatar_template": "/letter_avatar_proxy/v4/letter/l/858c86/{size}.png",712 "trust_level": 1713 }714 },715 {716 "extras": "latest",717 "description": "Most Recent Poster",718 "user": {719 "id": 74591,720 "username": "UMAR_MASUD",721 "name": "UMAR MASUD",722 "avatar_template": "/user_avatar/discuss.pytorch.org/umar_masud/{size}/68863_2.png",723 "trust_level": 2724 }725 }726 ]727 },728 {729 "fancy_title": "Residual Connection breaks or not while calling separately",730 "id": 213792,731 "title": "Residual Connection breaks or not while calling separately",732 "slug": "residual-connection-breaks-or-not-while-calling-separately",733 "posts_count": 2,734 "reply_count": 0,735 "highest_post_number": 2,736 "image_url": null,737 "created_at": "2024-12-04T09:43:35.728Z",738 "last_posted_at": "2024-12-04T22:23:14.123Z",739 "bumped": true,740 "bumped_at": "2024-12-04T22:23:14.123Z",741 "archetype": "regular",742 "unseen": false,743 "pinned": false,744 "unpinned": null,745 "visible": true,746 "closed": false,747 "archived": false,748 "bookmarked": null,749 "liked": null,750 "tags_descriptions": {},751 "like_count": 0,752 "views": 33,753 "category_id": 5,754 "featured_link": null,755 "has_accepted_answer": false,756 "posters": [757 {758 "extras": null,759 "description": "Original Poster",760 "user": {761 "id": 68774,762 "username": "hasan11085",763 "name": "Mahmudul Hasan",764 "avatar_template": "/user_avatar/discuss.pytorch.org/hasan11085/{size}/63260_2.png",765 "trust_level": 1766 }767 },768 {769 "extras": "latest",770 "description": "Most Recent Poster",771 "user": {772 "id": 3534,773 "username": "ptrblck",774 "name": "",775 "avatar_template": "/user_avatar/discuss.pytorch.org/ptrblck/{size}/1823_2.png",776 "admin": true,777 "moderator": true,778 "trust_level": 2779 }780 }781 ]782 },783 {784 "fancy_title": "How can I prevent infinity value after switching from float32 to float16?",785 "id": 214380,786 "title": "How can I prevent infinity value after switching from float32 to float16?",787 "slug": "how-can-i-prevent-infinity-value-after-switching-from-float32-to-float16",788 "posts_count": 2,789 "reply_count": 0,790 "highest_post_number": 2,791 "image_url": null,792 "created_at": "2024-12-19T00:47:57.130Z",793 "last_posted_at": "2024-12-19T02:02:53.245Z",794 "bumped": true,795 "bumped_at": "2024-12-19T02:02:53.245Z",796 "archetype": "regular",797 "unseen": false,798 "pinned": false,799 "unpinned": null,800 "visible": true,801 "closed": false,802 "archived": false,803 "bookmarked": null,804 "liked": null,805 "tags_descriptions": {},806 "like_count": 1,807 "views": 48,808 "category_id": 5,809 "featured_link": null,810 "has_accepted_answer": true,811 "posters": [812 {813 "extras": null,814 "description": "Original Poster",815 "user": {816 "id": 45116,817 "username": "JimW",818 "name": "",819 "avatar_template": "/user_avatar/discuss.pytorch.org/jimw/{size}/38000_2.png",820 "trust_level": 1821 }822 },823 {824 "extras": "latest",825 "description": "Most Recent Poster, Accepted Answer",826 "user": {827 "id": 41396,828 "username": "soulitzer",829 "name": "",830 "avatar_template": "/letter_avatar_proxy/v4/letter/s/839c29/{size}.png",831 "trust_level": 2832 }833 }834 ]835 },836 {837 "fancy_title": "Modifying an instance segmentation model to incorporate images and measurement data",838 "id": 217250,839 "title": "Modifying an instance segmentation model to incorporate images and measurement data",840 "slug": "modifying-an-instance-segmentation-model-to-incorporate-images-and-measurement-data",841 "posts_count": 1,842 "reply_count": 0,843 "highest_post_number": 1,844 "image_url": null,845 "created_at": "2025-02-27T21:52:00.292Z",846 "last_posted_at": "2025-02-27T21:52:00.336Z",847 "bumped": true,848 "bumped_at": "2025-02-28T14:35:17.605Z",849 "archetype": "regular",850 "unseen": false,851 "pinned": false,852 "unpinned": null,853 "visible": true,854 "closed": false,855 "archived": false,856 "bookmarked": null,857 "liked": null,858 "tags_descriptions": {},859 "like_count": 0,860 "views": 18,861 "category_id": 5,862 "featured_link": null,863 "has_accepted_answer": false,864 "posters": [865 {866 "extras": "latest single",867 "description": "Original Poster, Most Recent Poster",868 "user": {869 "id": 77908,870 "username": "mycul",871 "name": "",872 "avatar_template": "/user_avatar/discuss.pytorch.org/mycul/{size}/72394_2.png",873 "trust_level": 2874 }875 }876 ]877 },878 {879 "fancy_title": "Semantic Segmentation with Attention based CycleGAN",880 "id": 217647,881 "title": "Semantic Segmentation with Attention based CycleGAN",882 "slug": "semantic-segmentation-with-attention-based-cyclegan",883 "posts_count": 5,884 "reply_count": 1,885 "highest_post_number": 5,886 "image_url": null,887 "created_at": "2025-03-10T06:44:59.887Z",888 "last_posted_at": "2025-03-11T22:57:33.781Z",889 "bumped": true,890 "bumped_at": "2025-03-11T22:57:33.781Z",891 "archetype": "regular",892 "unseen": false,893 "pinned": false,894 "unpinned": null,895 "visible": true,896 "closed": false,897 "archived": false,898 "bookmarked": null,899 "liked": null,900 "tags_descriptions": {},901 "like_count": 0,902 "views": 133,903 "category_id": 5,904 "featured_link": null,905 "has_accepted_answer": false,906 "posters": [907 {908 "extras": null,909 "description": "Original Poster",910 "user": {911 "id": 81422,912 "username": "Idrees11",913 "name": "Idrees Bhat",914 "avatar_template": "/user_avatar/discuss.pytorch.org/idrees11/{size}/74448_2.png",915 "trust_level": 1916 }917 },918 {919 "extras": "latest",920 "description": "Most Recent Poster",921 "user": {922 "id": 18088,923 "username": "KFrank",924 "name": "K. Frank",925 "avatar_template": "/letter_avatar_proxy/v4/letter/k/ecb155/{size}.png",926 "trust_level": 2927 }928 }929 ]930 }931 ],932 "tags_descriptions": {},933 "fancy_title": "RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [200]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to comput",934 "id": 179879,935 "title": "RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [200]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to comput",936 "posts_count": 2,937 "created_at": "2023-05-14T11:34:09.242Z",938 "views": 908,939 "reply_count": 0,940 "like_count": 0,941 "last_posted_at": "2023-05-14T16:52:32.714Z",942 "visible": true,943 "closed": false,944 "archived": false,945 "has_summary": false,946 "archetype": "regular",947 "slug": "runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-200-is-at-version-2-expected-version-1-instead-hint-enable-anomaly-detection-to-find-the-operation-that-failed-to-comput",948 "category_id": 5,949 "word_count": 1509,950 "deleted_at": null,951 "user_id": 66167,952 "featured_link": null,953 "pinned_globally": false,954 "pinned_at": null,955 "pinned_until": null,956 "image_url": null,957 "slow_mode_seconds": 0,958 "draft": null,959 "draft_key": "topic_179879",960 "draft_sequence": null,961 "unpinned": null,962 "pinned": false,963 "current_post_number": 1,964 "highest_post_number": 2,965 "deleted_by": null,966 "actions_summary": [967 {968 "id": 4,969 "count": 0,970 "hidden": false,971 "can_act": false972 },973 {974 "id": 8,975 "count": 0,976 "hidden": false,977 "can_act": false978 },979 {980 "id": 10,981 "count": 0,982 "hidden": false,983 "can_act": false984 },985 {986 "id": 7,987 "count": 0,988 "hidden": false,989 "can_act": false990 }991 ],992 "chunk_size": 20,993 "bookmarked": false,994 "topic_timer": null,995 "message_bus_last_id": 0,996 "participant_count": 2,997 "show_read_indicator": false,998 "thumbnails": null,999 "slow_mode_enabled_until": null,1000 "can_vote": false,1001 "vote_count": 0,1002 "user_voted": false,1003 "discourse_zendesk_plugin_zendesk_id": null,1004 "discourse_zendesk_plugin_zendesk_url": "https://your-url.zendesk.com/agent/tickets/",1005 "details": {1006 "can_edit": false,1007 "notification_level": 1,1008 "participants": [1009 {1010 "id": 18088,1011 "username": "KFrank",1012 "name": "K. Frank",1013 "avatar_template": "/letter_avatar_proxy/v4/letter/k/ecb155/{size}.png",1014 "post_count": 1,1015 "primary_group_name": null,1016 "flair_name": null,1017 "flair_url": null,1018 "flair_color": null,1019 "flair_bg_color": null,1020 "flair_group_id": null,1021 "trust_level": 21022 },1023 {1024 "id": 66167,1025 "username": "mahmoud_ramadan_muha",1026 "name": "mahmoud ramadan muhammed",1027 "avatar_template": "/user_avatar/discuss.pytorch.org/mahmoud_ramadan_muha/{size}/60492_2.png",1028 "post_count": 1,1029 "primary_group_name": null,1030 "flair_name": null,1031 "flair_url": null,1032 "flair_color": null,1033 "flair_bg_color": null,1034 "flair_group_id": null,1035 "trust_level": 01036 }1037 ],1038 "created_by": {1039 "id": 66167,1040 "username": "mahmoud_ramadan_muha",1041 "name": "mahmoud ramadan muhammed",1042 "avatar_template": "/user_avatar/discuss.pytorch.org/mahmoud_ramadan_muha/{size}/60492_2.png"1043 },1044 "last_poster": {1045 "id": 18088,1046 "username": "KFrank",1047 "name": "K. Frank",1048 "avatar_template": "/letter_avatar_proxy/v4/letter/k/ecb155/{size}.png"1049 }1050 },1051 "bookmarks": []1052 },1053 {1054 "post_stream": {1055 "posts": [1056 {1057 "id": 401913,1058 "name": "",1059 "username": "Ategantos",1060 "avatar_template": "/letter_avatar_proxy/v4/letter/a/439d5e/{size}.png",1061 "created_at": "2023-05-14T13:38:44.497Z",1062 "cooked": "<p>I am having trouble training an agent to play Doom through the vizdoom library using as a state the frame of the game and a convolutional neural network. I am giving you my implementation of the training loop and I wanted to ask if everything is right and the gradients are computed correctly.</p>\n<blockquote>\n<p>def train_policy_gradient_net(env, net, optimizer, num_episodes, gamma, max_trajectory_len, standardize):<br>\nrewards_per_episode = <span class=\"chcklst-box fa fa-square-o fa-fw\"></span><br>\nsteps_per_episode = <span class=\"chcklst-box fa fa-square-o fa-fw\"></span><br>\nloss_per_epoch = <span class=\"chcklst-box fa fa-square-o fa-fw\"></span></p>\n<pre><code>for i in range(num_episodes):\n state = env.reset()\n done = False\n total_reward = 0\n steps = 0\n\n count = 0\n log_probabilities = []\n rewards = []\n while(count < max_trajectory_len and not done):\n # use the network's stochastic policy to choose action and convert shape from [1, num_actions] to [num_actions]\n log_prob = net(torch.tensor(np.expand_dims(np.expand_dims(state, axis=0), axis=0)).to(device)).squeeze(0)\n # get probabilities from log probabilities\n prob = torch.exp(log_prob)\n \n # for numerical precision reasons the probabilities are normalised with their sum to sum exactly at 1 (because before they might not sum up exactly to 1)\n prob = Tensor.cpu(prob).detach().numpy()\n action = np.random.choice(range(env.game.get_available_buttons_size()), p=prob / np.sum(prob))\n\n # keep all the log probabilities of the actions that were selected\n log_probabilities.append(log_prob[action])\n\n next_state, reward, done = env.step(action)\n \n total_reward += reward\n steps += 1\n\n rewards.append(reward)\n\n state = next_state\n count += 1\n \n rewards_per_episode.append(total_reward)\n steps_per_episode.append(steps)\n \n # train network on the trajectory generated from the episode\n # compute G for each action\n g_vals = np.zeros(len(rewards))\n g_vals[-1] = rewards[-1]\n for j in range(len(rewards) - 2, -1, -1):\n g_vals[j] = rewards[j] + gamma * g_vals[j + 1]\n\n if(standardize and len(g_vals) > 1):\n g_vals = (g_vals - np.mean(g_vals)) / np.std(g_vals)\n\n # we want to maximize the gain multiplied by the probabilities so we use a - to make the task to minimize the -gain so we can use pytorch's gradient descent\n log_probabilities_tensor = torch.stack(log_probabilities)\n g_vals = torch.tensor(g_vals).to(device)\n loss = -torch.sum((log_probabilities_tensor * g_vals))\n loss_per_epoch.append(loss.item())\n\n optimizer.zero_grad()\n loss.backward()\n optimizer.step()\n\nreturn rewards_per_episode, steps_per_episode, loss_per_epoch\n</code></pre>\n</blockquote>",1063 "post_number": 1,1064 "post_type": 1,1065 "posts_count": 1,1066 "updated_at": "2023-05-14T13:38:44.497Z",1067 "reply_count": 0,1068 "reply_to_post_number": null,1069 "quote_count": 0,1070 "incoming_link_count": 160,1071 "reads": 6,1072 "readers_count": 5,1073 "score": 796.2,1074 "yours": false,1075 "topic_id": 179884,1076 "topic_slug": "implementation-of-vanilla-policy-gradient-reinforce-method",1077 "display_username": "",1078 "primary_group_name": null,1079 "flair_name": null,1080 "flair_url": null,1081 "flair_bg_color": null,1082 "flair_color": null,1083 "flair_group_id": null,1084 "badges_granted": [],1085 "version": 1,1086 "can_edit": false,1087 "can_delete": false,1088 "can_recover": false,1089 "can_see_hidden_post": false,1090 "can_wiki": false,1091 "read": true,1092 "user_title": null,1093 "bookmarked": false,1094 "actions_summary": [],1095 "moderator": false,1096 "admin": false,1097 "staff": false,1098 "user_id": 66168,1099 "hidden": false,1100 "trust_level": 0,1101 "deleted_at": null,1102 "user_deleted": false,1103 "edit_reason": null,1104 "can_view_edit_history": true,1105 "wiki": false,1106 "post_url": "/t/implementation-of-vanilla-policy-gradient-reinforce-method/179884/1",1107 "can_accept_answer": false,1108 "can_unaccept_answer": false,1109 "accepted_answer": false,1110 "topic_accepted_answer": null,1111 "can_vote": false1112 }1113 ],1114 "stream": [1115 4019131116 ]1117 },1118 "timeline_lookup": [1119 [1120 1,1121 8951122 ]1123 ],1124 "suggested_topics": [1125 {1126 "fancy_title": "Training Machine Learning Model In Browser For Reinforcement Learning",1127 "id": 218074,1128 "title": "Training Machine Learning Model In Browser For Reinforcement Learning",1129 "slug": "training-machine-learning-model-in-browser-for-reinforcement-learning",1130 "posts_count": 1,1131 "reply_count": 0,1132 "highest_post_number": 1,1133 "image_url": null,1134 "created_at": "2025-03-20T13:12:14.662Z",1135 "last_posted_at": "2025-03-20T13:12:14.696Z",1136 "bumped": true,1137 "bumped_at": "2025-03-20T13:12:14.696Z",1138 "archetype": "regular",1139 "unseen": false,1140 "pinned": false,1141 "unpinned": null,1142 "visible": true,1143 "closed": false,1144 "archived": false,1145 "bookmarked": null,1146 "liked": null,1147 "tags_descriptions": {},1148 "like_count": 0,1149 "views": 78,1150 "category_id": 6,1151 "featured_link": null,1152 "has_accepted_answer": false,1153 "posters": [1154 {1155 "extras": "latest single",1156 "description": "Original Poster, Most Recent Poster",1157 "user": {1158 "id": 83382,1159 "username": "Sliferslacker",1160 "name": "",1161 "avatar_template": "/letter_avatar_proxy/v4/letter/s/ecccb3/{size}.png",1162 "trust_level": 11163 }1164 }1165 ]1166 },1167 {1168 "fancy_title": "Should we split the trajectories prior to calculating the loss for a DQN?",1169 "id": 221749,1170 "title": "Should we split the trajectories prior to calculating the loss for a DQN?",1171 "slug": "should-we-split-the-trajectories-prior-to-calculating-the-loss-for-a-dqn",1172 "posts_count": 2,1173 "reply_count": 0,1174 "highest_post_number": 2,1175 "image_url": null,1176 "created_at": "2025-07-23T13:01:26.078Z",1177 "last_posted_at": "2025-08-04T21:31:18.537Z",1178 "bumped": true,1179 "bumped_at": "2025-08-04T21:31:18.537Z",1180 "archetype": "regular",1181 "unseen": false,1182 "pinned": false,1183 "unpinned": null,1184 "visible": true,1185 "closed": false,1186 "archived": false,1187 "bookmarked": null,1188 "liked": null,1189 "tags_descriptions": {},1190 "like_count": 1,1191 "views": 29,1192 "category_id": 6,1193 "featured_link": null,1194 "has_accepted_answer": false,1195 "posters": [1196 {1197 "extras": null,1198 "description": "Original Poster",1199 "user": {1200 "id": 23119,