Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<import_modules>
start_real = datetime.now()
np.random.seed(123 )
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense,Flatten,Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop,Adam, SGD 
<load_from_csv>
def rmsle(Y, Y_pred):
assert Y.shape == Y_pred.shape
return np.sqrt(np.mean(np.square(Y_pred - Y)))
CV = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 x_cv = CV.loc[:,'pixel0':].values
y_cv = CV.loc[:,:'label'].values
x_train = train.loc[:,'pixel0':].values
y_train = train.loc[:,:'label'].values<train_model>
%%time
train_df = pd.read_table('.. /input/train.tsv')
 test_df = pd.read_table('.. /input/test.tsv')
 print(train_df.shape, test_df.shape )
print('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))
print('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))<categorify>
train_df = train_df.drop(train_df[(train_df.price < 3.0)].index)
 train_df.shape
y_train = to_categorical(y_train)
 y_cv = to_categorical(y_cv )<define_variables>
num_classes = y_train.shape[1]<data_type_conversions>
%%time
def wordCount(text):
try:
if text == 'No description yet':
return 0
else:
text = text.lower()
words = [w for w in text.split(" ")]
return len(words)
 except:
return 0
train_df['desc_len'] = train_df['item_description'].apply(lambda x: wordCount(x))
test_df['desc_len'] = test_df['item_description'].apply(lambda x: wordCount(x))
train_df['name_len'] = train_df['name'].apply(lambda x: wordCount(x))
test_df['name_len'] = test_df['name'].apply(lambda x: wordCount(x))
train_df.head()
img_rows = 28
img_cols = 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
 x_cv = x_cv.reshape(x_cv.shape[0], img_rows, img_cols, 1)
 input_shape =(img_rows, img_cols, 1)
 x_train = x_train.astype('float32')
 x_cv = x_cv.astype('float32')
 x_train = x_train/255
x_cv = x_cv/255
print('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))
print('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))<choose_model_class>
%%time
def split_cat(text):
try: return text.split("/")
 except: return("No Label", "No Label", "No Label")
 train_df['subcat_0'], train_df['subcat_1'], train_df['subcat_2'] = \
zip(*train_df['category_name'].apply(lambda x: split_cat(x)))
 test_df['subcat_0'], test_df['subcat_1'], test_df['subcat_2'] = \
zip(*test_df['category_name'].apply(lambda x: split_cat(x)) )
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), activation= 'relu', padding = 'Same',input_shape=input_shape))
model.add(Conv2D(64,(3,3), activation= 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(32,(3,3), activation= 'relu', padding = 'Same'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation= 'relu'))
model.add(Dropout(0.50))
model.add(Dense(num_classes, activation= 'softmax'))
model.compile(optimizer=SGD(0.01), loss='categorical_crossentropy', metrics=['accuracy'])
 print(model.summary() )<train_model>
full_set = pd.concat([train_df,test_df])
 all_brands = set(full_set['brand_name'].values)
 train_df.brand_name.fillna(value="missing", inplace=True)
 test_df.brand_name.fillna(value="missing", inplace=True)
 premissing = len(train_df.loc[train_df['brand_name'] == 'missing'])
 def brandfinder(line):
brand = line[0]
name = line[1]
namesplit = name.split(' ')
 if brand == 'missing':
for x in namesplit:
if x in all_brands:
return name
if name in all_brands:
return name
return brand
train_df['brand_name'] = train_df[['brand_name','name']].apply(brandfinder, axis = 1)
 test_df['brand_name'] = test_df[['brand_name','name']].apply(brandfinder, axis = 1)
 found = premissing-len(train_df.loc[train_df['brand_name'] == 'missing'])
 print(found )
batch_size = 32
epochs = 20
history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(x_cv, y_cv))<load_from_csv>
train_df["target"] = np.log1p(train_df.price)
 train_df, dev_df = train_test_split(train_df, random_state=123, train_size=0.99)
 n_trains = train_df.shape[0]
n_devs = dev_df.shape[0]
n_tests = test_df.shape[0]
print("Training on", n_trains, "examples")
 print("Validating on", n_devs, "examples")
 print("Testing on", n_tests, "examples" )
test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 test_id = test.loc[:,:'id'].values
x_test = test.loc[:,'pixel0':].values
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
 x_test = x_test.astype('float32')
 x_test = x_test/255<predict_on_test>
full_df = pd.concat([train_df, dev_df, test_df] )
pred = model.predict(x_test )<prepare_output>
%%time
def fill_missing_values(df):
df.category_name.fillna(value="missing", inplace=True)
 df.brand_name.fillna(value="missing", inplace=True)
 df.item_description.fillna(value="missing", inplace=True)
 df.item_description.replace('No description yet',"missing", inplace=True)
 return df
print("Filling missing data...")
 full_df = fill_missing_values(full_df)
 print(full_df.category_name[1] )
label = np.argmax(pred, axis=1 )<create_dataframe>
%%time
print("Processing categorical data...")
 le = LabelEncoder()
le.fit(full_df.category_name)
 full_df['category'] = le.transform(full_df.category_name)
 le.fit(full_df.brand_name)
 full_df.brand_name = le.transform(full_df.brand_name)
 le.fit(full_df.subcat_0)
 full_df.subcat_0 = le.transform(full_df.subcat_0)
 le.fit(full_df.subcat_1)
 full_df.subcat_1 = le.transform(full_df.subcat_1)
 le.fit(full_df.subcat_2)
 full_df.subcat_2 = le.transform(full_df.subcat_2)
 del le
test_csv = pd.DataFrame(test['id'].values,columns=['id'] )<feature_engineering>
test_csv['label'] = label<save_to_csv>
%%time
print("Transforming text data to sequences...")
 raw_text = np.hstack([full_df.item_description.str.lower() , full_df.name.str.lower() , full_df.category_name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to sequences...")
 full_df['seq_item_description'] = tok_raw.texts_to_sequences(full_df.item_description.str.lower())
 full_df['seq_name'] = tok_raw.texts_to_sequences(full_df.name.str.lower())
 del tok_raw
test_csv.to_csv('submission.csv', index=False )<set_options>
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_CATEGORY_SEQ = 8
MAX_TEXT = np.max([
np.max(full_df.seq_name.max()),
np.max(full_df.seq_item_description.max()),
])+ 100
MAX_CATEGORY = np.max(full_df.category.max())+ 1
MAX_BRAND = np.max(full_df.brand_name.max())+ 1
MAX_CONDITION = np.max(full_df.item_condition_id.max())+ 1
MAX_DESC_LEN = np.max(full_df.desc_len.max())+ 1
MAX_NAME_LEN = np.max(full_df.name_len.max())+ 1
MAX_SUBCAT_0 = np.max(full_df.subcat_0.max())+ 1
MAX_SUBCAT_1 = np.max(full_df.subcat_1.max())+ 1
MAX_SUBCAT_2 = np.max(full_df.subcat_2.max())+ 1
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('.. /kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
%%time
def get_rnn_data(dataset):
X = {
'name': pad_sequences(dataset.seq_name, maxlen=MAX_NAME_SEQ),
'item_desc': pad_sequences(dataset.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ),
'brand_name': np.array(dataset.brand_name),
'category': np.array(dataset.category),
'item_condition': np.array(dataset.item_condition_id),
'num_vars': np.array(dataset[["shipping"]]),
'desc_len': np.array(dataset[["desc_len"]]),
'name_len': np.array(dataset[["name_len"]]),
'subcat_0': np.array(dataset.subcat_0),
'subcat_1': np.array(dataset.subcat_1),
'subcat_2': np.array(dataset.subcat_2),
}
return X
train = full_df[:n_trains]
dev = full_df[n_trains:n_trains+n_devs]
test = full_df[n_trains+n_devs:]
X_train = get_rnn_data(train)
 Y_train = train.target.values.reshape(-1, 1)
 X_dev = get_rnn_data(dev)
 Y_dev = dev.target.values.reshape(-1, 1)
 X_test = get_rnn_data(test )
df_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 df_test = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 y_train = df_train.iloc[:,0].values
y_test = df_test.iloc[:,0].values
df_train = df_train.drop(['label'], axis=1)
 df_test = df_test.drop(['label'], axis=1)
 N_train = len(y_train)
 N_test = len(y_test)
<prepare_x_and_y>
def root_mean_squared_logarithmic_error(y_true, y_pred):
first_log = K.log(K.clip(y_pred, K.epsilon() , None)+ 1.)
second_log = K.log(K.clip(y_true, K.epsilon() , None)+ 1.)
return K.sqrt(K.mean(K.square(first_log - second_log), axis=-1)+0.0000001)
 def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1)+0.0000001 )
x_train = np.uint8(df_train.values.reshape(-1,28,28))
x_test = np.uint8(df_test.values.reshape(-1,28,28))<categorify>
np.random.seed(123)
 def new_rnn_model(lr=0.001, decay=0.0):
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 desc_len = Input(shape=[1], name="desc_len")
 name_len = Input(shape=[1], name="name_len")
 subcat_0 = Input(shape=[1], name="subcat_0")
 subcat_1 = Input(shape=[1], name="subcat_1")
 subcat_2 = Input(shape=[1], name="subcat_2")
 emb_name = Embedding(MAX_TEXT, 20 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 60 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_item_condition = Embedding(MAX_CONDITION, 5 )(item_condition)
 emb_desc_len = Embedding(MAX_DESC_LEN, 5 )(desc_len)
 emb_name_len = Embedding(MAX_NAME_LEN, 5 )(name_len)
 emb_subcat_0 = Embedding(MAX_SUBCAT_0, 10 )(subcat_0)
 emb_subcat_1 = Embedding(MAX_SUBCAT_1, 10 )(subcat_1)
 emb_subcat_2 = Embedding(MAX_SUBCAT_2, 10 )(subcat_2)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name)
 , Flatten()(emb_item_condition)
 , Flatten()(emb_desc_len)
 , Flatten()(emb_name_len)
 , Flatten()(emb_subcat_0)
 , Flatten()(emb_subcat_1)
 , Flatten()(emb_subcat_2)
 , rnn_layer1
, rnn_layer2
, num_vars
])
 main_l = Dropout(0.1 )(Dense(512,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(256,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(128,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(64,kernel_initializer='normal',activation='relu' )(main_l))
output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name , item_condition,
num_vars, desc_len, name_len, subcat_0, subcat_1, subcat_2], output)
 optimizer = Adam(lr=lr, decay=decay)
 model.compile(loss = 'mse', optimizer = optimizer)
 return model
model = new_rnn_model()
model.summary()
del model
def preprocessing(img):
kernel = np.array([[0,-1.5,0],[-1.5,8.5,-1.5],[0,-1.5,0]])
 img = cv2.filter2D(img,-1,kernel)
 img_thresh = img/255.0 *2 -1
return img_thresh
x_train = np.array(list(map(preprocessing,x_train)))
 x_test = np.array(list(map(preprocessing, x_test)))
 x_train= x_train.reshape(-1,28,28,1)
 x_test= x_test.reshape(-1,28,28,1)
 y_train = tf.keras.utils.to_categorical(y_train,10)
 y_test = tf.keras.utils.to_categorical(y_test,10 )<split>
%%time
BATCH_SIZE = 512 * 3
epochs = 4
exp_decay = lambda init, fin, steps:(init/fin)**(1/(steps-1)) - 1
steps = int(len(X_train['name'])/ BATCH_SIZE)* epochs
lr_init, lr_fin = 0.005, 0.001
lr_decay = exp_decay(lr_init, lr_fin, steps)
 rnn_model = new_rnn_model(lr=lr_init, decay=lr_decay)
 rnn_model.fit(
 X_train, Y_train, epochs=epochs, batch_size=BATCH_SIZE,
validation_data=(X_dev, Y_dev), verbose=1,
)
X_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, random_state=0, test_size=0.15)
 X_train.shape, X_val.shape, Y_train.shape, Y_val.shape<define_search_model>
%%time
print("Evaluating the model on validation data...")
 Y_dev_preds_rnn = rnn_model.predict(X_dev, batch_size=BATCH_SIZE)
 print(" RMSLE error:", rmsle(Y_dev, Y_dev_preds_rnn))
i = Input(shape=(X_train[0].shape))
x1 = Conv2D(32,(3,3), padding="same" )(i)
 x1 = LeakyReLU(0.2 )(x1)
 x1 = BatchNormalization()(x1)
 x1 = Conv2D(32,(3,3), padding="same" )(x1)
 x1 = LeakyReLU(0.2 )(x1)
 x1 = BatchNormalization()(x1)
 x1 = Conv2D(64,(3,3), padding="same" )(x1)
 x1 = LeakyReLU(0.2 )(x1)
 b1 = BatchNormalization()(x1)
 x2 = Conv2D(64,(3,3), padding="same" )(b1)
 x2 = LeakyReLU(0.2 )(x2)
 x2 = BatchNormalization()(x2)
 x2 = Conv2D(64,(5,5), padding="same" )(x2)
 x2 = LeakyReLU(0.2 )(x2)
 x2 = BatchNormalization()(x2)
 x3 = Add()([b1,x2])
 x3 = MaxPooling2D()(x3)
 x3 = Dropout(0.2 )(x3)
 x3 = Conv2D(128,(3,3), padding="same" )(x3)
 x3 = LeakyReLU(0.1 )(x3)
 x3 = BatchNormalization()(x3)
 x3 = Conv2D(128,(3,3), padding="same" )(x3)
 x3 = LeakyReLU(0.1 )(x3)
 x3 = Conv2D(256,(3,3), padding="same" )(x3)
 x3 = LeakyReLU(0.1 )(x3)
 b2 = BatchNormalization()(x3)
 x3 = Conv2D(256,(5,5), padding="same" )(b2)
 x3 = LeakyReLU(0.1 )(x3)
 x3 = BatchNormalization()(x3)
 x = Add()([b2,x3])
 x = MaxPooling2D()(x)
 x = Conv2D(256,(3,3), padding="same" )(x)
 x = LeakyReLU(0.1 )(x)
 x = GlobalMaxPooling2D()(x)
 x = Flatten()(x)
 x = Dense(128, activation='relu', kernel_regularizer=l2(0.01))(x)
 x = Dropout(0.2 )(x)
 x = Dense(128, activation='relu', kernel_regularizer=l2(0.01))(x)
 x = Dropout(0.2 )(x)
 x = Dense(10, kernel_regularizer=l2(0.01))(x)
 model = Model(i,x)
 plot_model(model, "model.png", show_shapes=True )<train_model>
rnn_preds = rnn_model.predict(X_test, batch_size=BATCH_SIZE, verbose=1)
 rnn_preds = np.expm1(rnn_preds )
model.compile(optimizer=Adam(lr=0.001, beta_1=0.9),
loss='squared_hinge',
metrics=['accuracy'])
 dataGen = ImageDataGenerator(rotation_range=15,width_shift_range=0.2, height_shift_range=0.2,zoom_range=0.15)
 dataGen.fit(X_train)
 batch_size=32
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3,
patience=3, min_lr=1e-10)
 r = model.fit_generator(dataGen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=X_train.shape[0]//batch_size, epochs=50, validation_data=(X_val,Y_val), verbose=2,callbacks=[reduce_lr] )<compute_test_metric>
full_df = pd.concat([train_df, dev_df, test_df] )
model.evaluate(x_test, y_test )<load_from_csv>
%%time
print("Handling missing values...")
 full_df['category_name'] = full_df['category_name'].fillna('missing' ).astype(str)
 full_df['subcat_0'] = full_df['subcat_0'].astype(str)
 full_df['subcat_1'] = full_df['subcat_1'].astype(str)
 full_df['subcat_2'] = full_df['subcat_2'].astype(str)
 full_df['brand_name'] = full_df['brand_name'].fillna('missing' ).astype(str)
 full_df['shipping'] = full_df['shipping'].astype(str)
 full_df['item_condition_id'] = full_df['item_condition_id'].astype(str)
 full_df['desc_len'] = full_df['desc_len'].astype(str)
 full_df['name_len'] = full_df['name_len'].astype(str)
 full_df['item_description'] = full_df['item_description'].fillna('No description yet' ).astype(str )
df_final = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 df_final = df_final.drop(['id'], axis=1)
 X_final = np.uint8(df_final.values.reshape(-1,28,28))
X_final = np.array(list(map(preprocessing,X_final)))
 X_final= X_final.reshape(-1,28,28,1)
 pred = model.predict(X_final ).argmax(axis=1 )<save_to_csv>
%%time
print("Vectorizing data...")
 default_preprocessor = CountVectorizer().build_preprocessor()
def build_preprocessor(field):
field_idx = list(full_df.columns ).index(field)
 return lambda x: default_preprocessor(x[field_idx])
 vectorizer = FeatureUnion([ 
('name', CountVectorizer(
 ngram_range=(1, 2),
max_features=50000,
preprocessor=build_preprocessor('name'))), 
('subcat_0', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_0'))), 
('subcat_1', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_1'))), 
('subcat_2', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_2'))), 
('brand_name', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('brand_name'))), 
('shipping', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('shipping'))), 
('item_condition_id', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('item_condition_id'))), 
('desc_len', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('desc_len'))), 
('name_len', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('name_len'))), 
('item_description', TfidfVectorizer(
 ngram_range=(1, 3),
max_features=100000,
preprocessor=build_preprocessor('item_description'))),
])
 X = vectorizer.fit_transform(full_df.values)
 X_train = X[:n_trains]
Y_train = train_df.target.values.reshape(-1, 1)
 X_dev = X[n_trains:n_trains+n_devs]
Y_dev = dev_df.target.values.reshape(-1, 1)
 X_test = X[n_trains+n_devs:]
sub = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 sub['label'] = pred
sub['label']= sub['label'].astype(int)
 sub.to_csv('submission.csv', index=False )<define_variables>
%%time
print("Fitting Ridge model on training examples...")
 ridge_model = Ridge(
 solver='auto', fit_intercept=True, alpha=1.0,
max_iter=100, normalize=False, tol=0.05, random_state = 1, 
)
 ridge_modelCV = RidgeCV(
 fit_intercept=True, alphas=[5.0],
normalize=False, cv = 2, scoring='neg_mean_squared_error', 
)
 ridge_model.fit(X_train, Y_train)
 ridge_modelCV.fit(X_train, Y_train )
torch.manual_seed(0)
 np.random.seed(0)
 TRAIN_PATH = '.. /input/Kannada-MNIST/train.csv'
TEST_PATH = '.. /input/Kannada-MNIST/test.csv'
SUBMISSION_PATH = '/kaggle/working/submission.csv'
DIG_PATH = '.. /input/Kannada-MNIST/Dig-MNIST.csv'<train_model>
Y_dev_preds_ridge = ridge_model.predict(X_dev)
 Y_dev_preds_ridge = Y_dev_preds_ridge.reshape(-1, 1)
 print("RMSL error on dev set:", rmsle(Y_dev, Y_dev_preds_ridge))
<prepare_x_and_y>
Y_dev_preds_ridgeCV = ridge_modelCV.predict(X_dev)
 Y_dev_preds_ridgeCV = Y_dev_preds_ridgeCV.reshape(-1, 1)
 print("CV RMSL error on dev set:", rmsle(Y_dev, Y_dev_preds_ridgeCV))
class KannadaMNISTDataset(Dataset):
def __init__(self, csv_path, transform):
data = pd.read_csv(csv_path ).to_numpy()
self.y = data[:, 0]
self.X = data[:, 1:].reshape(-1, 28, 28 ).astype(np.uint8)
 assert self.y.shape[0] == self.X.shape[0]
self.transform = transform
def __len__(self):
return self.X.shape[0]
def __getitem__(self, idx):
return self.transform(self.X[idx]), self.y[idx]
def asPIL(self, idx):
return transforms.ToPILImage()(self.X[idx]), self.y[idx]
def merge(self, other):
self.X = np.concatenate(( self.X, other.X))
self.y = np.concatenate(( self.y, other.y))<split>
%%time
ridge_preds = ridge_model.predict(X_test)
 ridge_preds = np.expm1(ridge_preds)
 ridgeCV_preds = ridge_modelCV.predict(X_test)
 ridgeCV_preds = np.expm1(ridgeCV_preds )
transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.Resize(( 32, 32)) ,
transforms.RandomAffine(degrees=10, translate=(0.25, 0.25), scale=(0.75, 1.25), shear=0.1),
transforms.ToTensor()
])
 dataset = KannadaMNISTDataset(TRAIN_PATH, transform)
 dataset.merge(KannadaMNISTDataset(DIG_PATH, transform))
train_idx, test_idx = train_test_split(range(len(dataset)) , train_size=0.85)
 train_sampler = SubsetRandomSampler(train_idx)
 test_sampler = SubsetRandomSampler(test_idx)
 BATCH_SIZE = 512
train_loader = torch.utils.data.DataLoader(dataset, batch_size=BATCH_SIZE, sampler=train_sampler, num_workers=8, pin_memory=True)
 test_loader = torch.utils.data.DataLoader(dataset, batch_size=BATCH_SIZE, sampler=test_sampler, num_workers=8, pin_memory=True )<define_search_model>
%%time
def aggregate_predicts3(Y1, Y2, Y3, ratio1, ratio2):
assert Y1.shape == Y2.shape
return Y1 * ratio1 + Y2 * ratio2 + Y3 *(1.0 - ratio1-ratio2)
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self ).__init__()
def make_conv_group(in_, out_):
return nn.Sequential(
 nn.Conv2d(in_, out_, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) ,
nn.BatchNorm2d(out_, momentum=0.05),
nn.ReLU(inplace=True)
)
 self.layers = nn.Sequential(
 make_conv_group(1, 64),
make_conv_group(64, 128),
make_conv_group(128, 128),
make_conv_group(128, 128),
nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)) ,
nn.Dropout2d(p=0.1),
make_conv_group(128, 128),
make_conv_group(128, 128),
make_conv_group(128, 256),
nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)) ,
nn.Dropout2d(p=0.1),
make_conv_group(256, 256),
make_conv_group(256, 256),
nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)) ,
nn.Dropout2d(p=0.1),
make_conv_group(256, 512),
nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)) ,
nn.Dropout2d(p=0.1),
make_conv_group(512, 2048),
make_conv_group(2048, 256),
nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)) ,
nn.Dropout2d(p=0.1),
make_conv_group(256, 256)
)
 self.head = nn.Linear(256, 10)
 for layer in self.layers.modules() :
if isinstance(layer, nn.Conv2d):
nn.init.xavier_uniform_(layer.weight.data, gain=nn.init.calculate_gain('relu'))
def forward(self, x):
x = self.layers(x)
 x = x.view(x.size(0), -1)
 x = self.head(x)
 x = nn.Softmax(dim=1 )(x)
 return x<choose_model_class>
%%time
best1 = 0
best2 = 0
lowest = 0.99
for i in range(100):
for j in range(100):
r = i*0.01
r2 = j*0.01
if r+r2 < 1.0:
Y_dev_preds = aggregate_predicts3(Y_dev_preds_rnn, Y_dev_preds_ridgeCV, Y_dev_preds_ridge, r, r2)
 fpred = rmsle(Y_dev, Y_dev_preds)
 if fpred < lowest:
best1 = r
best2 = r2
lowest = fpred
Y_dev_preds = aggregate_predicts3(Y_dev_preds_rnn, Y_dev_preds_ridgeCV, Y_dev_preds_ridge, best1, best2 )
net = SimpleNet().cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters())
 scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', threshold=1e-5, verbose=True)
 EPOCH = 0<find_best_params>
preds = aggregate_predicts3(rnn_preds, ridgeCV_preds, ridge_preds, best1, best2)
 submission = pd.DataFrame({
"test_id": test_df.test_id,
"price": preds.reshape(-1),
})
 submission.to_csv("./rnn_ridge_submission_best.csv", index=False )
<split><EOS> 
stop_real = datetime.now()
execution_time_real = stop_real-start_real
print(execution_time_real )