# Error about type

**URL:** <https://discuss.pytorch.org/t/error-about-type/87409>\
**Category:** Uncategorized\
**Created:** [June 30, 2020, 7:49am UTC](https://discuss.pytorch.org/t/error-about-type/87409 "2020-06-30T07:49:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![111348](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111348/32/25811_2.png) [@111348](https://discuss.pytorch.org/u/111348)\
**Post date:** [June 30, 2020, 7:49am UTC](https://discuss.pytorch.org/t/error-about-type/87409/1 "2020-06-30T07:49:28Z")

</div>

import gym

import random

import collections

import numpy as np

import torch

import torch.nn as nn

import torch.nn.functional as F

import torch.optim as optim

#Hyperparameters

lr\_mu = 0.0005 # learning rate of Actor

lr\_q = 0.001 # learning rate of Critic

gamma = 0.99

batch\_size = 32

buffer\_limit = 50000

tau = 0.005 # for target network soft update

class ReplayBuffer():

```
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)

```

class MuNet(nn.Module): #

```
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

```

class QNet(nn.Module): #score(state, action)

# enters the network in middle layer

```
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

```

class OrnsteinUhlenbeckNoise:

```
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

```

def train(mu, mu\_target, q, q\_target, memory, q\_optimizer, mu\_optimizer):

```
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()

```

def soft\_update(net, net\_target):

```
for param_target, param in zip(net_target.parameters(), net.parameters()):

    param_target.data.copy_(param_target.data * (1.0 - tau) + param.data * tau)

```

def main():

env = gym.make(‘Pendulum-v0’)

memory = ReplayBuffer()

q, q\_target = QNet(), QNet()

q\_target.load\_state\_dict(q.state\_dict())

mu, mu\_target = MuNet(), MuNet()

mu\_target.load\_state\_dict(mu.state\_dict())

score = 0.0

print\_interval = 20

min\_play\_reward = 0

iterations = 100000

mu\_optimizer = optim.Adam(mu.parameters(), lr=lr\_mu)

q\_optimizer = optim.Adam(q.parameters(), lr=lr\_q)

ou\_noise = OrnsteinUhlenbeckNoise(mu=np.zeros(1))

def play\_game():

```
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   

```

for iteration in range(iterations):

```
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 > min_play_reward:

            play_game()

        break              

            

if memory.size()>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

```

env.close()

if **name** == ‘ **main** ’:

```
main()

```

This is Pendelum project code.  
It has some error about type. Could you let me know how can i solve?

RuntimeError Traceback (most recent call last)  
 in ()  
150  
151 if memory.size()\>2000:  
–\> 152 train(mu, mu\_target, q, q\_target, memory, q\_optimizer, mu\_optimizer)  
153 soft\_update(mu, mu\_target)  
154 soft\_update(q, q\_target)

5 frames  
 in train(mu, mu\_target, q, q\_target, memory, q\_optimizer, mu\_optimizer)  
86  
87 target = r + gamma \* q\_target(s\_prime, mu\_target(s\_prime))  
—\> 88 q\_loss = F.smooth\_l1\_loss(q(s,a), target.detach())  
89  
90 q\_optimizer.zero\_grad()

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in **call** (self, \*input, \*\*kwargs)  
548 result = self.\_slow\_forward(\*input, \*\*kwargs)  
549 else:  
–\> 550 result = self.forward(\*input, \*\*kwargs)  
551 for hook in self.\_forward\_hooks.values():  
552 hook\_result = hook(self, input, result)

in forward(self, x, a)  
65 def forward(self, x, a):  
66 h1 = F.relu(self.fc\_s(x))  
—\> 67 h2 = F.relu(self.fc\_a(a))  
68 cat = torch.cat([h1,h2], dim=1)  
69 q = F.relu(self.fc\_q(cat))

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in **call** (self, \*input, \*\*kwargs)  
548 result = self.\_slow\_forward(\*input, \*\*kwargs)  
549 else:  
–\> 550 result = self.forward(\*input, \*\*kwargs)  
551 for hook in self.\_forward\_hooks.values():  
552 hook\_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)  
85  
86 def forward(self, input):  
—\> 87 return F.linear(input, self.weight, self.bias)  
88  
89 def extra\_repr(self):

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)  
1608 if input.dim() == 2 and bias is not None:  
1609 # fused op is marginally faster  
-\> 1610 ret = torch.addmm(bias, input, weight.t())  
1611 else:  
1612 output = input.matmul(weight.t())

RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1’ in call to \_th\_addmm

above sentence

---

<div class="post-metadata">

**Author:** ![shlomish3](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@shlomish3](https://discuss.pytorch.org/u/shlomish3)\
**Post date:** [June 30, 2020, 9:19am UTC](https://discuss.pytorch.org/t/error-about-type/87409/2 "2020-06-30T09:19:02Z")

</div>

Apperently you need to type case (or initialize) one of your variables to another, shorter data type.  
As the error indicates, try casting it [torch.float32](https://pytorch.org/docs/stable/tensors.html):

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/3/d/3da688298305c1d719fc91e732cc1335dc424ef0.png)

It’s a bit difficult to follow your code example, so I can’t really tell what variable causes the problem.
