TypeError: unsupported operand type(s) for +: 'int' and 'list'

username: dani_kal
cooked: <p>Hi!!!<br>
I am looking for help because I have been stuck at this point for 3 days and I dont know what else to do.<br>
I want to train a NN. In my code after all appropriate installations this error appears.<br>
TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’<br>
I think that the code has no errors because I have find other similar codes in the internet and they did not have problem.<br>
I believe that I dont have install the appropriate versions either from Python, either from Pytorch, something like that…<br>
Can you help me identify and solve the problem?</p>
<p>My code:<br>
def <strong>init</strong>(self, state_size, action_size, seed, fc1_units=400, fc2_units=300, use_bn=False):<br>
print(type(fc1_units))<br>
print(type(action_size))<br>
“”“Initialize parameters and build model.”""<br>
super(Critic, self).<strong>init</strong>()<br>
self.seed = torch.manual_seed(seed)<br>
self.use_bn = use_bn<br>
self.fc1 = nn.Linear(state_size, fc1_units)<br>
<strong>self.fc2 = nn.Linear(fc1_units + action_size, fc2_units)</strong>   ERROR<br>
self.fc3 = nn.Linear(fc2_units, 1)<br>
if self.use_bn:<br>
self.bn1 = nn.BatchNorm1d(fc1_units)<br>
self.bn2 = nn.BatchNorm1d(fc2_units)</p>
<pre><code>    self.reset_parameters()
</code></pre>
<p><strong>Error :</strong><br>
&lt;class ‘int’&gt;<br>
&lt;class ‘list’&gt;<br>
Traceback (most recent call last):<br>
File “maddpg_trainer.py”, line 151, in <br>
main()<br>
File “maddpg_trainer.py”, line 143, in main<br>
agent = MADDPGAgentTrainer(state_shape, action_size, num_agents, writer=writer, random_seed=48, dirname=dirname, print_every=100, model_path=args.model)<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 331, in <strong>init</strong><br>
for i in range(self.num_agents)]<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 331, in <br>
for i in range(self.num_agents)]<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 86, in <strong>init</strong><br>
random_seed, fc1_units=self.config[‘FC1’], fc2_units=self.config[‘FC2’]<br>
File “C:\ML-Agents\Katorina\models.py”, line 87, in <strong>init</strong><br>
self.fc2 = nn.Linear(fc1_units + action_size, fc2_units)<br>
<strong>TypeError: unsupported operand type(s) for +: ‘int’ and 'list’</strong></p>
<p>Thank you in advance!</p>

username: Dishin_Goyani
cooked: <aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>self.fc2 = nn.Linear(fc1_units + action_size, fc2_units)</p>
</blockquote>
</aside>
<p>If i understand correctly; you want to add number into the List than it would be</p>
<pre><code class="lang-auto">self.fc2 = nn.Linear([fc1_units] + action_size, fc2_units)
</code></pre>

username: dani_kal
cooked: <p>Hi!!<br>
Thank you very much for answering me!<br>
Actually they are integers. Their initialization is this line</p>
<aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>def <strong>init</strong> (self, state_size, action_size, seed, fc1_units=400, fc2_units=300, use_bn=False):</p>
</blockquote>
</aside>

username: RaLo4
cooked: <aside class="quote no-group" data-username="dani_kal" data-post="3" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>Actually they are integers</p>
</blockquote>
</aside>
<p>The problem is that <code>action_size</code> is actually not an integer. You even printed it’s type here:</p>
<aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>print(type(action_size))</p>
</blockquote>
</aside>
<p>getting this result:</p>
<aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>&lt;class ‘list’&gt;</p>
</blockquote>
</aside>
<p>If you want to use the <code>+</code> operator on a <code>list</code> and an <code>integer</code> you can either add the number into the List as Dishin_Goyani explained.</p>
<aside class="quote no-group" data-username="Dishin_Goyani" data-post="2" data-topic="92205">
<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/dishin_goyani/48/16708_2.png" class="avatar"> Dishin_Goyani:</div>
<blockquote>
<p>If i understand correctly; you want to add number into the List than it would be</p>
<pre><code class="lang-auto">self.fc2 = nn.Linear([fc1_units] + action_size, fc2_units)
</code></pre>
</blockquote>
</aside>
<p>Or take one item from the List (assuming there are integers in your list) and add it together with your <code>fc1_units</code>. Like:</p>
<pre><code class="lang-auto">self.fc2 = nn.Linear(fc1_units + action_size[0], fc2_units)
</code></pre>

username: dani_kal
cooked: <p>Unfortunately, nothing works.<br>
In the first case  with this change : self.fc2 = nn.Linear([fc1_units] + action_size, fc2_units)<br>
I get this error</p>
<blockquote>
<p>File “C:\Users\Danae\Anaconda3\envs\effort7\lib\site-packages\torch\nn\modules\linear.py”, line 41, in <strong>init</strong><br>
self.weight = Parameter(torch.Tensor(out_features, in_features))<br>
TypeError: ‘list’ object cannot be interpreted as an integer</p>
</blockquote>
<p>and with this change self.fc2 = nn.Linear(fc1_units + action_size[0], fc2_units)<br>
I get this error</p>
<blockquote>
<p>File “C:\Users\Danae\Anaconda3\envs\effort7\lib\site-packages\torch\nn\modules\linear.py”, line 41, in <strong>init</strong><br>
self.weight = Parameter(torch.Tensor(out_features, in_features))<br>
RuntimeError: CUDA error (35): CUDA driver version is insufficient for CUDA runtime version</p>
</blockquote>

username: RaLo4
cooked: <p>Can you maybe print <code>action_size</code>. Just so I can see what this list contains.<br>
Just do the same as you did with</p>
<aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>print(type(action_size))</p>
</blockquote>
</aside>
<p>but remove the <code>type()</code> so it’s just <code>print(action_size)</code> and post the result.</p>
<p>But judging from this error</p>
<aside class="quote no-group" data-username="dani_kal" data-post="5" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>RuntimeError: CUDA error (35): CUDA driver version is insufficient for CUDA runtime version</p>
</blockquote>
</aside>
<p>it actually should have worked, but there might be a problem with CUDA or graphics driver version, or you are trying to use CUDA when you are supposed to use cpu (e.g. if you are on a cpu installation of pytorch or don’t have a compatible gpu)</p>

username: dani_kal
cooked: <blockquote>
<p>&lt;class ‘int’&gt;<br>
&lt;class ‘list’&gt;<br>
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]<br>
Traceback (most recent call last):<br>
File “maddpg_trainer.py”, line 151, in <br>
main()<br>
File “maddpg_trainer.py”, line 143, in main<br>
agent = MADDPGAgentTrainer(state_shape, action_size, num_agents, writer=writer, random_seed=48, dirname=dirname, print_every=100, model_path=args.model)<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 331, in <strong>init</strong><br>
for i in range(self.num_agents)]<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 331, in <br>
for i in range(self.num_agents)]<br>
File “C:\ML-Agents\Katorina\maddpg.py”, line 86, in <strong>init</strong><br>
random_seed, fc1_units=self.config[‘FC1’], fc2_units=self.config[‘FC2’]<br>
File “C:\ML-Agents\Katorina\models.py”, line 88, in <strong>init</strong><br>
self.fc2 = nn.Linear(fc1_units + action_size, fc2_units)<br>
TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’</p>
</blockquote>
<p>This is the result of print(action_size)<br>
I installed torch with this command without gpu.</p>
<blockquote>
<p>conda install pytorch torchvision cpuonly -c pytorch</p>
</blockquote>
<p>I had also tried to install a previous version of pytorch because I believed that this was the reason of the errors.<br>
Installing without CUDA<br>
<code>conda install pytorch=0.4.1 -c pytorch</code></p>

username: RaLo4
cooked: <p>ok. I have no idea what <code>action_size</code> is supposed to be, but</p>
<aside class="quote no-group" data-username="dani_kal" data-post="5" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>self.fc2 = nn.Linear(fc1_units + action_size[0], fc2_units)</p>
</blockquote>
</aside>
<p>will definitely work (as in, it will no longer give you this error)</p>
<aside class="quote no-group" data-username="dani_kal" data-post="1" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>TypeError: unsupported operand type(s) for +: ‘int’ and 'list’</p>
</blockquote>
</aside>
<p>As for this error message:</p>
<aside class="quote no-group" data-username="dani_kal" data-post="5" data-topic="92205">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/43a26b/48.png" class="avatar"> dani_kal:</div>
<blockquote>
<p>RuntimeError: CUDA error (35): CUDA driver version is insufficient for CUDA runtime version</p>
</blockquote>
</aside>
<p>I am not quite sure. It could be multiply things. The only thing I could say from the top of my head is:<br>
Are you maybe loading a model like so <code>model = torch.load(pathname)</code>?<br>
If so, you might wanna try and include a map_location like so: <code>model = torch.load(pathname, map_location=map_location)</code><br>
For CPU (your case I am assuming) that would be:<br>
<code>model = torch.load(pathname, map_location='cpu')</code></p>

username: dani_kal
cooked: <p>action_size is an integer that  represents the dimension of each action<br>
Do you mean something like that?</p>
<blockquote>
<p>logger = logging.getLogger(<strong>name</strong>)<br>
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
logger.info(‘Using device: {}’.format(device))</p>
</blockquote>