Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
train = np.loadtxt('.. /input/digit-recognizer/train.csv', delimiter=',', skiprows=1, dtype=np.uint8)
 train_X = train[:, 1:].reshape(-1, 28, 28, 1)
 train_y = train[:, 0].astype(np.int)
 is_dev = np.arange(train_X.shape[0])% 10 == 0
dev_X = train_X[is_dev]
dev_y = train_y[is_dev]
train_X = train_X[~is_dev]
train_y = train_y[~is_dev]
X_mean = train_X.astype(np.float32 ).mean() / 255
X_std = train_X.astype(np.float32 ).std() / 255
test_X = np.loadtxt('.. /input/digit-recognizer/test.csv', delimiter=',', skiprows=1, dtype=np.uint8)
 test_X = test_X.reshape(-1, 28, 28, 1)
 print(X_mean, X_std )
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 42
seed_everything(seed)
 warnings.filterwarnings('ignore' )<define_variables>
class TransformDataset(Dataset):
def __init__(self, transform, X, y=None):
self.transform = transform
self.X = X
self.y = y
def __len__(self):
return self.X.shape[0]
def __getitem__(self, index):
X_item = self.transform(self.X[index])
 if self.y is None:
return X_item,
else:
return X_item, self.y[index]
train_transform = Compose([
ToPILImage() ,
Pad(2),
RandomAffine(degrees=30, translate=(0.2, 0.2), shear=0.2),
ToTensor() ,
Normalize(( X_mean,),(X_std,)) ,
RandomErasing()
])
 test_transform = Compose([
ToPILImage() ,
Pad(2),
ToTensor() ,
Normalize(( X_mean,),(X_std,))
])
 train_loader = DataLoader(TransformDataset(train_transform, train_X, train_y), batch_size=100, shuffle=True)
 dev_loader = DataLoader(TransformDataset(test_transform, dev_X, dev_y), batch_size=400, shuffle=False)
 test_loader = DataLoader(TransformDataset(test_transform, test_X), batch_size=400, shuffle=False )
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 TRAIN_DATA_DIR = 'rfcx-audio-detection'
TRAIN_GCS_PATH = KaggleDatasets().get_gcs_path(TRAIN_DATA_DIR)
 TEST_DATA_DIR = 'rfcx-species-audio-detection'
TEST_GCS_PATH = KaggleDatasets().get_gcs_path(TEST_DATA_DIR)
 TEST_FILES = tf.io.gfile.glob(TEST_GCS_PATH + '/tfrecords/test/*.tfrec')
 FILENAMES = tf.io.gfile.glob(TEST_GCS_PATH + '/tfrecords/train/*.tfrec')
 no_of_training_samples = count_data_items(FILENAMES)
 print('num_training_samples are', no_of_training_samples)
<data_type_conversions>
model = resnet18(num_classes=10)
 model.conv1 = nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2, bias=False)
 nn.init.kaiming_normal_(model.conv1.weight, mode='fan_out', nonlinearity='relu')
 model.maxpool = nn.Identity()
criterion = nn.CrossEntropyLoss()
<define_variables>
max_dev_acc = 0
optimizer = torch.optim.SGD(model.parameters() , lr=0.1, momentum=0.9)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', verbose=True, patience=1, factor=0.1**0.5)
 for epoch in tqdm(range(20), desc='Epochs'):
train_loss = train_epoch(model, criterion, optimizer, train_loader)
 acc = classification(model, dev_loader)
 scheduler.step(acc)
 print('trainLoss=%.4f' % train_loss, 'devAcc=%.4f' % acc)
 if acc > max_dev_acc:
max_dev_acc = acc
torch.save(model.state_dict() , 'model.pt' )
<define_variables><EOS> 
model.load_state_dict(torch.load('model.pt'))
y_pred = classification(model, test_loader)
 with open('submission.csv', 'w')as f:
print('ImageId,Label', file=f)
 for i, y in enumerate(y_pred):
print(i + 1, y, sep=',', file=f )