RuntimeError: Expected object of scalar type Double but got scalar type Float

username: mattpats
cooked: <p>I’m trying to get a RNN to predict the next value in a sequence. How can I resolve the error message below?</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
</code></pre>
<p>The data I am using is imported from a CSV. Instead of using the actual data in this post, I’ve created dummy data that is the same data type (np.float64), as the data I’m using. I think the data’s type might be the source of the problem. I’ve tried altering it but keep getting the same error message.</p>
<pre><code class="lang-auto"># create random data that mimics actual data I am using

sampl = np.random.uniform(low=-3, high=3, size=(50,))
sampl.astype(np.float64)
</code></pre>
<pre><code class="lang-auto"># define seq length
seq_length = 10

data_x = sampl.copy()

# number of sequences w/i data
n_seq = len(data_x)//seq_length

# only keep enough data to make full sequences
data_x = data_x[:n_seq * seq_length]

# Number of input data we can have
nbr_input = len(data_x[:-seq_length])

# create feature data and target data
features = []
targets = []

# create data one seq at a time
for i in range(nbr_input):
    features.append(data_x[i:i+seq_length])
    targets.append(data_x[i+seq_length])

# convert data into pytorch tensors
feature_tensor = torch.from_numpy(np.array(features))
target_tensor  = torch.from_numpy(np.array(targets))

# add a last dimension so that data is (batch_size, seq_length, features)
feature_tensor = feature_tensor.unsqueeze(-1)
target_tensor  = target_tensor.unsqueeze(-1)
</code></pre>
<pre><code class="lang-auto"># DEFINE THE RNN

class RNN(nn.Module):
    def __init__(self, input_size, output_size, hidden_dim, n_layers):
        super(RNN, self).__init__()
        self.hidden_dim=hidden_dim
        self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)        
        self.fc = nn.Linear(hidden_dim, output_size)

    def forward(self, x, hidden):
        batch_size = x.size(0)
        r_out, hidden = self.rnn(x, hidden)
        r_out = r_out.view(-1, self.hidden_dim)  
        output = self.fc(r_out)
        return output, hidden
</code></pre>
<pre><code class="lang-auto"># test that dimensions of the model are as expected
test_rnn = RNN(input_size=1, output_size=1, hidden_dim=32, n_layers=2)

print(test_rnn)
</code></pre>
<pre><code class="lang-auto">print('Input size: ', feature_tensor.size())

# test out rnn sizes

test_out, test_h = test_rnn(feature_tensor, None)

print('Output size: ', test_out.size())
print('Hidden state size: ', test_h.size())
</code></pre>
<blockquote>
<p>Input size:  torch.Size([40, 10, 1])</p>
</blockquote>
<blockquote>
<p>RuntimeError: Expected object of scalar type Double but got scalar type Float for argument <span class="hashtag-raw">#3</span> ‘mat2’ in call to _th_addmm_out</p>
</blockquote>

username: InnovArul
cooked: <p>Try to make the model and data to be of same data type by calling <code>.float()</code> on both model and data tensors.</p>
<pre><code class="lang-auto">test_rnn = test_rnn.float()
feature_tensor = feature_tensor.float()
...
test_out, test_h = test_rnn(feature_tensor, None)

...
</code></pre>