Kaggle competition: QSTP - Deep Learning 2019
<SOS> metric: categorizationaccuracy Kaggle data source: qstp-deep-learning-2019<groupby>
%matplotlib inline
user_agg = train_df.groupby('user_id')[target].agg(['sum', 'count'])
 content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count'] )<data_type_conversions>
df = pd.read_csv('.. /input/train.csv')
 df.head()
for f in ['prior_question_elapsed_time']:
train_df[f] = pd.to_numeric(train_df[f], downcast='float' )<categorify>
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):

 self.data_frame = pd.read_csv(csv_file)
 self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
 def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.data_frame['Id'][idx])
 image = Image.open(img_name ).convert('RGB')
 label = np.array(self.data_frame['Category'][idx])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample
%%time
train_df['residual'] = train_df[target] - train_df['content_id'].map(content_agg['sum'] / content_agg['count'])
 residual_agg = train_df.groupby('user_id')['residual'].agg(['sum'] )<data_type_conversions>
transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.ToTensor()
])
 trainset = ImageDataset(csv_file = '.. /input/train.csv', root_dir = '.. /input/data/data/', transform=transform)
 trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=0 )
prior_question_elapsed_time_agg = train_df.groupby('user_id' ).agg({'prior_question_elapsed_time': ['sum', lambda x: len(x)]})
 prior_question_elapsed_time_agg.columns = ['sum', 'count']
prior_question_elapsed_time_agg['count'] = prior_question_elapsed_time_agg['count'].astype('int32')
 prior_question_elapsed_time_agg.info()<categorify>
for i in range(len(trainset)) :
sample = trainset[i]
print(i, sample[0].size() , " | Label: ", sample[1])
 if i == 9:
break
train_df['user_prior_question_elapsed_time_diff'] =(train_df[target] - train_df['user_id'].map(user_agg['sum'] / user_agg['count'])) *(train_df['prior_question_elapsed_time'] - train_df['user_id'].map(prior_question_elapsed_time_agg['sum'] / prior_question_elapsed_time_agg['count']))
user_prior_question_elapsed_time_diff_agg = train_df.groupby('user_id')['user_prior_question_elapsed_time_diff'].agg(['sum'])
 train_df['user_prior_question_elapsed_time_diff_mean'] = train_df['user_id'].map(user_prior_question_elapsed_time_diff_agg['sum'] / user_agg['count'] )<define_variables>
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...' )
USER_TRIES = 70
VALID_TRIES = math.ceil(USER_TRIES / 10 )<groupby>
model = models.resnext101_32x8d(pretrained=True )
train_df = train_df.groupby('user_id' ).tail(USER_TRIES ).reset_index(drop=True )<load_from_csv>
for param in model.parameters() :
param.requires_grad = True
for param in model.parameters() :
param.requires_grad = True
for name, child in model.named_children() :
if name in ['layer3', 'layer4', 'fc']:
print(name + ' is unfrozen')
 for param in child.parameters() :
param.requires_grad = True
else:
print(name + ' is frozen')
 for param in child.parameters() :
param.requires_grad = False
data_types_dict = {'question_id': 'int16', 'part': 'int8', 'bundle_id': 'int16', 'tags': 'string'}
questions_df = pd.read_csv(
 path/'riiid-test-answer-prediction/questions.csv',
usecols=data_types_dict.keys() ,
dtype=data_types_dict
)<feature_engineering>
model.fc = nn.Sequential(
 nn.Linear(2048, 4096),
nn.ReLU() ,
nn.Linear(4096, 256),
nn.ReLU() ,
nn.Linear(256, 67),
nn.LogSoftmax(dim=1))
unique_tags_combos_keys = {v:i for i,v in enumerate(questions_df['tags'].unique())}
questions_df['tags_encoded'] = questions_df['tags'].apply(lambda x : unique_tags_combos_keys[x])
 questions_df['tags_encoded'] = pd.to_numeric(questions_df['tags_encoded'], downcast='integer')
 questions_df.info()<feature_engineering>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=0.0006, momentum=0.9)
def extract_tag_factory(tag_pos):
def extract_tag(x):
if isinstance(x, str)and tag_pos < len(x.split()):
splits = x.split()
splits.sort()
return int(splits[tag_pos])
 else:
return 255
return extract_tag
for i in range(0, 3):
questions_df[f'tag_{i + 1}'] = questions_df['tags'].apply(extract_tag_factory(i))
questions_df[f'tag_{i + 1}'] = questions_df[f'tag_{i + 1}'].astype('uint8' )<merge>
epochs=20
for e in range(epochs):
running_loss = 0
corr = torch.tensor(( 0), dtype=torch.int64)
 for images, labels in trainloader:
images = images.cuda()
labels=labels.cuda()
optimizer.zero_grad()
output = model(images)
 _, out = torch.max(output, 1)
 loss = criterion(output, labels)
 loss.backward()
corr+=sum(out==labels)
 optimizer.step()
running_loss += loss.item()
else:
print("Epoch {} - Training loss: {}".format(e, running_loss/len(trainloader)))
 print(corr,"/10934")
train_df = pd.merge(train_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 train_df.drop(columns=['question_id'], inplace=True )<categorify>
model.eval()
train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_id'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] )<feature_engineering>
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()
train_df['prior_question_elapsed_time_mean'] = train_df['user_id'].map(prior_question_elapsed_time_agg['sum'] / prior_question_elapsed_time_agg['count'] )<feature_engineering>
testset = ImageDataset(csv_file = '.. /input/sample_sub.csv', root_dir = '.. /input/data/data/', transform=transform)
 testloader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=False, num_workers=0 )
train_df['residual_user_mean'] = train_df['user_id'].map(residual_agg['sum'] / user_agg['count'] )<data_type_conversions>
predictions=[]
for data, target in testloader:
if train_on_gpu:
data, target = data.cuda() , target.cuda()
output = model(data)
 _, pred = torch.max(output, 1)
 for i in range(len(pred)) :
predictions.append(int(pred[i]))
train_df['prior_question_elapsed_time'].fillna(train_df['prior_question_elapsed_time'].mean() , inplace=True)
 train_df['user_correctness'].fillna(train_df['user_correctness'].mean() , inplace=True )<data_type_conversions>
submission['Category'] = predictions
<feature_engineering><EOS> 
submission.to_csv('submission1.csv', index=False, encoding='utf-8')