Hi, I am working on Age and Gender prediction. I get this error "AttributeError: ‘tuple’ object has no attribute ‘size’ ".
‘’’
Combine Gender and Age
y_gender_age = np.stack((y_gender, y_age), axis=1)
y_gender_age[0:5]
array([[ 0, 100],
[ 0, 100],
[ 1, 100],
[ 1, 100],
[ 1, 100]], dtype=int64)
type(y_gender_age)
numpy.ndarray
X_train, X_test, y_train, y_test = train_test_split(X, y_gender_age, test_size=0.3, random_state=42, shuffle=True)
‘’’
Building Model
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv2d(in_channels = 128, out_channels = 32,
kernel_size = 1)
self.pool = nn.MaxPool2d(kernel_size = 1)
self.conv2 = nn.Conv2d(in_channels = 32, out_channels = 64,
kernel_size = 1)
self.conv3 = nn.Conv2d(in_channels = 64, out_channels = 128,
kernel_size = 1)
self.conv4 = nn.Conv2d(in_channels = 128, out_channels = 256,
kernel_size = 1)
nn.Flatten()
self.fc1 = nn.Linear(in_features = 32768, out_features = 256)
self.fc2 = nn.Linear(in_features = 256, out_features = 100)
self.linear1 = nn.Linear(in_features = 100, out_features = 1) # age_out
self.linear2 = nn.Linear(in_features = 100, out_features = 2) # gender_out
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = x.view(x.size(0),-1)
x = self.dropout(F.relu(self.fc1(x)))
x = self.dropout(F.relu(self.fc2(x)))
output1 = F.relu(self.linear1(x)) # age_output1
output2 = F.sigmoid(self.linear2(x)) # gender_output2
return output1, output2
import torch.optim as optim
For multilabel output: age
criterion1_mae_age = nn.L1Loss()
for binary output : Gender
criterion2_binary_gender = nn.BCELoss()
optimizer = optim.SGD(net.parameters(), lr = 0.001)
def train_model(model, criterion1, criterion2, optimizer, n_epochs = 25):
“”" returns trained model “”"
# initialize tracker for minimum validation loss
# criterion1 is for age mean_absolute_error, cricriterion2 is for Binary_crossentropy_loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
train_loss = 0.0
valid_loss = 0.0
# train the model #
net.train()
for i, data in enumerate(trainloader):
image, labels = data
optimizer.zero_grad() # zero the parameter gradient
output = net(image.float())
output = net(image.float())
#label1_hat = output['label1']
#label2_hat = output['label2']
# calculate loss
loss1 = criterion1(output, labels.squeeze().type(torch.LongTensor))
loss2 = criterion2(output, labels)
loss = loss1 + loss2
# back prop
loss.backward()
#grad
optimizer.step()
train_loss = train_loss + ((1 / (i + 1)) * (loss.data - train_loss))
if i % 50 == 0:
print('Epoch %d, Batch %d loss: %.6f' %
(epoch, i + 1, train_loss))
net_conv = train_model(net, criterion1_mae_age, criterion2_binary_gender, optimizer)
‘’’
Please help me where did I wrote wrong code.