Dtype error expected long but got float

username: Angry_potato
cooked: <p>Hi<br>
I am trying to implement an Classifier but I got the following error. I have no idea why this is happening<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/c/3c02b2121905195874d99bea30466a2804aad5b6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/3c02b2121905195874d99bea30466a2804aad5b6" title="1585307759132408411466"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/c/3c02b2121905195874d99bea30466a2804aad5b6_2_666x500.jpeg" alt="1585307759132408411466" data-base62-sha1="8ySqfVFmzv4l9yfpL40kWUbL2F8" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/c/3c02b2121905195874d99bea30466a2804aad5b6_2_666x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/c/3c02b2121905195874d99bea30466a2804aad5b6_2_999x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/c/3c02b2121905195874d99bea30466a2804aad5b6_2_1332x1000.jpeg 2x" data-dominant-color="73736D"><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">1585307759132408411466</span><span class="informations">2304×1728 639 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b593ff9b416eb390140972c9ff999116e9df97a8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b593ff9b416eb390140972c9ff999116e9df97a8" title="1585307790003819982334"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b593ff9b416eb390140972c9ff999116e9df97a8_2_666x500.jpeg" alt="1585307790003819982334" data-base62-sha1="pUjvsqwp9DG3LVuEzSgDUj5tmIw" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b593ff9b416eb390140972c9ff999116e9df97a8_2_666x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b593ff9b416eb390140972c9ff999116e9df97a8_2_999x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b593ff9b416eb390140972c9ff999116e9df97a8_2_1332x1000.jpeg 2x" data-dominant-color="797871"><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">1585307790003819982334</span><span class="informations">2304×1728 1.12 MB</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>

username: ptrblck
cooked: <p>Could you check the dtype of <code>input</code> via <code>print(input.type())</code>?<br>
I guess it might be a <code>LongTensor</code> and you can transform it via <code>input = input.float()</code>.</p>
<p>PS: It’s better to post code directly by wrapping it into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Angry_potato
cooked: <p>Thank you for the reply. I found the solution on another website.<br>
Simply change all the parameters of model to float by using net.float() before loss and convert the input to float.</p>

username: braindotai
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Could you elaborate why the model’s dtype is initialized to long even though he didn’t specify anywhere?<br>
Thanks</p>

username: Angry_potato
cooked: <p>Might be default.Not sure though</p>

username: ptrblck
cooked: <p>I don’t think the model’s parameters are initialized to long, but rather the <code>input</code>.<br>
<code>torch.from_numpy()</code> respects the data type passed to this method and often we see issues, when the original numpy array contained int values.</p>

username: braindotai
cooked: <p>Okay, got your point. Thanks</p>

username: techboy
cooked: <p>Could somebody help me. I am new to Pytorch and am trying to predict house prices. I am always getting the following error: Found dtype Long but expected Float.<br>
My Notebook: <a href="https://colab.research.google.com/drive/1Vd9fJs0bfd7IZ8D5dHS859EThz1DEB58?usp=sharing" rel="noopener nofollow ugc">https://colab.research.google.com/drive/1Vd9fJs0bfd7IZ8D5dHS859EThz1DEB58?usp=sharing</a></p>
<pre><code class="lang-auto">#Doesn't Work
epochs=500
final_losses=[]
for i in range(epochs):
    i=i+1
    y_pred=model.forward(X_train)
    y_pred=y_pred#.to(torch.float32)#.float()
    #print(y_pred.type())
    loss=loss_function(y_pred,y_train)#.float()
    #print("Loss: ",loss.type())
    final_losses.append(loss)
    if i%10==1:
        print("Epoch number: {} and the loss : {}".format(i,loss.item()))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
</code></pre>
<p>Any help would be highly appreciated.</p>

username: Angry_potato
cooked: <p>Hi techboy,<br>
What you should do to fix this error is to convert the tensor which is in dtype Long to float. You can figure out which tensor is causing error by looking out the line which gives error while execution.</p>

username: techboy
cooked: <p>Thank you very much. I had forgotten to change y_train and y_test to float.</p>