# RuntimeError: Found dtype Double but expected Float

**URL:** <https://discuss.pytorch.org/t/runtimeerror-found-dtype-double-but-expected-float/122125>\
**Category:** nlp\
**Created:** [May 23, 2021, 1:32pm UTC](https://discuss.pytorch.org/t/runtimeerror-found-dtype-double-but-expected-float/122125 "2021-05-23T13:32:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craine](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/craine/32/38644_2.png) [@craine](https://discuss.pytorch.org/u/craine)\
**Post date:** [May 23, 2021, 1:32pm UTC](https://discuss.pytorch.org/t/runtimeerror-found-dtype-double-but-expected-float/122125/1 "2021-05-23T13:32:47Z")

</div>

I have tried a bunch of things I’ve found on forums to fix this issue but nothing is working. I’m getting this error trying to run my model.

Notebook is public here:

> **[hello-bert-basics-with-commonlit.ipynb](https://drive.google.com/file/d/1akU15oJrzFi9KsY3oyrstmvf9tW_8mpS/view?usp=sharing)**
>
> Colaboratory notebook

ERROR:

## Epoch: 0%| | 0/4 [00:00\<?, ?it/s]

RuntimeError Traceback (most recent call last)  
 in   
30 # Backward pass  
31 torch.set\_default\_dtype(torch.float64)  
—\> 32 loss.backward()  
33 # Update parameters and take a step using the computed gradient  
34 optimizer.step()

1 frames  
/opt/conda/lib/python3.7/site-packages/torch/autograd/ **init**.py in backward(tensors, grad\_tensors, retain\_graph, create\_graph, grad\_variables)  
130 Variable._execution\_engine.run\_backward(  
131 tensors, grad\_tensors_, retain\_graph, create\_graph,  
 → 132 allow\_unreachable=True) # allow\_unreachable flag  
133  
134

RuntimeError: Found dtype Double but expected Float

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 23, 2021, 7:55pm UTC](https://discuss.pytorch.org/t/runtimeerror-found-dtype-double-but-expected-float/122125/2 "2021-05-23T19:55:53Z")

</div>

I guess the error is raised by changing the default `dtype` before calling the `backward` method in:

```python
     31 torch.set_default_dtype(torch.float64)
---> 32 loss.backward()

```

I assume this could create type mismatches, since the forward pass wouldn’t match the backward pass anymore.  
Could you explain, why you are changing the default `dtype` globally during training?

---

<div class="post-metadata">

**Author:** ![craine](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/craine/32/38644_2.png) [@craine](https://discuss.pytorch.org/u/craine)\
**Post date:** [May 23, 2021, 8:24pm UTC](https://discuss.pytorch.org/t/runtimeerror-found-dtype-double-but-expected-float/122125/3 "2021-05-23T20:24:54Z")

</div>

I copied an error from a test.. I was just trying something out.. My mistake. Here is the error:

## Epoch: 0%| | 0/4 [00:00\<?, ?it/s]

RuntimeError Traceback (most recent call last)  
 in   
29 train\_loss\_set.append(loss.item())  
30 # Backward pass  
—\> 31 loss.backward()  
32 # Update parameters and take a step using the computed gradient  
33 optimizer.step()

/opt/conda/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain\_graph, create\_graph)  
219 retain\_graph=retain\_graph,  
220 create\_graph=create\_graph)  
 → 221 torch.autograd.backward(self, gradient, retain\_graph, create\_graph)  
222  
223 def register\_hook(self, hook):

/opt/conda/lib/python3.7/site-packages/torch/autograd/ **init**.py in backward(tensors, grad\_tensors, retain\_graph, create\_graph, grad\_variables)  
130 Variable._execution\_engine.run\_backward(  
131 tensors, grad\_tensors_, retain\_graph, create\_graph,  
 → 132 allow\_unreachable=True) # allow\_unreachable flag  
133  
134

RuntimeError: Found dtype Double but expected Float
