Advertisement
makispaiktis

Kaggle 5 - Dropout and BatchNormalization

Jul 13th, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. # Set Matplotlib defaults
  3. plt.style.use('seaborn-whitegrid')
  4. plt.rc('figure', autolayout=True)
  5. plt.rc('axes', labelweight='bold', labelsize='large', titleweight='bold', titlesize=18, titlepad=10)
  6. plt.rc('animation', html='html5')
  7.  
  8. import pandas as pd
  9. from sklearn.preprocessing import StandardScaler, OneHotEncoder
  10. from sklearn.compose import make_column_transformer
  11. from sklearn.model_selection import GroupShuffleSplit
  12. from tensorflow import keras
  13. from tensorflow.keras import layers
  14. from tensorflow.keras import callbacks
  15.  
  16.  
  17.  
  18. # 0. Auxiliary Function
  19. def group_split(X, y, group, train_size=0.75):
  20.     splitter = GroupShuffleSplit(train_size=train_size)
  21.     train, test = next(splitter.split(X, y, groups=group))
  22.     return (X.iloc[train], X.iloc[test], y.iloc[train], y.iloc[test])
  23.  
  24.  
  25.  
  26.  
  27. # 1a. Read dataset 1
  28. spotify = pd.read_csv('../input/dl-course-data/spotify.csv')
  29. X = spotify.copy().dropna()
  30. y = X.pop('track_popularity')
  31.  
  32. # 1b. Target and predictors (numerical and categorical)
  33. artists = X['track_artist']
  34. features_num = ['danceability', 'energy', 'key', 'loudness', 'mode',
  35.                 'speechiness', 'acousticness', 'instrumentalness',
  36.                 'liveness', 'valence', 'tempo', 'duration_ms']
  37. features_cat = ['playlist_genre']
  38.  
  39. # 1c. Preprocessor
  40. preprocessor = make_column_transformer( (StandardScaler(), features_num),
  41.                                         (OneHotEncoder(), features_cat) )
  42.  
  43. X_train, X_valid, y_train, y_valid = group_split(X, y, artists)
  44. X_train = preprocessor.fit_transform(X_train)
  45. X_valid = preprocessor.transform(X_valid)
  46. y_train = y_train / 100
  47. y_valid = y_valid / 100
  48.  
  49. input_shape = [X_train.shape[1]]
  50. print("Input shape: {}".format(input_shape))
  51.  
  52.  
  53.  
  54. # 2. Adding DROPOUT to dataset 1
  55. model = keras.Sequential([ layers.Dense(128, activation='relu', input_shape=input_shape),
  56.                            layers.Dropout(0.3),
  57.                            layers.Dense(64, activation='relu'),
  58.                            layers.Dropout(0.3),
  59.                            layers.Dense(1) ])
  60.  
  61. model.compile(optimizer='adam', loss='mae')
  62.  
  63. history = model.fit(X_train, y_train,
  64.                     validation_data=(X_valid, y_valid),
  65.                     batch_size=512,
  66.                     epochs=50,
  67.                     verbose=0)
  68.  
  69. history_df = pd.DataFrame(history.history)
  70. history_df.loc[:, ['loss', 'val_loss']].plot()
  71. print("Minimum Validation Loss: {:0.4f}".format(history_df['val_loss'].min()))
  72.  
  73.  
  74.  
  75. # 3. Read dataset 2 (NO separating target from predictors, NO preprocessing, NOTHING at all)
  76. concrete = pd.read_csv('../input/dl-course-data/concrete.csv')
  77. df = concrete.copy()
  78. df_train = df.sample(frac=0.7, random_state=0)
  79. df_valid = df.drop(df_train.index)
  80.  
  81. X_train = df_train.drop('CompressiveStrength', axis=1)
  82. X_valid = df_valid.drop('CompressiveStrength', axis=1)
  83. y_train = df_train['CompressiveStrength']
  84. y_valid = df_valid['CompressiveStrength']
  85. input_shape = [X_train.shape[1]]
  86.  
  87.  
  88.  
  89. # 4. No dropout yet, no batch normalization yet
  90. model = keras.Sequential([ layers.Dense(512, activation='relu', input_shape=input_shape),
  91.                            layers.Dense(512, activation='relu'),    
  92.                            layers.Dense(512, activation='relu'),
  93.                            layers.Dense(1) ])
  94.  
  95. model.compile(optimizer='sgd', loss='mae', metrics=['mae'])
  96.  
  97. history = model.fit(X_train, y_train,
  98.                     validation_data=(X_valid, y_valid),
  99.                     batch_size=64,
  100.                     epochs=100,
  101.                     verbose=0)
  102.  
  103. history_df = pd.DataFrame(history.history)
  104. history_df.loc[0:, ['loss', 'val_loss']].plot()
  105. print(("Minimum Validation Loss: {:0.4f}").format(history_df['val_loss'].min()))
  106. print("Did you end up with a blank graph? Trying to train this network on this dataset will usually fail. Even when it does converge (due to a lucky weight initialization), it tends to converge to a very large number.", end="\n\n\n")
  107.  
  108.  
  109.  
  110. # 5. Adding batch normalization before every layer
  111. model = keras.Sequential([ layers.BatchNormalization(),
  112.                            layers.Dense(512, activation='relu', input_shape=input_shape),
  113.                            layers.BatchNormalization(),
  114.                            layers.Dense(512, activation='relu'),
  115.                            layers.BatchNormalization(),
  116.                            layers.Dense(512, activation='relu'),
  117.                            layers.BatchNormalization(),
  118.                            layers.Dense(1) ])
  119.  
  120. model.compile(optimizer='sgd', loss='mae', metrics=['mae'])
  121.  
  122. history = model.fit(X_train, y_train,
  123.                     validation_data=(X_valid, y_valid),
  124.                     batch_size=64,
  125.                     epochs=100,
  126.                     verbose=0)
  127.  
  128. history_df = pd.DataFrame(history.history)
  129. history_df.loc[0:, ['loss', 'val_loss']].plot()
  130. print(("Minimum Validation Loss: {:0.4f}").format(history_df['val_loss'].min()))
  131. print("You can see that adding batch normalization was a big improvement on the first attempt! By adaptively scaling the data as it passes through the network, batch normalization can let you train models on difficult datasets.", end="\n\n\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement