text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from sklearn.model_selection import train_test_split

import gc

import numpy as np # linear algebra

import os

import pandas as pd

import shutil

import tensorflow as tf

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np
import pandas as pd
import gc
from tqdm import tqdm
cols = {'countryCode': np.float32, 'c2': np.float32, 'c4': np.float32, 'size': np.float32, 'mediationProviderVersion': np.int32, 'bidFloorPrice': np.float32, 'sentPrice': np.float32, 'winBid': np.float32, 'c1std_bidWin': np.float32, 'c1max_bidWin': np.float32, 'c1min_bidWin': np.float32, 'c1mean_bidWin': np.float32, 'c1median_bidWin': np.float32, 'c1sem_bidWin': np.float32, 'c1var_bidWin': np.float32, 'deviceIdstd_bidWin': np.float32, 'deviceIdmax_bidWin': np.float32, 'deviceIdmin_bidWin': np.float32, 'deviceIdmean_bidWin': np.float32, 'deviceIdmedian_bidWin': np.float32, 'deviceIdsem_bidWin': np.float32, 'deviceIdvar_bidWin': np.float32, 'unitDisplayTypestd_bidWin': np.float32, 'unitDisplayTypemax_bidWin': np.float32, 'unitDisplayTypemean_bidWin': np.float32, 'unitDisplayTypemedian_bidWin': np.float32, 'unitDisplayTypesem_bidWin': np.float32, 'unitDisplayTypevar_bidWin': np.float32, 'bundleIdstd_bidWin': np.float32, 'bundleIdmax_bidWin': np.float32, 'bundleIdmin_bidWin': np.float32, 'bundleIdmean_bidWin': np.float32, 'bundleIdmedian_bidWin': np.float32, 'bundleIdsem_bidWin': np.float32, 'bundleIdvar_bidWin': np.float32, 'ver': np.float32, 'unitDisplayType_0': np.uint8, 'unitDisplayType_1': np.uint8, 'unitDisplayType_2': np.uint8, 'connectionType_0': np.uint8, 'connectionType_1': np.uint8, 'connectionType_2': np.uint8, 'connectionType_3': np.uint8, 'bundleId_0': np.uint8, 'bundleId_1': np.uint8, 'bundleId_2': np.uint8, 'bundleId_3': np.uint8, 'bundleId_4': np.uint8, 'bundleId_5': np.uint8, 'bundleId_6': np.uint8, 'bundleId_7': np.uint8, 'bundleId_8': np.uint8, 'bundleId_9': np.uint8, 'bundleId_10': np.uint8, 'bundleId_11': np.uint8, 'bundleId_12': np.uint8, 'bundleId_13': np.uint8, 'bundleId_14': np.uint8, 'bundleId_15': np.uint8, 'bundleId_16': np.uint8, 'bundleId_17': np.uint8}
for x in range(50):
    cols[f'c1_{x}'] = np.uint8
df = pd.read_csv('../input/fork-of-eda-item-price/prepared_train.csv', float_precision='round_trip', dtype=cols)
s = df.deviceIdstd_bidWin.mean()
df['deviceIdstd_bidWin'].fillna(s, inplace=True)
s = df.deviceIdvar_bidWin.mean()
df['deviceIdvar_bidWin'].fillna(s, inplace=True)
s = df.deviceIdsem_bidWin.mean()
df['deviceIdsem_bidWin'].fillna(s, inplace=True)
train, val = train_test_split(df, test_size=0.33, random_state=42)

len(train.columns)

import tensorflow as tf
import glob, math
from IPython.display import clear_output
class Auction_Sequence(tf.keras.utils.Sequence):
    def __init__(self, data, batch_size):
        self.data = data
        self.batch_size = batch_size
    def __len__(self):
        return math.ceil(len(self.data) / self.batch_size)
    def __getitem__(self, idx):
        dc = ['winBid']
        batch = self.data[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_x = batch.drop(dc, axis=1)
        batch_y = batch['winBid']
        return (batch_x.values, batch_y.values)
    def on_epoch_end(self):
        self.data = self.data.sample(frac=1, ignore_index=True, random_state=11)
train_seq = Auction_Sequence(train, 4096)
test_seq = Auction_Sequence(val, 4096)
def get_model(size):
    tf.random.set_seed(11)
    np.random.seed(11)
    activation = tf.keras.layers.LeakyReLU(alpha=0.3)
    feature = tf.keras.Input(shape=(size,))
    hidden_state = tf.keras.layers.Dense(128, activation=activation, name='ind')(feature)
    hidden_state = tf.keras.layers.Dense(64, activation=activation)(hidden_state)
    hidden_state = tf.keras.layers.Dense(32, activation=activation)(hidden_state)
    hidden_state = tf.keras.layers.Dense(16, activation=activation)(hidden_state)
    hidden_state = tf.keras.layers.Dense(8, activation=activation)(hidden_state)
    hidden_state = tf.keras.layers.Dense(4, activation=activation)(hidden_state)
    outputs = tf.keras.layers.Dense(1, activation=None)(hidden_state)
    model = tf.keras.models.Model(inputs=feature, outputs=outputs)
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse')
    return model

from tqdm import tqdm
from sklearn.metrics import f1_score
import shutil
import os
path = f'./models/train_model/'
os.makedirs(path, exist_ok=True)
model = get_model(len(train.columns) - 1)
checkpoint = tf.keras.callbacks.ModelCheckpoint(path + 'model-{val_loss:03f}-{epoch:03d}-.h5', verbose=1, monitor='val_loss', save_weights_only=True, save_best_only=True, mode='min')
model.fit(train_seq, epochs=100, validation_data=test_seq, workers=-1, max_queue_size=10, use_multiprocessing=True, callbacks=[checkpoint], verbose=2)
files = glob.glob(os.path.expanduser(f'{path}*'))
best_weight = sorted(files, key=os.path.getmtime)[-1]
print(best_weight)
model.load_weights(best_weight)
shutil.rmtree(path)
score = float(best_weight.split('-')[1])
model.save(f'train_{score}.h5')
del model, best_weight
gc.collect()
tf.keras.backend.clear_session()