import tensorflow as tf
from tensorflow.estimator import DNNRegressor, LinearRegressor
import keras
from keras.layers import Dense, add, Input
from keras.models import Model, Sequential
from keras.utils import to_categorical
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping
from sklearn.preprocessing import MinMaxScaler
%matplotlib inline
import matplotlib.pyplot as plt
early_stopping_monitor = EarlyStopping(patience=5)
with images, first layers identify simple patterns, subsequent layers combine those to identify complex objects
nodes and weights:
4 steps:
for classification use 'relu' in hidden layers, 'softmax' in output layer with >2 classes otherwise sigmoid
critical and may cause dying neuron: neuron takes values <0 for all rows of data, so slopes are 0 and weights don't get updated
Optimizers:
model = Sequential()
model.add(Dense(16, activation='relu', input_shape=(2,)))
model.add(Dense(8, activation='relu'))
model.add(Dense(4, activation='softmax'))
model.compile('adam', loss='categorical_crossentropy')
print(model.summary())
m1_inputs = Input(shape=(2,))
m1_layer1 = Dense(12, activation='sigmoid')(m1_inputs)
m1_layer2 = Dense(4, activation='softmax')(m1_layer1)
m2_inputs = Input(shape=(2,))
m2_layer1 = Dense(8, activation='relu')(m2_inputs)
m2_layer2 = Dense(4, activation='softmax')(m2_layer1)
merged = add([m1_layer2, m2_layer2])
model = Model(inputs=[m1_inputs, m2_inputs], outputs=merged)
print(model.summary())
df = pd.read_csv('./data/hourly_wages.csv')
train_df = df[:int(len(df)*0.9)]
test_df = df[int(len(df)*0.9):]
train_features_df = train_df.drop('wage_per_hour', axis=1)
train_target_df = train_df['wage_per_hour']
test_features_df = test_df.drop('wage_per_hour', axis=1)
test_target_df = test_df['wage_per_hour']
n_features = features_df.shape[1]
model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(n_features,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
#print("Loss function: " + model.loss)
model.fit(train_features_df, train_target_df, epochs=10, validation_split=0.3, verbose=False)
score = model.evaluate(test_features_df, test_target_df)
print(f"Evaluation score: {np.sqrt(score):0.2f}")
df = pd.read_csv('./data/titanic_all_numeric.csv')
train_df = df[:int(len(df)*0.9)]
test_df = df[int(len(df)*0.9):]
train_features_df = train_df.drop('survived', axis=1)
train_target_df = to_categorical(train_df['survived'])
test_features_df = test_df.drop('survived', axis=1)
test_target_df = to_categorical(test_df['survived'])
n_features = features_df.shape[1]
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(n_cols,)))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
# Fit the model
model.fit(train_features_df, train_target_df, epochs=10, verbose=False, validation_split=0.3,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df)
print(f"Evaluation accuracy: {score[1]:0.2f}")
model.save('./models/titanic_model.h5')
predictions = model.predict(predictors)
predicted_prob_true = predictions[:,1]
# print predicted_prob_true
print(predicted_prob_true[:5])
def get_new_model(input_shape):
model = Sequential()
model.add(Dense(100, activation='relu', input_shape=input_shape))
model.add(Dense(100, activation='relu'))
model.add(Dense(2, activation='softmax'))
return (model)
lr_to_test = [0.000001, 0.01, 1]
# Loop over learning rates
for lr in lr_to_test:
print('\nTesting model with learning rate: %f'%lr )
model = get_new_model((n_cols,))
model.compile(optimizer=SGD(lr=lr), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_features_df, train_target_df, epochs=10, verbose=False, validation_split=0.3,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation accuracy: {score[1]:0.2f}")
#model_1
model_1 = Sequential()
model_1.add(Dense(10, activation='relu', input_shape = (n_cols,)))
model_1.add(Dense(10, activation='relu'))
model_1.add(Dense(2, activation='softmax'))
#model_2
model_2 = Sequential()
model_2.add(Dense(50, activation='relu', input_shape = (n_cols,)))
model_2.add(Dense(50, activation='relu'))
model_2.add(Dense(50, activation='relu'))
model_2.add(Dense(2, activation='softmax'))
model_1.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model_2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model_1_training = model_1.fit(train_features_df, train_target_df, epochs=20, validation_split=0.3,
callbacks=[early_stopping_monitor], verbose=False)
model_2_training = model_2.fit(train_features_df, train_target_df, epochs=20, validation_split=0.3,
callbacks=[early_stopping_monitor], verbose=False)
score = model_1.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation accuracy model 1: {score[1]:0.2f}")
score = model_2.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation accuracy model 2: {score[1]:0.2f}")
# Create the plot
plt.plot(model_1_training.history['val_loss'], 'r', model_2_training.history['val_loss'], 'b')
plt.xlabel('Epochs')
plt.ylabel('Validation score')
plt.show()
df = pd.read_csv('./data/mnist.csv', header=None)
train_df = df[:int(len(df)*0.9)]
test_df = df[int(len(df)*0.9):]
y_train = to_categorical(train_df.iloc[:,0])
X_train = train_df.iloc[:,1:]
y_test = to_categorical(test_df.iloc[:,0])
X_test = test_df.iloc[:,1:]
early_stopping_monitor = EarlyStopping(patience=5)
model = Sequential()
model.add(Dense(20, activation='relu', input_shape=(784,)))
model.add(Dense(20, activation='relu', input_shape=(784,)))
model.add(Dense(20, activation='relu', input_shape=(784,)))
model.add(Dense(20, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Fit the model
model.fit(X_train,y_train,validation_split=0.3, verbose=False, epochs=50, callbacks=[early_stopping_monitor])
score = model.evaluate(X_test, y_test, verbose=False)
print(f"Evaluation accuracy: {score[1]:0.2f}")
early_stopping_monitor = EarlyStopping(patience=5)
df = pd.read_csv('./data/slmnist.csv', header=None, dtype=np.float64)
target_df = np.array(pd.get_dummies(df.iloc[:,0]))
features_df = df.iloc[:,1:]
features_df = pd.DataFrame(MinMaxScaler().fit_transform(features_df), columns=features_df.columns)
train_features_df = features_df[:int(len(features_df)*0.9)]
train_target_df = target_df[:int(len(target_df)*0.9)]
test_features_df = features_df[int(len(features_df)*0.9):]
test_target_df = target_df[int(len(target_df)*0.9):]
model = Sequential()
model.add(Dense(16, activation='relu', input_shape=(784,)))
model.add(Dense(4, activation='softmax'))
model.compile('SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_features_df, train_target_df, validation_split=0.3, verbose=False, epochs=5,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation train accuracy: {score[1]:0.2f}")
score = model.evaluate(train_features_df, train_target_df, verbose=False)
print(f"Evaluation test accuracy: {score[1]:0.2f}")
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(784,)))
model.add(Dense(4, activation='softmax'))
model.compile('SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_features_df, train_target_df, validation_split=0.3, verbose=False, epochs=5,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation train accuracy: {score[1]:0.2f}")
score = model.evaluate(train_features_df, train_target_df, verbose=False)
print(f"Evaluation test accuracy: {score[1]:0.2f}")
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(784,)))
model.add(Dense(4, activation='softmax'))
model.compile('SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_features_df, train_target_df, validation_split=0.3, verbose=False, epochs=5,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation train accuracy: {score[1]:0.2f}")
score = model.evaluate(train_features_df, train_target_df, verbose=False)
print(f"Evaluation test accuracy: {score[1]:0.2f}")
model = Sequential()
model.add(Dense(16, activation='relu', input_shape=(784,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(4, activation='softmax'))
model.compile('SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_features_df, train_target_df, validation_split=0.3, verbose=False, epochs=5,
callbacks=[early_stopping_monitor])
score = model.evaluate(test_features_df, test_target_df, verbose=False)
print(f"Evaluation train accuracy: {score[1]:0.2f}")
score = model.evaluate(train_features_df, train_target_df, verbose=False)
print(f"Evaluation test accuracy: {score[1]:0.2f}")
tf.logging.set_verbosity(0)
housing = pd.read_csv('./data/kc_house_data.csv')
bedrooms = tf.feature_column.numeric_column("bedrooms")
bathrooms = tf.feature_column.numeric_column("bathrooms")
sqft_living = tf.feature_column.numeric_column("sqft_living")
# Define the list of feature columns
feature_list = [bedrooms, bathrooms, sqft_living]
def input_fn():
labels = np.array(housing['price'])
features = {'bedrooms':np.array(housing['bedrooms']),
'bathrooms':np.array(housing['bathrooms']),
'sqft_living':np.array(housing['sqft_living'])
}
return features, labels
model = DNNRegressor(feature_columns=feature_list, hidden_units=[4,200])
model_trainning = model.train(input_fn, steps=100)
score = model.evaluate(input_fn, steps=10)
print(score)
print(f"Mean error from evaluation: {np.sqrt(score['average_loss']):0.2f}")
model = tf.estimator.LinearRegressor(feature_columns=feature_list)
model_trainning = model.train(input_fn, steps=500)
score = model.evaluate(input_fn, steps=500)
print(score)
print(f"Mean error from evaluation: {np.sqrt(score['average_loss']):0.2f}")