{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# This Notebook for Training phase\n### if you miss previous notebook [link_here](https://www.kaggle.com/code/bitguber/preprocessing-notebook-for-digital-turbine)\n### if i make any mistake or inefficient operation for training then please comment it","metadata":{}},{"cell_type":"markdown","source":"# import modules and prepared train data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np # linear algebra\nimport pandas as pd\nimport gc\nfrom tqdm import tqdm\ncols={'countryCode': np.float32,\n 'c2': np.float32,\n 'c4': np.float32,\n 'size': np.float32,\n 'mediationProviderVersion': np.int32,\n 'bidFloorPrice': np.float32,\n 'sentPrice': np.float32,\n 'winBid': np.float32,\n 'c1std_bidWin': np.float32,\n 'c1max_bidWin': np.float32,\n 'c1min_bidWin': np.float32,\n 'c1mean_bidWin': np.float32,\n 'c1median_bidWin': np.float32,'c1sem_bidWin': np.float32,'c1var_bidWin': np.float32,\n 'deviceIdstd_bidWin': np.float32,\n 'deviceIdmax_bidWin': np.float32,\n 'deviceIdmin_bidWin': np.float32,\n 'deviceIdmean_bidWin': np.float32,\n 'deviceIdmedian_bidWin': np.float32,\n 'deviceIdsem_bidWin': np.float32,\n 'deviceIdvar_bidWin': np.float32,\n             'unitDisplayTypestd_bidWin': np.float32,\n 'unitDisplayTypemax_bidWin': np.float32,\n 'unitDisplayTypemean_bidWin': np.float32,\n 'unitDisplayTypemedian_bidWin': np.float32,\n 'unitDisplayTypesem_bidWin': np.float32,\n 'unitDisplayTypevar_bidWin': np.float32,\n'bundleIdstd_bidWin': np.float32,\n 'bundleIdmax_bidWin': np.float32,\n 'bundleIdmin_bidWin': np.float32,\n 'bundleIdmean_bidWin': np.float32,\n 'bundleIdmedian_bidWin': np.float32,\n 'bundleIdsem_bidWin': np.float32,\n 'bundleIdvar_bidWin': np.float32,\n 'ver': np.float32,\n 'unitDisplayType_0': np.uint8,\n 'unitDisplayType_1': np.uint8,\n 'unitDisplayType_2': np.uint8,\n 'connectionType_0': np.uint8,\n 'connectionType_1': np.uint8,\n 'connectionType_2': np.uint8,\n 'connectionType_3': np.uint8,\n 'bundleId_0': np.uint8,\n 'bundleId_1': np.uint8,\n 'bundleId_2': np.uint8,\n 'bundleId_3': np.uint8,\n 'bundleId_4': np.uint8,\n 'bundleId_5': np.uint8,\n 'bundleId_6': np.uint8,\n 'bundleId_7': np.uint8,\n 'bundleId_8': np.uint8,\n 'bundleId_9': np.uint8,\n 'bundleId_10': np.uint8,\n 'bundleId_11': np.uint8,\n 'bundleId_12': np.uint8,\n 'bundleId_13': np.uint8,\n 'bundleId_14': np.uint8,\n 'bundleId_15': np.uint8,\n 'bundleId_16': np.uint8,\n 'bundleId_17': np.uint8}\nfor x in range(50):\n    cols[f'c1_{x}']=np.uint8\n# loading train data\ndf=pd.read_csv('../input/fork-of-eda-item-price/prepared_train.csv',float_precision='round_trip',dtype=cols)\n\n# fill up nan in features with mean value of it\ns=df.deviceIdstd_bidWin.mean()\ndf['deviceIdstd_bidWin'].fillna(s,inplace=True)\ns=df.deviceIdvar_bidWin.mean()\ndf['deviceIdvar_bidWin'].fillna(s,inplace=True)\ns=df.deviceIdsem_bidWin.mean()\ndf['deviceIdsem_bidWin'].fillna(s,inplace=True)\n\n# split train and validation data from prepared train data\ntrain,val = train_test_split(df, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-09-21T11:13:10.680031Z","iopub.execute_input":"2022-09-21T11:13:10.680482Z","iopub.status.idle":"2022-09-21T11:15:12.588154Z","shell.execute_reply.started":"2022-09-21T11:13:10.680379Z","shell.execute_reply":"2022-09-21T11:15:12.587074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check there is any nan present on it\ndf.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-09-21T11:15:12.590131Z","iopub.execute_input":"2022-09-21T11:15:12.590799Z","iopub.status.idle":"2022-09-21T11:15:13.679263Z","shell.execute_reply.started":"2022-09-21T11:15:12.590762Z","shell.execute_reply":"2022-09-21T11:15:13.678174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train.columns)","metadata":{"execution":{"iopub.status.busy":"2022-09-21T11:15:13.681199Z","iopub.execute_input":"2022-09-21T11:15:13.681805Z","iopub.status.idle":"2022-09-21T11:15:13.691328Z","shell.execute_reply.started":"2022-09-21T11:15:13.681739Z","shell.execute_reply":"2022-09-21T11:15:13.690310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model and his Training Define part ","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport glob,math\nfrom IPython.display import clear_output\n\n# I like to use tensorflow sequence transform to fitting preprocess\nclass Auction_Sequence(tf.keras.utils.Sequence):\n\n    def __init__(self, data, batch_size):\n        self.data = data\n        self.batch_size = batch_size\n\n    def __len__(self):\n        return math.ceil(len(self.data) / self.batch_size)\n\n    def __getitem__(self, idx):\n        dc=['winBid']\n        batch=self.data[idx * self.batch_size:(idx + 1) *self.batch_size]\n        batch_x=batch.drop(dc,axis=1)\n        batch_y=batch['winBid']\n        return batch_x.values,batch_y.values\n    def on_epoch_end(self):\n        # i like do in end of epoch shuffle data and it also help to prevents overfit\n        self.data=self.data.sample(frac=1,ignore_index=True,random_state=11)\ntrain_seq=Auction_Sequence(train,4096)\ntest_seq=Auction_Sequence(val,4096)\n\n# Method to return model with specific input size\ndef get_model(size):\n    tf.random.set_seed(11)\n    np.random.seed(11)\n    activation=tf.keras.layers.LeakyReLU(alpha=0.3)\n    feature=tf.keras.Input(shape=(size,),)\n    hidden_state=tf.keras.layers.Dense(128,activation=activation,name='ind')(feature)\n    hidden_state=tf.keras.layers.Dense(64,activation=activation)(hidden_state)\n    hidden_state=tf.keras.layers.Dense(32,activation=activation)(hidden_state)\n    hidden_state=tf.keras.layers.Dense(16,activation=activation)(hidden_state)\n    hidden_state=tf.keras.layers.Dense(8,activation=activation)(hidden_state)\n    hidden_state=tf.keras.layers.Dense(4,activation=activation)(hidden_state)\n#     hidden_state=tf.keras.layers.Dense(2,activation=activation)(hidden_state)\n    outputs=tf.keras.layers.Dense(1,activation=None)(hidden_state)\n    model = tf.keras.models.Model(inputs=feature, outputs=outputs)\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse')\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2022-09-21T11:15:13.693056Z","iopub.execute_input":"2022-09-21T11:15:13.693446Z","iopub.status.idle":"2022-09-21T11:15:18.995821Z","shell.execute_reply.started":"2022-09-21T11:15:13.693411Z","shell.execute_reply":"2022-09-21T11:15:18.994790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Phase and Save trained model ","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nfrom sklearn.metrics import f1_score\nimport shutil\nimport os\npath=f'./models/train_model/'\nos.makedirs(path,exist_ok=True)\nmodel=get_model(len(train.columns)-1)\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(path+'model-{val_loss:03f}-{epoch:03d}-.h5',\n    verbose=1, monitor='val_loss',save_weights_only=True,save_best_only=True, mode='min')\n    \nmodel.fit(train_seq,epochs=100,validation_data=test_seq,workers=-1,max_queue_size=10,\n                  use_multiprocessing=True, callbacks=[checkpoint],verbose=2)\n\nfiles = glob.glob(os.path.expanduser(f\"{path}*\"))\nbest_weight=sorted(files, key=os.path.getmtime)[-1]\nprint(best_weight)\nmodel.load_weights(best_weight)\nshutil.rmtree(path)\nscore=float(best_weight.split('-')[1])\nmodel.save(f'train_{score}.h5')\ndel model,best_weight\ngc.collect()\ntf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2022-09-21T11:15:18.998171Z","iopub.execute_input":"2022-09-21T11:15:18.998648Z","iopub.status.idle":"2022-09-21T11:48:37.687148Z","shell.execute_reply.started":"2022-09-21T11:15:18.998621Z","shell.execute_reply":"2022-09-21T11:48:37.685947Z"},"trusted":true},"execution_count":null,"outputs":[]}]}