Error with local variable 'batch' and optimization.step

Ahmedib: <p>this the first time I use pytorch and I use<br>
python version==3.7.6  , pandas==1.0.3  , numpy==1.18.4  ,sklearn==0.22.2.post1  torch==1.5.0+cpu , matplotlib==3.2.1<br>
I try this code for time series with LSTM<br>
<a href="https://romanorac.github.io/machine/learning/2019/09/27/time-series-prediction-with-lstm.html" rel="nofollow noopener">time series with LSTM Pytorch</a><br>
the problem in this code exactly ( local variable ‘batch’ referenced before assignment ) and do es the  order between  <code>lr_scheduler.step()</code> and  <code>optimizer.step()</code> cause the problem and can I change them</p>
<hr>
<pre><code class="lang-auto">import time
import random

class Optimization:
  
    def __init__(self, model, loss_fn, optimizer, scheduler):
        self.model = model
        self.loss_fn = loss_fn
        self.optimizer = optimizer
        self.scheduler = scheduler
        self.train_losses = []
        self.val_losses = []
        self.futures = []

    @staticmethod
    def generate_batch_data(x, y, batch_size):
        for batch, i in enumerate(range(0, len(x) - batch_size, batch_size)):
            x_batch = x[i : i + batch_size]
            y_batch = y[i : i + batch_size]
            yield x_batch, y_batch, batch

    def train(
        self,
        x_train,
        y_train,
        x_val=None,
        y_val=None,
        batch_size=100,
        n_epochs=15,
        do_teacher_forcing=None,
    ):
        seq_len = x_train.shape[1]
        for epoch in range(n_epochs):
            start_time = time.time()
            self.futures = []

            train_loss = 0
            for x_batch, y_batch, batch in self.generate_batch_data(x_train, y_train, batch_size):
                y_pred = self._predict(x_batch, y_batch, seq_len, do_teacher_forcing)
                self.optimizer.zero_grad()
                loss = self.loss_fn(y_pred, y_batch)
                loss.backward()
                self.optimizer.step()
                train_loss += loss.item()
                
            self.scheduler.step()
            train_loss /= batch
            self.train_losses.append(train_loss)

            self._validation(x_val, y_val, batch_size)

            elapsed = time.time() - start_time
            print(
                "Epoch %d Train loss: %.2f. Validation loss: %.2f. Avg future: %.2f. Elapsed time: %.2fs."
                % (epoch + 1, train_loss, self.val_losses[-1], np.average(self.futures), elapsed)
            )

    def _predict(self, x_batch, y_batch, seq_len, do_teacher_forcing):
        if do_teacher_forcing:
            future = random.randint(1, int(seq_len) / 2)
            limit = x_batch.size(1) - future
            y_pred = self.model(x_batch[:, :limit], future=future, y=y_batch[:, limit:])
        else:
            future = 0
            y_pred = self.model(x_batch)
        self.futures.append(future)
        return y_pred

    def _validation(self, x_val, y_val, batch_size):
        if x_val is None or y_val is None:
            return
        with torch.no_grad():
            val_loss = 0
            for x_batch, y_batch, batch in self.generate_batch_data(x_val, y_val, batch_size):
                y_pred = self.model(x_batch)
                loss = self.loss_fn(y_pred, y_batch)
                val_loss += loss.item()
            val_loss /= batch
            self.val_losses.append(val_loss)

    def evaluate(self, x_test, y_test, batch_size, future=1):
        with torch.no_grad():
            test_loss = 0
            actual, predicted = [], []
            for x_batch, y_batch, batch in self.generate_batch_data(x_test, y_test, batch_size):
                y_pred = self.model(x_batch, future=future)
                y_pred = (
                    y_pred[:, -len(y_batch) :] if y_pred.shape[1] &gt; y_batch.shape[1] else y_pred
                )
                loss = self.loss_fn(y_pred, y_batch)
                test_loss += loss.item()
                actual += torch.squeeze(y_batch[:, -1]).data.cpu().numpy().tolist()
                predicted += torch.squeeze(y_pred[:, -1]).data.cpu().numpy().tolist()
            test_loss /= batch
            return actual, predicted, test_loss

    def plot_losses(self):
        plt.plot(self.train_losses, label="Training loss")
        plt.plot(self.val_losses, label="Validation loss")
        plt.legend()
        plt.title("Losses")`
</code></pre>
<hr>
<pre><code class="lang-auto">model_1 = Model(input_size=1, hidden_size=100, output_size=1)
loss_fn_1 = nn.MSELoss()
optimizer_1 = optim.Adam(model_1.parameters(), lr=1e-3)
scheduler_1 = optim.lr_scheduler.StepLR(optimizer_1, step_size=5, gamma=0.1)
optimization_1 = Optimization(model_1, loss_fn_1, optimizer_1, scheduler_1)
optimization_1.train(x_train, y_train,x_val,y_val,do_teacher_forcing=False)
</code></pre>

ptrblck: <p>This error might be raised, if <code>generate_batch_data</code> doesn’t return anything while you are trying to normalize the loss with <code>batch</code>.</p>
<p>Could you check, if the loop is executed as least once?</p>

Ahmedib: <p>thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> , it works when I change the batch_size in training  to 1 but is it not possible to increase the batch ?<br>
I try 32 or other batches ,gives me same error</p>

ptrblck: <p>It would be possible, if <code>len(x)</code> contains at least <code>batch_size+1</code> samples, which doesn’t seem to be the case.<br>
This loop won’t yield anything for lengths in <code>[0, batch_size]</code>:</p>
<pre><code class="lang-python">batch_size = 32
for i in range(0, 32 - batch_size, batch_size):
    print(i)
</code></pre>

Ahmedib: <p>ok , Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a></p>