Error about type

111348: <p>import gym</p>
<p>import random</p>
<p>import collections</p>
<p>import numpy as np</p>
<p>import torch</p>
<p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<p>import torch.optim as optim</p>
<p><span class="hashtag">#Hyperparameters</span></p>
<p>lr_mu        = 0.0005   # learning rate of Actor</p>
<p>lr_q         = 0.001    # learning rate of Critic</p>
<p>gamma        = 0.99</p>
<p>batch_size   = 32</p>
<p>buffer_limit = 50000</p>
<p>tau          = 0.005 # for target network soft update</p>
<p>class ReplayBuffer():</p>
<pre><code>def __init__(self):

    self.buffer = collections.deque(maxlen=buffer_limit)

def put(self, data):

    self.buffer.append(data)



def sample(self, n):

    mini_batch = random.sample(self.buffer, n)

    s_lst, a_lst, r_lst, s_prime_lst, done_mask_lst = [], [], [], [], []

    for transition in mini_batch:

        s, a, r, s_prime, done_mask = transition

        s_lst.append(s)

        a_lst.append([a])

        r_lst.append([r])

        s_prime_lst.append(s_prime)

        done_mask_lst.append([done_mask])

    

    return torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst), \

           torch.tensor(r_lst), torch.tensor(s_prime_lst, dtype=torch.float), \

           torch.tensor(done_mask_lst)



def size(self):

    return len(self.buffer)
</code></pre>
<p>class MuNet(nn.Module):  #</p>
<pre><code>def __init__(self):

    super(MuNet, self).__init__()

    self.fc1 = nn.Linear(3, 128)

    self.fc2 = nn.Linear(128, 64)

    self.fc_mu = nn.Linear(64, 1)

def forward(self, x):

    x = F.relu(self.fc1(x))

    x = F.relu(self.fc2(x))

    mu = torch.tanh(self.fc_mu(x))*2 # Multipled by 2 because the action space of the Pendulum-v0 is [-2,2]

    return mu
</code></pre>
<p>class QNet(nn.Module):  <span class="hashtag">#score</span>(state, action)</p>
<h1>enters the network in middle layer</h1>
<pre><code>def __init__(self):

    super(QNet, self).__init__()

    

    self.fc_s = nn.Linear(3, 64) #encodes the state

    self.fc_a = nn.Linear(1,64)  #encodes the actions

    self.fc_q = nn.Linear(128, 32) #s+a = 128 

    self.fc_3 = nn.Linear(32,1) # output

def forward(self, x, a):

    h1 = F.relu(self.fc_s(x))

    h2 = F.relu(self.fc_a(a))

    cat = torch.cat([h1,h2], dim=1)

    q = F.relu(self.fc_q(cat))

    q = self.fc_3(q)

    return q
</code></pre>
<p>class OrnsteinUhlenbeckNoise:</p>
<pre><code>def __init__(self, mu):

    self.theta, self.dt, self.sigma = 0.1, 0.01, 0.1

    self.mu = mu

    self.x_prev = np.zeros_like(self.mu)

def __call__(self):

    x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + \

            self.sigma * np.sqrt(self.dt) * np.random.normal(size=self.mu.shape)

    self.x_prev = x

    return x
</code></pre>
<p>def train(mu, mu_target, q, q_target, memory, q_optimizer, mu_optimizer):</p>
<pre><code>s,a,r,s_prime,done_mask  = memory.sample(batch_size)



target = r + gamma * q_target(s_prime, mu_target(s_prime)) 

q_loss = F.smooth_l1_loss(q(s,a), target.detach())



q_optimizer.zero_grad()

q_loss.backward()

q_optimizer.step()



mu_loss = -q(s,mu(s)).mean() # That's all for the policy loss.

mu_optimizer.zero_grad()

mu_loss.backward()

mu_optimizer.step()
</code></pre>
<p>def soft_update(net, net_target):</p>
<pre><code>for param_target, param in zip(net_target.parameters(), net.parameters()):

    param_target.data.copy_(param_target.data * (1.0 - tau) + param.data * tau)
</code></pre>
<p>def main():</p>
<p>env = gym.make(‘Pendulum-v0’)</p>
<p>memory = ReplayBuffer()</p>
<p>q, q_target = QNet(), QNet()</p>
<p>q_target.load_state_dict(q.state_dict())</p>
<p>mu, mu_target = MuNet(), MuNet()</p>
<p>mu_target.load_state_dict(mu.state_dict())</p>
<p>score = 0.0</p>
<p>print_interval = 20</p>
<p>min_play_reward = 0</p>
<p>iterations = 100000</p>
<p>mu_optimizer = optim.Adam(mu.parameters(), lr=lr_mu)</p>
<p>q_optimizer  = optim.Adam(q.parameters(), lr=lr_q)</p>
<p>ou_noise = OrnsteinUhlenbeckNoise(mu=np.zeros(1))</p>
<p>def play_game():</p>
<pre><code>done = False

state = env.reset()    

while(not done):        

    a = mu(torch.from_numpy(s).float()) 

    a = a.item() + ou_noise()[0]

    s_prime, r, done, info = env.step([a])

    env.render()

    state = s_prime   
</code></pre>
<p>for iteration in range(iterations):</p>
<pre><code>s = env.reset()        

for t in range(300): # maximum length of episode is 200 for Pendulum-v0

    a = mu(torch.from_numpy(s).float()) 

    a = a.item() + ou_noise()[0]

    s_prime, r, done, info = env.step([a])

    memory.put((s,a,r/100.0,s_prime,done))

    score +=r

    s = s_prime

    if done:

        if score/print_interval &gt; min_play_reward:

            play_game()

        break              

            

if memory.size()&gt;2000:

        train(mu, mu_target, q, q_target, memory, q_optimizer, mu_optimizer)

        soft_update(mu, mu_target)

        soft_update(q,  q_target)

    

if iteration%print_interval==0 and iteration!=0:

    print("# of episode :{}, avg score : {:.1f}".format(iteration, score/print_interval))

    score = 0.0
</code></pre>
<p>env.close()</p>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:</p>
<pre><code>main()
</code></pre>
<p>This is Pendelum project code.<br>
It has some error about type. Could you let me know how can i solve?</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
150<br>
151     if memory.size()&gt;2000:<br>
–&gt; 152             train(mu, mu_target, q, q_target, memory, q_optimizer, mu_optimizer)<br>
153             soft_update(mu, mu_target)<br>
154             soft_update(q,  q_target)</p>
<p>5 frames<br>
 in train(mu, mu_target, q, q_target, memory, q_optimizer, mu_optimizer)<br>
86<br>
87     target = r + gamma * q_target(s_prime, mu_target(s_prime))<br>
—&gt; 88     q_loss = F.smooth_l1_loss(q(s,a), target.detach())<br>
89<br>
90     q_optimizer.zero_grad()</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p> in forward(self, x, a)<br>
65     def forward(self, x, a):<br>
66         h1 = F.relu(self.fc_s(x))<br>
—&gt; 67         h2 = F.relu(self.fc_a(a))<br>
68         cat = torch.cat([h1,h2], dim=1)<br>
69         q = F.relu(self.fc_q(cat))</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)<br>
85<br>
86     def forward(self, input):<br>
—&gt; 87         return F.linear(input, self.weight, self.bias)<br>
88<br>
89     def extra_repr(self):</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)<br>
1608     if input.dim() == 2 and bias is not None:<br>
1609         # fused op is marginally faster<br>
-&gt; 1610         ret = torch.addmm(bias, input, weight.t())<br>
1611     else:<br>
1612         output = input.matmul(weight.t())</p>
<p>RuntimeError: Expected object of scalar type Float but got scalar type Double for argument <span class="hashtag">#2</span> ‘mat1’ in call to _th_addmm</p>
<p>above sentence</p>

shlomish3: <p>Apperently you need to type case (or initialize) one of your variables to another, shorter data type.<br>
As the error indicates, try casting it <a href="https://pytorch.org/docs/stable/tensors.html" rel="noopener nofollow ugc">torch.float32</a>:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0.png" data-download-href="https://discuss.pytorch.org/uploads/default/3da688298305c1d719fc91e732cc1335dc424ef0" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0_2_690x267.png" alt="image" data-base62-sha1="8NnUX8Za6gP1zyMpefzK6JVePWo" width="690" height="267" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0_2_690x267.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0_2_1035x400.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0.png 2x" data-dominant-color="F9F8F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1091×423 22.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>It’s a bit difficult to follow your code example, so I can’t really tell what variable causes the problem.</p>