Predictions using Logistic Regression in Pytorch return infinity
I started watching a tutorial on PyTorch and I am learning the concept of logistic regression.
I tried it using some stock data that I had. I have inputs, which contains two parameters trade_quantity and trade_value, and targets which has the corresponding stock price.
inputs = torch.tensor([[182723838.00, 2375432.00],
                       [185968153.00, 2415558.00],
                       [181970093.00, 2369140.00],
                       [221676832.00, 2811589.00],
                       [339785916.00, 4291782.00],
                       [225855390.00, 2821301.00],
                       [151430199.00, 1889032.00],
                       [122645372.00, 1552998.00],
                       [129015052.00, 1617158.00],
                       [121207837.00, 1532166.00],
                       [139554705.00, 1789392.00]])

targets = torch.tensor([[76.90],
                        [76.90],
                        [76.90],
                        [80.70],
                        [78.95],
                        [79.60],
                        [80.05],
                        [78.90],
                        [79.40],
                        [78.95],
                        [77.80]])

I defined the model function, the loss as the mean square error, and tried to run it a few times to get some predictions. Here's the code:
def model(x):
    return x @ w.t() + b

def mse(t1, t2):
    diff = t1 - t2
    return torch.sum(diff * diff) / diff.numel()

preds = model(inputs)
loss = mse(preds, targets)
loss.backward()
with torch.no_grad():
    w -= w.grad * 1e-5
    b -= b.grad * 1e-5
    w.grad.zero_()
    b.grad.zero_()

I am using Jupyter for this and ran the last part of the code a few times, after which the predictions come as:
tensor([[inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf],
        [inf]], grad_fn=&lt;AddBackward0&gt;)

If I run it for a few more times the predictions become nan. Can you please tell me why is this happening?

To me, this looks more like linear regression than logistic regression. You are trying to fit a linear model onto your data. It's different to a binary classification task where you would need to use a special kind of activation function (a sigmoid for instance) so that the output is either 0 or 1.
In this particular instance you want to solve a 2D linear problem given input x of shape (batch, x1, x2) (where x1 is trade_quantity and x2 is trade_value) and target (batch, y) (y being the stock_price).
So the objective is to find the best w and b matrices (weight matrix and bias column) so that x@w + b is the closest to y as possible, according to your criterion, the mean square error.

I would recommend normalizing your data so it stays in a [0, 1] range. You can do so by measuring the mean and standard deviation of inputs and targets.
inputs_min, inputs_max = inputs.min(axis=0).values, inputs.max(axis=0).values
targets_min, targets_max = targets.min(axis=0).values, targets.max(axis=0).values

Then applying the transformation:
x = (inputs - inputs_min)/(inputs_max - inputs_min)
y = (targets - targets_min)/(targets_max - targets_min)

Try changing your learning rate and have it run for multiple epochs.
lr = 1e-2
for epochs in range(100):
    preds = model(x)
    loss = mse(preds, y)
    loss.backward()
    with torch.no_grad():
        w -= lr*w.grad
        b -= lr*b.grad
        w.grad.zero_()
        b.grad.zero_()

I use a (1, 2) randomly initialized matrix for w (and a (1,) matrix for b):
w = torch.rand(1, 2)
w.requires_grad = True
b = torch.rand(1)
b.requires_grad = True

And got the following train loss over 100 epochs:

To find the right hyperparameters, it's better to have a validation set. This set will get normalized with the mean and std from the train set. It will be used to evaluate the performances at the end of each epoch on data that is 'unknown' to the model. Same goes for your test set, if you have one.
