akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
TypeError: ‘Tensor’ object is not callable

username: liguo
cooked: <p>Hello,everyone.<br>
Newbie here, trying to learn pytorch. Recently, I’m using lstm or gru to do failure prediction. This is a binary classification problem ,so I use BCEWithLogitsLoss as the loss function, but finally, it is wrong.</p>
<pre><code class="lang-auto">%matplotlib notebook
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import torch
from torch import nn

data = pd.read_csv('D:/A_PHM_Data/data/train/002/00a22713-68d5-372a-a009-b948ce453442.csv', header=0)
data = data.iloc[:, :-3]
value = data.values.astype(float)

train_x = value[:300].reshape(1, -1, 72)
train_y = np.zeros((train_x.shape[1],1))
train_x = torch.from_numpy(train_x)
train_y = torch.from_numpy(train_y)

class LSTM(nn.Module):
    def __init__(self):
        super(LSTM,self).__init__()
        self.lstm = nn.LSTM(input_size=72, hidden_size=100,batch_first=True)
        self.out = nn.Linear(100,1)
    
    def forward(self, x, h_state, c_state):
        r_out, (h_state, c_state) = self.lstm(x, (h_state, c_state))
        output = self.out(r_out)
#         output = torch.sigmoid(output)
        return output
    
    def InitHidden(self):
        h_state = torch.zeros(1,1,100)
        c_state = torch.zeros(1,1,100)
        return h_state, c_state


device = torch.device('cuda')
model = LSTM().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
loss = torch.nn.BCEWithLogitsLoss()
h_state, c_state = model.InitHidden()
h_state, c_state = h_state.to(device), c_state.to(device)
train_x = train_x.float().to(device)
train_y = train_y.float().to(device)
test_x = test_x.to(device)
test_y = test_y.to(device)

model.train()
for epoch in range(1000):
    output = model(train_x, h_state,c_state).squeeze(0)
    loss = loss(output, train_y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
</code></pre>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
2 for epoch in range(1000):<br>
3     output = model(train_x, h_state,c_state).squeeze(0)<br>
----&gt; 4     loss = loss(output, train_y)<br>
5     optimizer.zero_grad()<br>
6     loss.backward()</p>
<p>TypeError: ‘Tensor’ object is not callable</p>

username: ptrblck
cooked: <p>You are overwriting the loss function <code>loss</code> with the loss value in:</p>
<pre><code class="lang-python">loss = loss(output, train_y)
</code></pre>
<p>Use <code>criterion = nn.BCEWithLogitsLoss</code> or use another name for the loss value and it should work.</p>

username: liguo
cooked: <p>Thanks!!Very appreciate!</p>
Indexing in a tensor

username: ahmedemam576
cooked: <p>Hi there<br>
i have been reading through deep learning with pytorch book, and i have a small question.<br>
in this code we are trying to calculate the mean and std of the pixels to normalize them</p>
<pre><code class="lang-auto">n_channels = batch.shape[1]
for c in range(n_channels):
    mean = torch.mean(batch[:, c])
    std = torch.std(batch[:, c])
    batch[:, c] = (batch[:, c] - mean) / std
</code></pre>
<p>what is the meaning of batch[:, c]  given that the channels have the 2nd dimension, if the channel dimension has another position like the last dimension how can i calculate the mean across the channels??</p>

username: ptrblck
cooked: <p><code>batch[:, c]</code> will index the current channel and <code>torch.mean</code> as well as <code>torch.std</code> will calculate the mean and standard deviation for this channel, respectively.<br>
How should the result of the stats “across the channels” look like, i.e. which shape should it have?<br>
If you want to calculate the mean in the channel dimension, you could use <code>torch.mean(batch, dim=1)</code>.</p>

username: ahmedemam576
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks a lot, that was extremely helpful <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>