TypeError: Train() takes 1 positional argument but 2 were given when trying to call model.eval() after loading model

Edi_Lacic: <p>After having trained and saved my model, I load it by calling<br>
PATH = xyz<br>
model_load = torch.load(PATH)<br>
model_load.eval()</p>
<p>The following is how my class and functions are defined:</p>
<p><strong>class DA_rnn(nn.Module):</strong><br>
**    “”“da_rnn.”""**</p>
<p>**    def <strong>init</strong>(self, X, y, T,**<br>
**                 encoder_num_hidden,**<br>
**                 decoder_num_hidden,**<br>
**                 batch_size,**<br>
**                 learning_rate,**<br>
**                 epochs,**<br>
**                 parallel=False):**<br>
**        “”“da_rnn initialization.”""**<br>
**        super(DA_rnn, self).<strong>init</strong>()**<br>
**        self.encoder_num_hidden = encoder_num_hidden**<br>
**        self.decoder_num_hidden = decoder_num_hidden**<br>
**        self.learning_rate = learning_rate**<br>
**        self.batch_size = batch_size**<br>
**        self.parallel = parallel**<br>
**        self.shuffle = False**<br>
**        self.epochs = epochs**<br>
**        self.T = T**<br>
**        self.X = X**<br>
**        self.y = y**</p>
<p>**        self.device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’)**<br>
**        print("==&gt; Use accelerator: ", self.device)**</p>
<p>**        self.Encoder = Encoder(input_size=X.shape[1],**<br>
**                               encoder_num_hidden=encoder_num_hidden,**<br>
**                               T=T).to(self.device)**<br>
**        self.Decoder = Decoder(encoder_num_hidden=encoder_num_hidden,**<br>
**                               decoder_num_hidden=decoder_num_hidden,**<br>
**                               T=T).to(self.device)**</p>
<p>**        # Loss function**<br>
**        self.criterion = nn.MSELoss()**</p>
<p>**        if self.parallel:**<br>
**            self.encoder = nn.DataParallel(self.encoder)**<br>
**            self.decoder = nn.DataParallel(self.decoder)**</p>
<p>**        self.encoder_optimizer = optim.Adam(params=filter(lambda p: p.requires_grad,**<br>
**                                                          self.Encoder.parameters()),**<br>
**                                            lr=self.learning_rate)**<br>
**        self.decoder_optimizer = optim.Adam(params=filter(lambda p: p.requires_grad,**<br>
**                                                          self.Decoder.parameters()),**<br>
**                                            lr=self.learning_rate)**</p>
<p>**        # Training set**<br>
**        self.train_timesteps = int(self.X.shape[0] * 0.7)**<br>
**        self.y = self.y - np.mean(self.y[:self.train_timesteps])  # Normalize**<br>
**        self.input_size = self.X.shape[1]**</p>
<pre><code>**def train(self):**
    **"""training process."""**
</code></pre>
<p>**        iter_per_epoch = int(np.ceil(self.train_timesteps * 1. / self.batch_size))**<br>
**        self.iter_losses = np.zeros(self.epochs * iter_per_epoch)**<br>
**        self.epoch_losses = np.zeros(self.epochs)**</p>
<p>**        n_iter = 0**</p>
<p>**        for epoch in range(self.epochs):**<br>
**            if self.shuffle:**<br>
**                ref_idx = np.random.permutation(self.train_timesteps - self.T)**<br>
**            else:**<br>
**                ref_idx = np.array(range(self.train_timesteps - self.T))**</p>
<p>**            idx = 0**</p>
<p>**            while (idx &lt; self.train_timesteps):**<br>
**                # get the indices of X_train**<br>
**                indices = ref_idx[idx:(idx + self.batch_size)]**<br>
**                # x = np.zeros((self.T - 1, len(indices), self.input_size))**<br>
**                x = np.zeros((len(indices), self.T - 1, self.input_size))**<br>
**                y_prev = np.zeros((len(indices), self.T - 1))**<br>
**                y_gt = self.y[indices + self.T]**</p>
<p>**                # format x into 3D tensor**<br>
**                for bs in range(len(indices)):**<br>
**                    x[bs, :, :] = self.X[indices[bs]:(indices[bs] + self.T - 1), :]**<br>
**                    y_prev[bs, :] = self.y[indices[bs]: (indices[bs] + self.T - 1)]**</p>
<p>**                loss = self.train_forward(x, y_prev, y_gt)**<br>
**                self.iter_losses[int(epoch * iter_per_epoch + idx / self.batch_size)] = loss**</p>
<p>**                idx += self.batch_size**<br>
**                n_iter += 1**</p>
<p>**                if n_iter % 10000 == 0 and n_iter != 0:**<br>
**                    for param_group in self.encoder_optimizer.param_groups:**<br>
**                        param_group[‘lr’] = param_group[‘lr’] * 0.9**<br>
**                    for param_group in self.decoder_optimizer.param_groups:**<br>
**                        param_group[‘lr’] = param_group[‘lr’] * 0.9**</p>
<p>**                self.epoch_losses[epoch] = np.mean(self.iter_losses[range(**<br>
**                    epoch * iter_per_epoch, (epoch + 1) * iter_per_epoch)])**</p>
<p>**            if epoch % 10 == 0:**<br>
**                print("Epochs: ", epoch, " Iterations: ", n_iter,**<br>
**                      " Loss: ", self.epoch_losses[epoch])**</p>
<p>**            if epoch % 10 == 0:**<br>
**                y_train_pred = self.test(on_train=True)**<br>
**                y_test_pred = self.test(on_train=False)**<br>
**                y_pred = np.concatenate((y_train_pred, y_test_pred))**<br>
**                plt.ioff()**<br>
**                plt.figure()**<br>
**                plt.plot(range(1, 1 + len(self.y)), self.y, label=“True”)**<br>
**                plt.plot(range(self.T, len(y_train_pred) + self.T),**<br>
**                         y_train_pred, label=‘Predicted - Train’)**<br>
**                plt.plot(range(self.T + len(y_train_pred), len(self.y) + 1),**<br>
**                         y_test_pred, label=‘Predicted - Test’)**<br>
**                plt.legend(loc=‘upper left’)**<br>
**                plt.show()**</p>
<p>**            # # Save files in last iterations**<br>
**            # if epoch == self.epochs - 1:**<br>
**            #     np.savetxt(’…/loss.txt’, np.array(self.epoch_losses), delimiter=’,’)**<br>
**            #     np.savetxt(’…/y_pred.txt’,**<br>
**            #                np.array(self.y_pred), delimiter=’,’)**<br>
**            #     np.savetxt(’…/y_true.txt’,**<br>
**            #                np.array(self.y_true), delimiter=’,’)**</p>
<p>and then it throws the following error:<br>
“TypeError: train() takes 1 positional argument but 2 were given”</p>

ptrblck: <p>Could you repost the formatted code by wrapping it into three backticks ``` or by clicking on the “Preformatted text” button in the text window?<br>
The current code is quite hard to read and also doesn’t seem to show the actual <code>model.train()</code> call.</p>