Kaggle competition: QSTP - Deep Learning 2019
<SOS> metric: categorizationaccuracy Kaggle data source: qstp-deep-learning-2019<prepare_output>
%matplotlib inline
sample_sub['SalePrice']=final_pred<save_to_csv>
df = pd.read_csv('.. /input/train.csv')
 df.head()
sample_sub.to_csv('sample_submission.csv',index=False )<install_modules>
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.df = pd.read_csv(csv_file)
 self.dir = root_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_path = os.path.join(self.dir, self.df['Id'][index])
 image = Image.open(img_path ).convert('RGB')
 label = np.array(self.df['Category'][index])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1<install_modules>
gpu = torch.cuda.is_available()
if gpu:
print("CUDA is available.Training on GPU")
 else:
print("CUDA unavailable.Training on CPU" )
!pip install /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.1-py3-none-any.whl<import_modules>
model = models.vgg19(pretrained=True)
 model.classifier = nn.Sequential(nn.Linear(512 * 7 * 7, 4096),
nn.LeakyReLU() ,
nn.Dropout() ,
nn.Linear(4096, 512),
nn.LeakyReLU() ,
nn.Dropout() ,
nn.Linear(512, 67), 
)
 model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters() , lr=0.001, momentum=0.9)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1 )
from collections import defaultdict
import datatable as dt
import statsmodels.api as sm
from sklearn.metrics import roc_auc_score
from matplotlib import pyplot as plt
import riiideducation
from pathlib import Path
import seaborn as sns
from pytorch_tabnet.tab_model import TabNetClassifier<define_variables>
def train_model(model, criterion, optimizer, scheduler, num_epochs=20):
since = time.time()
best_acc = 0.0
best_model_state = copy.deepcopy(model.state_dict())
 for epoch in range(num_epochs):
epoch += 1
print("Epoch : {}/{}".format(epoch, num_epochs))
print("-"*10)
 for phase in ['train', 'eval']:
if phase is 'train':
scheduler.step()
model.train()
else:
model.eval()
running_loss = 0.0
running_correct = 0
for inputs, labels in trainloader:
if gpu:
inputs = Variable(inputs.cuda())
 labels = Variable(labels.cuda())
 else:
inputs = Variable(inputs)
 labels = Variable(labels)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase is 'train'):
outputs = model(inputs)
 _, predicted = torch.max(outputs.data, 1)
 loss = criterion(outputs, labels)
 if phase is 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
 running_correct += torch.sum(predicted.cpu() == labels.cpu())
 epoch_loss = running_loss / len(trainset)
 epoch_acc = running_correct.double() / len(trainset)
 print("{}: Loss: {:.4f}, Accuracy: {:.4f}".format(phase, epoch_loss, epoch_acc))
if phase == 'eval' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_state = copy.deepcopy(model.state_dict())
 print()
time_elapsed = time.time() - since
print("Training finished in {} seconds!".format(time_elapsed))
print("Best eval accuracy: {:.4f}".format(100*best_acc))
model.load_state_dict(best_model_state)
 return model
path = Path('/kaggle/input')
 assert path.exists()<define_variables>
model = train_model(model, criterion, optimizer, scheduler, 5 )
%%time
data_types_dict = {
'user_id': 'int32',
'content_id': 'int16',
'answered_correctly': 'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'bool'
}
target = 'answered_correctly'
train_df = dt.fread(path/"riidtrainjay/train.jay", columns=set(data_types_dict.keys())).to_pandas()<drop_column>
testset = ImageDataset(csv_file='.. /input/sample_sub.csv', root_dir='.. /input/data/data/', transform=transform)
 testloader = DataLoader(testset, batch_size=32, num_workers=4, shuffle=False )
%%time
train_df = train_df[train_df[target] != -1].reset_index(drop=True)
 train_df.drop(columns=['timestamp'], inplace=True )<data_type_conversions>
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()
train_df['prior_question_had_explanation'].fillna(False, inplace=True)
 train_df['prior_question_had_explanation'] = train_df['prior_question_had_explanation'].astype('uint8' )<groupby>
predictions = []
for inputs, labels in testloader:
if gpu:
inputs = Variable(inputs.cuda())
 else:
inputs = Variable(inputs)
 outputs = model(inputs)
 _, predicted = torch.max(outputs.data, 1)
 for i in range(len(predicted)) :
predictions.append(int(predicted[i]))
submission['Category'] = predictions
<drop_column><EOS> 
submission.to_csv('VGG19_submission1.csv', index=False, encoding='utf-8' )