Kaggle competition: QSTP - Deep Learning 2019
<SOS> metric: categorizationaccuracy Kaggle data source: qstp-deep-learning-2019<data_type_conversions>
!pip install gdown
!gdown https://drive.google.com/uc?id=1-2ayU2W8YnVgvfHEukbpT9-HyrLF5vXt
train_test['MSSubClass'] = train_test['MSSubClass'].apply(str)
 train_test['YrSold'] = train_test['YrSold'].apply(str)
 train_test['MoSold'] = train_test['MoSold'].apply(str)
 train_test['Functional'] = train_test['Functional'].fillna('Typ')
 train_test['Electrical'] = train_test['Electrical'].fillna("SBrkr")
 train_test['KitchenQual'] = train_test['KitchenQual'].fillna("TA")
 train_test['Exterior1st'] = train_test['Exterior1st'].fillna(train_test['Exterior1st'].mode() [0])
 train_test['Exterior2nd'] = train_test['Exterior2nd'].fillna(train_test['Exterior2nd'].mode() [0])
 train_test['SaleType'] = train_test['SaleType'].fillna(train_test['SaleType'].mode() [0])
 train_test["PoolQC"] = train_test["PoolQC"].fillna("None")
 train_test["Alley"] = train_test["Alley"].fillna("None")
 train_test['FireplaceQu'] = train_test['FireplaceQu'].fillna("None")
 train_test['Fence'] = train_test['Fence'].fillna("None")
 train_test['MiscFeature'] = train_test['MiscFeature'].fillna("None")
 for col in('GarageArea', 'GarageCars'):
train_test[col] = train_test[col].fillna(0)
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
train_test[col] = train_test[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
train_test[col] = train_test[col].fillna('None')
 for col in train_test:
if train_test[col].isna().sum() > 0:
print(train_test[col][0] )<features_selection>
!pip install torchsummary
useless = ['GarageYrBlt','YearRemodAdd']
train_test = train_test.drop(useless, axis = 1)
 def impute_knn(df):
ttn = train_test.select_dtypes(include=[np.number])
 ttc = train_test.select_dtypes(exclude=[np.number])
 cols_nan = ttn.columns[ttn.isna().any() ].tolist()
cols_no_nan = ttn.columns.difference(cols_nan ).values
for col in cols_nan:
imp_test = ttn[ttn[col].isna() ]
imp_train = ttn.dropna()
model = KNeighborsRegressor(n_neighbors=5)
 knr = model.fit(imp_train[cols_no_nan], imp_train[col])
 ttn.loc[ttn[col].isna() , col] = knr.predict(imp_test[cols_no_nan])
 return pd.concat([ttn,ttc],axis=1)
 train_test = impute_knn(train_test)
 objects = []
for i in train_test.columns:
if train_test[i].dtype == object:
objects.append(i)
 train_test.update(train_test[objects].fillna('None'))
for col in train_test:
if train_test[col].isna().sum() > 0:
print(train_test[col][0] )<feature_engineering>
%matplotlib inline
train_test["SqFtPerRoom"] = train_test["GrLivArea"] /(train_test["TotRmsAbvGrd"] +
train_test["FullBath"] +
train_test["HalfBath"] +
train_test["KitchenAbvGr"])
 train_test['Total_Home_Quality'] = train_test['OverallQual'] + train_test['OverallCond']
train_test['Total_Bathrooms'] =(train_test['FullBath'] +(0.5 * train_test['HalfBath'])+
train_test['BsmtFullBath'] +(0.5 * train_test['BsmtHalfBath']))
train_test["HighQualSF"] = train_test["1stFlrSF"] + train_test["2ndFlrSF"]
train_test['MSSubClass'] = train_test['MSSubClass'].apply(str)
 train_test['YrSold'] = train_test['YrSold'].apply(str)
 train_test['MoSold'] = train_test['MoSold'].apply(str)
 train_test_dummy = pd.get_dummies(train_test)
 numeric_features = train_test_dummy.dtypes[train_test_dummy.dtypes != object].index
skewed_features = train_test_dummy[numeric_features].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skewed_features[skewed_features > 0.5]
skew_index = high_skew.index
for i in skew_index:
train_test_dummy[i] = np.log1p(train_test_dummy[i] )<import_modules>
torch.manual_seed(1337)
 torch.cuda.manual_seed(1337)
 np.random.seed(1337)
 random.seed(1337 )
import shap
import xgboost as xgb
from catboost import Pool
from sklearn.svm import SVR
from catboost import CatBoostRegressor
from lightgbm import LGBMRegressor
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeRegressor
from mlxtend.regressor import StackingRegressor
from sklearn.linear_model import LinearRegression, BayesianRidge
from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import KFold, cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.metrics import mean_squared_error, mean_absolute_error, mean_squared_log_error<compute_train_metric>
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
train = train_test_dummy[0:2930]
test = train_test_dummy[2930:]
test['Id'] = test_id
def rmse(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def cv_rmse(model):
rmse = np.sqrt(-cross_val_score(model, train, target_log, scoring="neg_mean_squared_error", cv=kf))
return(rmse )<choose_model_class>
class FocalLoss(nn.Module):
def __init__(self, num_class, alpha=None, gamma=2, balance_index=-1, smooth=None, size_average=True):
super(FocalLoss, self ).__init__()
self.num_class = num_class
self.alpha = alpha
self.gamma = gamma
self.smooth = smooth
self.size_average = size_average
if self.alpha is None:
self.alpha = torch.ones(self.num_class, 1)
 elif isinstance(self.alpha,(list, np.ndarray)) :
assert len(self.alpha)== self.num_class
self.alpha = torch.FloatTensor(alpha ).view(self.num_class, 1)
 self.alpha = self.alpha / self.alpha.sum()
elif isinstance(self.alpha, float):
alpha = torch.ones(self.num_class, 1)
 alpha = alpha *(1 - self.alpha)
 alpha[balance_index] = self.alpha
self.alpha = alpha
else:
raise TypeError('Not support alpha type')
 if self.smooth is not None:
if self.smooth < 0 or self.smooth > 1.0:
raise ValueError('smooth value should be in [0,1]')
 def forward(self, logit, target):
if logit.dim() > 2:
logit = logit.view(logit.size(0), logit.size(1), -1)
 logit = logit.permute(0, 2, 1 ).contiguous()
logit = logit.view(-1, logit.size(-1))
target = target.view(-1, 1)
 epsilon = 1e-10
alpha = self.alpha
idx = target.cpu().long()
one_hot_key = torch.FloatTensor(target.size(0), self.num_class ).zero_()
one_hot_key = one_hot_key.scatter_(1, idx, 1)
 if one_hot_key.device != logit.device:
one_hot_key = one_hot_key.to(logit.device)
 if self.smooth:
one_hot_key = torch.clamp(
 one_hot_key, self.smooth/(self.num_class-1), 1.0 - self.smooth)
 pt =(one_hot_key * logit ).sum(1)+ epsilon
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
alpha=alpha.to(device)
 loss = -1 * alpha * torch.pow(( 1 - pt), gamma)* logpt
if self.size_average:
loss = loss.mean()
else:
loss = loss.sum()
return loss
kf = KFold(n_splits=10, random_state=42, shuffle=True)
 cv_scores = []
cv_std = []
baseline_models = ['Linear_Reg.','Bayesian_Ridge_Reg.','LGBM_Reg.','SVR',
'Dec_Tree_Reg.','Random_Forest_Reg.', 'XGB_Reg.',
'Grad_Boost_Reg.','Cat_Boost_Reg.','Stacked_Reg.']
lreg = LinearRegression()
score_lreg = cv_rmse(lreg)
 cv_scores.append(score_lreg.mean())
 cv_std.append(score_lreg.std())
 brr = BayesianRidge(compute_score=True)
 score_brr = cv_rmse(brr)
 cv_scores.append(score_brr.mean())
 cv_std.append(score_brr.std())
 l_gbm = LGBMRegressor(objective='regression')
 score_l_gbm = cv_rmse(l_gbm)
 cv_scores.append(score_l_gbm.mean())
 cv_std.append(score_l_gbm.std())
 svr = SVR()
score_svr = cv_rmse(svr)
 cv_scores.append(score_svr.mean())
 cv_std.append(score_svr.std())
 dtr = DecisionTreeRegressor()
score_dtr = cv_rmse(dtr)
 cv_scores.append(score_dtr.mean())
 cv_std.append(score_dtr.std())
 rfr = RandomForestRegressor()
score_rfr = cv_rmse(rfr)
 cv_scores.append(score_rfr.mean())
 cv_std.append(score_rfr.std())
 xgb = xgb.XGBRegressor()
score_xgb = cv_rmse(xgb)
 cv_scores.append(score_xgb.mean())
 cv_std.append(score_xgb.std())
 gbr = GradientBoostingRegressor()
score_gbr = cv_rmse(gbr)
 cv_scores.append(score_gbr.mean())
 cv_std.append(score_gbr.std())
 catb = CatBoostRegressor()
score_catb = cv_rmse(catb)
 cv_scores.append(score_catb.mean())
 cv_std.append(score_catb.std())
 stack_gen = StackingRegressor(regressors=(CatBoostRegressor() ,
LinearRegression() ,
BayesianRidge() ,
GradientBoostingRegressor()),
meta_regressor = CatBoostRegressor() ,
use_features_in_secondary = True)
 score_stack_gen = cv_rmse(stack_gen)
 cv_scores.append(score_stack_gen.mean())
 cv_std.append(score_stack_gen.std())
 final_cv_score = pd.DataFrame(baseline_models, columns = ['Regressors'])
 final_cv_score['RMSE_mean'] = cv_scores
final_cv_score['RMSE_std'] = cv_std<split>
class SubsetSampler(Sampler):
def __init__(self, indices):
self.num_samples = len(indices)
 self.indices = indices
def __iter__(self):
return iter(self.indices)
 def __len__(self):
return self.num_samples
X_train,X_val,y_train,y_val = train_test_split(train,target_log,test_size = 0.1,random_state=42)
 cat = CatBoostRegressor()
cat_model = cat.fit(X_train,y_train,
eval_set =(X_val,y_val),
plot=True,
verbose = 0 )<predict_on_test>
def train_valid_split(dataset, test_size = 0.25, shuffle = False, random_seed = 0):
length = dataset.__len__()
indices = list(range(1,length))
if shuffle == True:
random.seed(random_seed)
 random.shuffle(indices)
 if type(test_size)is float:
split = floor(test_size * length)
 elif type(test_size)is int:
split = test_size
else:
raise ValueError('%s should be an int or a float' % str)
 return indices[split:], indices[:split]
cat_pred = cat_model.predict(X_val)
 cat_score = rmse(y_val, cat_pred)
 cat_score<find_best_params>
transform_augmented = transforms.Compose([
transforms.RandomResizedCrop(224),
PowerPIL() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 transform_raw = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
cat_model.get_all_params()<train_on_grid>
trainset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_augmented)
 valset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_raw)
 accset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_raw )
grid = {'iterations': [1000,6000],
'learning_rate': [0.05, 0.005, 0.0005],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 3, 5, 9]}
final_model = CatBoostRegressor()
randomized_search_result = final_model.randomized_search(grid,
X = X_train,
y= y_train,
verbose = False,
plot=True)
<train_model>
df=pd.read_csv("./.. /input/train.csv" )
params = {'iterations': 6000,
'learning_rate': 0.005,
'depth': 4,
'l2_leaf_reg': 1,
'eval_metric':'RMSE',
'early_stopping_rounds': 200,
'verbose': 200,
'random_seed': 42}
cat_f = CatBoostRegressor(**params)
 cat_model_f = cat_f.fit(X_train,y_train,
eval_set =(X_val,y_val),
plot=True,
verbose = False)
 catf_pred = cat_model_f.predict(X_val)
 catf_score = rmse(y_val, catf_pred )<predict_on_test>
class_freq=defaultdict(int)
 freq=[]
for ind,val in df.iterrows() :
class_freq[int(val["Category"])]+=1
for ind,val in class_freq.items() :
freq.append([ind,val])
 freq.sort()
freq=[i[1] for i in freq]
wt_per_class=[0.]*67
N=float(sum(freq))
for i in range(67):
wt_per_class[i] = N/float(freq[i])
 weight=[0]*len(df)
 for ind,val in df.iterrows() :
cat=val["Category"]
weight[ind]=wt_per_class[cat]
test_pred = cat_f.predict(test)
 submission = pd.DataFrame(test_id, columns = ['Id'])
 test_pred = np.expm1(test_pred)
 submission['SalePrice'] = test_pred
submission.head()<save_to_csv>
train_idx, valid_idx = train_valid_split(trainset, 0.25)
 train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetSampler(valid_idx)
 train_loader = DataLoader(trainset,batch_size=50,sampler=train_sampler,num_workers=4)
 valid_loader = DataLoader(valset,batch_size=200,sampler=valid_sampler,num_workers=4)
 acc_loader = DataLoader(accset,batch_size=200,num_workers=4)
submission.to_csv("result.csv", index = False, header = True )<load_from_csv>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
train_data = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 train_data.head()<load_from_csv>
model_ft = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x16d_wsl')
test_data = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 test_data.head()<drop_column>
avgpool = model_ft.avgpool
l4 = model_ft.layer4
l3 = model_ft.layer3
train_ID = train_data['Id']
test_ID = test_data['Id']
train_data.drop("Id", axis = 1, inplace = True)
 test_data.drop("Id", axis = 1, inplace = True )<prepare_x_and_y>
for param in model_ft.parameters() :
param.requires_grad = False
ntrain = train_data.shape[0]
ntest = test_data.shape[0]
y_train = train_data.SalePrice.values
all_data = pd.concat(( train_data, test_data)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<create_dataframe>
model_ft.fc = nn.Sequential(nn.Dropout(p=0.5),nn.Linear(2048, 1024),nn.LeakyReLU(inplace=True),nn.Linear(1024,67),nn.Softmax(dim=1))
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )<data_type_conversions>
model_ft.to(device )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None")
 all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None")
 all_data["Alley"] = all_data["Alley"].fillna("None")
 all_data["Fence"] = all_data["Fence"].fillna("None")
 train_data = train_data.drop(['PoolQC','MiscFeature', 'Alley', 'Fence'] ,axis=1)
 all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None")
 all_data["GarageFinish"] = all_data["GarageFinish"].fillna("None")
 all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0)
 for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None')
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0)
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None')
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0])
 all_data = all_data.drop(['Utilities'], axis=1)
 all_data["Functional"] = all_data["Functional"].fillna("Typ")
 all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0])
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0])
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
 all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None")
 all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )<data_type_conversions>
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_ft.parameters() , lr=0.00001,momentum=0.9 )
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str)
 cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))<feature_engineering>
model_ft_wts=torch.load("best_model_resnext_16d_2048_1024_dropout_0.5_b.pkl" )
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']<sort_values>
model_ft.load_state_dict(model_ft_wts )
corrmat['SalePrice'].sort_values(ascending=False )<feature_engineering>
best_acc=0.84516
best_model_wts = copy.deepcopy(model_ft.state_dict() )
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam )<categorify>
num_epochs=0
for epoch in range(num_epochs):
print("Epoch : "+str(epoch))
print("-"*10)
 running_loss = 0.0
running_corrects=0
wrong=0
model_ft.train()
for inp,labels in train_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = model_ft(inp)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = running_loss /(len(train_loader)*1.0)
 print('TRAINING SET Loss: {}'.format(epoch_loss))
if True:
correct=0
wrong=0
model_ft.eval()
for inp,labels in valid_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 correct += torch.sum(preds == labels)
 wrong += torch.sum(preds != labels)
 acc =(correct.float())/(( correct+wrong ).float())
 print('VALIDATION SET Correct: {} Wrong {} Acc {}'.format(correct,wrong,acc))
if acc > best_acc:
best_acc = acc
best_model_wts = copy.deepcopy(model_ft.state_dict())
 running_loss = 0.0
running_correct = 0
model_ft.load_state_dict(best_model_wts)
 torch.save(model_ft.state_dict() , "best_model_resnext_16d_2048_1024_dropout_0.5_c_wts.pkl")
 print('------ Finished Training -----' )
all_data = pd.get_dummies(all_data)
 print(all_data.shape )<split>
model_ft.load_state_dict(best_model_wts )
train_data = all_data[:ntrain]
test_data = all_data[ntrain:]<import_modules>
if True:
correct=0
wrong=0
model_ft.eval()
for inp,labels in acc_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 correct += torch.sum(preds == labels)
 wrong += torch.sum(preds != labels)
 acc =(correct.float())/(( correct+wrong ).float())
 print('ACCURACY SET Correct: {} Wrong {} ACC {} '.format(correct,wrong,acc))
from sklearn.metrics import mean_squared_error
import xgboost as xgb
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split<choose_model_class>
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )<compute_test_metric>
testset = ImageDataset(csv_file = '.. /input/sample_sub.csv', root_dir = '.. /input/data/data/', transform=transform_raw)
 testloader = torch.utils.data.DataLoader(testset, batch_size=300, shuffle=False, num_workers=4)
 res_predict=[]
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
model_xgb.fit(train_data, y_train)
 xgb_train_pred = model_xgb.predict(train_data)
 xgb_pred = np.expm1(model_xgb.predict(test_data))
print(rmsle(y_train, xgb_train_pred))<compute_train_metric>
model_ft.eval()
for inp,label in testloader:
inp = inp.to(device)
 label = label.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 for i in range(len(preds)) :
res_predict.append(int(preds[i]))
submission['Category'] = res_predict
<save_to_csv><EOS> 
submission.to_csv('submission.csv', index=False, encoding='utf-8' )