Skip to content

Instantly share code, notes, and snippets.

View securetorobert's full-sized avatar

Robert John securetorobert

  • Nigeria
View GitHub Profile
@securetorobert
securetorobert / neural_networks_boston_data_nn.py
Last active September 19, 2018 02:09
Neural Networks with Keras on Boston Housing data
#imports
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keral.layers import Dense
#build our model
model = Sequential()
model.add(Dense(50, activation='relu'))
@securetorobert
securetorobert / neural_networks_boston_data_training.py
Created July 12, 2018 23:53
Neural Networks with Keras on Boston Housing data
X = scaled_train_df.drop(target, axis=1).values
Y = scaled_train_df[[target]].values
# Train the model
model.fit(
X[10:],
Y[10:],
epochs=50,
shuffle=True,
verbose=2
@securetorobert
securetorobert / neural_networks_boston_housing_inference.py
Created July 12, 2018 23:56
Neural Networks with Keras on Boston Housing data
#inference
prediction = model.predict(X[:1])
y_0 = prediction[0][0]
print('Prediction with scaling - {}',format(y_0))
y_0 -= added
y_0 /= multiplied_by
print("Housing Price Prediction - ${}".format(y_0))
@securetorobert
securetorobert / tf_input_function.py
Created July 15, 2018 10:28
An input function for use with TensorFlow Estimators
def my_input_fn(file_path, perform_shuffle=False, repeat_count=1):
def decode_csv(line):
parsed_line = tf.decode_csv(line, [[0.], [0.], [0.], [0.], [0]])
label = parsed_line[-1:] # Last element is the label
del parsed_line[-1] # Delete last element
features = parsed_line # Everything (but last element) are the features
d = dict(zip(feature_names, features)), label
return d
dataset = (tf.data.TextLineDataset(file_path) # Read text file
@securetorobert
securetorobert / tf_memory_input_function.py
Created July 15, 2018 10:35
An input function using data already in memory for iris predictions
# Let create a memory dataset for prediction.
# We've taken the first 3 examples in FILE_TEST.
prediction_input = [[5.9, 3.0, 4.2, 1.5], # -> 1, Iris Versicolor
[6.9, 3.1, 5.4, 2.1], # -> 2, Iris Virginica
[5.1, 3.3, 1.7, 0.5]] # -> 0, Iris Sentosa
def new_input_fn():
def decode(x):
x = tf.split(x, 4) # Need to split into our 4 features
# When predicting, we don't need (or have) any labels
return dict(zip(feature_names, x)) # Then build a dict from them
@securetorobert
securetorobert / tf_estimator_dnnclassifier_iris.py
Created July 15, 2018 10:37
A DNNClassifier for iris dataset
feature_names = [
'SepalLength',
'SepalWidth',
'PetalLength',
'PetalWidth']
# Create the feature_columns, which specifies the input to our model.
# All our input features are numeric, so use numeric_column for each one.
feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names]
@securetorobert
securetorobert / tf_estimator_dnnclassifier_iris_train.py
Created July 15, 2018 10:38
Training a DNNClassifier on the Iris Dataset
# Train our model, use the previously function my_input_fn
# Input to training is a file with training example
# Stop training after 8 iterations of train data (epochs)
classifier.train(
input_fn=lambda: my_input_fn(FILE_TRAIN, True, 8))
@securetorobert
securetorobert / tf_estimator_dnn_classifier_iris_evaluate.py
Created July 15, 2018 10:45
Evaluate a DNNClassifer on iris dataset
# Evaluate our model using the examples contained in FILE_TEST
# Return value will contain evaluation_metrics such as: loss & average_loss
evaluate_result = estimator.evaluate(
input_fn=lambda: my_input_fn(FILE_TEST, False, 4)
print("Evaluation results")
for key in evaluate_result:
print(" {}, was: {}".format(key, evaluate_result[key]))
@securetorobert
securetorobert / tf_estimator_dnn_classifier_iris_predict.py
Created July 15, 2018 10:50
Predictions using DNNClassifier on iris dataset
# Predict the type of some Iris flowers.
# Let's predict the examples in FILE_TEST, repeat only once.
predict_results = classifier.predict(
input_fn=lambda: my_input_fn(FILE_TEST, False, 1))
print("Predictions on test file")
for prediction in predict_results:
# Will print the predicted class, i.e: 0, 1, or 2 if the prediction
# is Iris Sentosa, Vericolor, Virginica, respectively.
print prediction["class_ids"][0]
@securetorobert
securetorobert / tf_feature_column_bucketized_column.py
Created July 15, 2018 11:09
Create a bucketized column from a numeric column
# A numeric column for the raw input.
numeric_feature_column = tf.feature_column.numeric_column("Year")
# Bucketize the numeric column on the years 1960, 1980, and 2000
bucketized_feature_column = tf.feature_column.bucketized_column(
source_column = numeric_feature_column,
boundaries = [1960, 1980, 2000])