typeError: only integer tensors.....only appearing in the editor not in notebook

username: juseraru
cooked: <p>So basically this error is appearing only when I train my model in the editor (vscode, with anaconda environment) but when i run the same script in a jupyter notebook (in vscode) it works fine no error. I think might be import error or something else. i have three files, my pytorch model, my physics model and the main train file where I do the imports.</p>
<p>The error is: <em><strong>typeError: only integer tensors of a single element can be converted to an index.</strong></em></p>
<p>this is the line of code where the error is popping up.<br>
noise = (torch.randn_like(action) * self.policy_noise).clamp(-self.noise_clip, self.noise_clip)<br>
which is in the pytorch model.</p>
<p>here is the complete error message:</p>
<p>Traceback (most recent call last):<br>
File “c:/Users/ACER/Desktop/ACER/jupyter notebook/python_scripts/drone/train.py”, line 113, in <br>
policy.train(replay_buffer, batch_size)<br>
File “c:\Users\ACER\Desktop\ACER\jupyter notebook\python_scripts\drone\agent.py”, line 144, in train<br>
torch.randn_like(action) * self.policy_noise<br>
TypeError: only integer tensors of a single element can be converted to an index</p>

username: ptrblck
cooked: <p>I guess you might be using different PyTorch versions in your IDE and Jupyter, where apparently some operations might yield different shapes.<br>
Could you check the versions via <code>print(torch.__version__)</code> and make sure they are equal?</p>

username: juseraru
cooked: <p>hi thanks for answering but that is not possible im using the same virtual environment running in the IDE and jupyter. I did still checked versions via <code>print(torch.__verison__)</code> and they are the same ‘1.6.0’.</p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> Any other suggestion?</p>
<p>I also have tested the  code (as a jupyte notebook) in google colab and works fine like in my local ipython. the issue is directly with the separeted scripts.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="juseraru" data-post="3" data-topic="96866">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juseraru/48/28945_2.png" class="avatar"> juseraru:</div>
<blockquote>
<p>the issue is directly with the separeted scripts.</p>
</blockquote>
</aside>
<p>I’m not sure if I misunderstand the use case, but I thought you are using the same script and the only difference is the notebook vs. IDE?<br>
Could you reuse the working script and execute it in the currently non-working environment, if that’s not the case?</p>

username: juseraru
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> yes. so what I have is 3 scripts for the IDE: agent.py(pytorch model), env.py(physics model) and train.py where I do import the other two classes. in my jupyter notebook i just have 4 cells 1 to import all the modules(torch, numpy…etc) used in the scripts, 1 cell for each script, the only output is the cumulative reward  of each epoch nothing else, that is coming from the train cell.</p>
<p>again no error in the jupyter notebook but it does appear when i run train.py in the IDE.</p>
<p>something that i dont want to do but im thinking is to have just one big script, and see if the error still appear, in each script i just have one class except for the agent script which i have two classes memory buffer and the agent. it could be something related with the data, maybe somewhere i am still no changing the data format and im saving tensors instead of numpy arrays im gonna check that as well.</p>

username: ptrblck
cooked: <p>Yeah, it’s a bit weird, but your guess might be right. The issue might indeed come from a difference in the data loading or processing.</p>
<p>Could you print the shape of <code>action</code> in both envs so that we could maybe try debugging the actual issue to narrow down the difference?</p>