Created
December 23, 2020 11:24
-
-
Save SlappyAUS/1f0fda13b3be84241aa567fdfbb1f5f9 to your computer and use it in GitHub Desktop.
Keras Deep Neural Net Template #python #ml #tensorflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup plotting | |
import matplotlib.pyplot as plt | |
from learntools.deep_learning_intro.dltools import animate_sgd | |
plt.style.use('seaborn-whitegrid') | |
# Set Matplotlib defaults | |
plt.rc('figure', autolayout=True) | |
plt.rc('axes', labelweight='bold', labelsize='large', | |
titleweight='bold', titlesize=18, titlepad=10) | |
plt.rc('animation', html='html5') | |
## Preprocessing | |
import numpy as np | |
import pandas as pd | |
from sklearn.preprocessing import StandardScaler, OneHotEncoder | |
from sklearn.compose import make_column_transformer, make_column_selector | |
from sklearn.model_selection import train_test_split | |
fuel = pd.read_csv('../input/dl-course-data/fuel.csv') | |
X = fuel.copy() | |
# Remove target | |
y = X.pop('FE') | |
preprocessor = make_column_transformer( | |
(StandardScaler(), | |
make_column_selector(dtype_include=np.number)), | |
(OneHotEncoder(sparse=False), | |
make_column_selector(dtype_include=object)), | |
) | |
X = preprocessor.fit_transform(X) | |
y = np.log(y) # log transform target instead of standardizing | |
input_shape = [X.shape[1]] | |
print("Input shape: {}".format(input_shape)) | |
## Look at Data | |
#fuel.head() | |
# Uncomment to see processed features | |
pd.DataFrame(X[:10,:]).head() | |
## Modelling | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
model = keras.Sequential([ | |
layers.Dense(128, activation='relu', input_shape=input_shape), | |
layers.Dense(128, activation='relu'), | |
layers.Dense(64, activation='relu'), | |
layers.Dense(1), | |
]) | |
## Compile | |
model.compile( | |
optimizer='adam', | |
loss='mae', | |
) | |
## Train | |
history = model.fit( | |
X_train, y_train, | |
validation_data=(X_valid, y_valid), | |
batch_size=256, | |
epochs=10, | |
) | |
## Plot Loss | |
import pandas as pd | |
# convert the training history to a dataframe | |
history_df = pd.DataFrame(history.history) | |
# use Pandas native plot method | |
history_df['loss'].plot(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment