Why I am getting "AttributeError: 'Tensor' object has no attribute 'train_img' "

I am trying to write a program for MNIST Digit Recognition. I am taking help from this link Kaggle Link.

When I am training my model it is showing AttributeError: 'Tensor' object has no attribute 'train_img' I am getting the error at the time of print However, the code is full the same and I just changed the variable name like train_img instead of data. My code is given below:

# module packages
from .. import config
from . import preprocess, my_model

# Loding train Data from preprocess.py
train_loader = preprocess.load_train_data()

# Loding Model from my_model.py
model = my_model.get_model()

# Define a Loss function and optimizer
optimizer = optim.Adam(params=model.parameters(), lr=0.003)
criterion = nn.CrossEntropyLoss()

# Train the network
for epoch in range(config.nb_epocs):
    running_loss = 0.0
    for batch_idx, (train_img, train_labels) in enumerate(train_loader):
        # get the inputs; data is a list of [inputs, labels]
        train_img = train_img.unsqueeze(1)
        train_img, train_labels = train_img, train_labels
        # zero the parameter gradients
        # forward + backward + optimize
        output = model(train_img)
        loss = criterion(output, train_labels)
        # print statistics
        running_loss += loss.item()
        if (batch_idx + 1)% 100 == 0:    
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                config.nb_epocs, (batch_idx + 1) * len(train_img), len(train_loader.dataset),
                100. * (batch_idx + 1) / len(train_loader), loss.train_img[0]))
            running_loss = 0.0
print('Finished Training')

Could you tell me what I have to do?

Another thing is, can I do validation_split at the same time of training like Keras model_fit?

loss doesn’t have any attribute name train_img. If you want to get the value of the loss, simply use loss.item()

For splitting you data into train, validation and test, you can use Dataset and DataLoader. Please see Torch.utils.data.dataset.random_split for example.

Thank you Keyv_krmn. I will try this