Skip to content

Instantly share code, notes, and snippets.

View securetorobert's full-sized avatar

Robert John securetorobert

  • Nigeria
View GitHub Profile
@securetorobert
securetorobert / tf_serving_input_fn.py
Created September 18, 2018 09:49
Create a Serving Input Function
def serving_input_fn():
feature_placeholders = {
'crim' : tf.placeholder(tf.float64, [None]),
'zn' : tf.placeholder(tf.float64, [None]),
'indus' : tf.placeholder(tf.float64, [None]),
'chas' : tf.placeholder(tf.int64, [None]),
'nox' : tf.placeholder(tf.float64, [None]),
'rm' : tf.placeholder(tf.float64, [None]),
'age' : tf.placeholder(tf.float64, [None]),
'dis' : tf.placeholder(tf.float64, [None]),
@securetorobert
securetorobert / tf_train_and_evaluate.py
Created September 18, 2018 09:51
Train and Evaluate
def train_and_evaluate(output_dir, num_train_steps):
estimator = tf.estimator.LinearRegressor(
model_dir = output_dir,
feature_columns = feature_cols)
train_spec=tf.estimator.TrainSpec(
input_fn = read_dataset('./train_headless.csv', mode = tf.estimator.ModeKeys.TRAIN),
max_steps = num_train_steps)
exporter = tf.estimator.LatestExporter('exporter', serving_input_fn)
eval_spec=tf.estimator.EvalSpec(
input_fn = read_dataset('./valid_headless.csv', mode = tf.estimator.ModeKeys.EVAL),
@securetorobert
securetorobert / knn.py
Created October 11, 2018 23:55
An sklearn clustering example
from sklearn.neighbors import NearestNeighbors
nn = NearestNeighbors(n_neighbors=5, radius=2.0)
nn.fit(users)
user = np.array([1, 4, 4, 5])
d, neighbors = nn.kneighbors(user.reshape(1, -1))
print(neighbors)
@securetorobert
securetorobert / keras_collaborative_filtering.py
Last active October 12, 2018 12:59
Various collaborative filtering approaches in Keras
import pandas as pd
import numpy as np
from tensorflow import keras
ratings = pd.read_csv('./train.csv')
test_df = pd.read_csv('./test.csv')
test_df['Rating'] = 0
merged_temp = pd.concat([ratings, test_df], axis=0)
merged_temp.reindex()
import tensorflow as tf
from tensorflow import keras
model = keras.models.Sequential([
keras.layers.Dense(4, activation=tf.nn.relu, input_shape=(4,)),
keras.layers.Dense(4, activation=tf.nn.relu),
keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
@securetorobert
securetorobert / tf_keras_sequential_regularized.py
Created January 14, 2019 07:04
Simple Keras Model with Regularization
import tensorflow as tf
from tensorflow import keras
model = keras.models.Sequential([
keras.layers.Dense(4, activation=tf.nn.relu, input_shape=(4,), kernel_regularizer=keras.regularizers.l2(l=0.1)),
keras.layers.Dense(4, activation=tf.nn.relu, kernel_regularizer=keras.regularizers.l2(l=0.1)),
keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
@securetorobert
securetorobert / tf_keras_sequential_l1_l2.py
Created January 14, 2019 07:10
Simple Keras Model with l1_l2 regularization
import tensorflow as tf
from tensorflow import keras
model = keras.models.Sequential([
keras.layers.Dense(4, activation=tf.nn.relu, input_shape=(4,), kernel_regularizer=keras.regularizers.l1_l2(l1=0.1, l2=0.01)),
keras.layers.Dense(4, activation=tf.nn.relu, kernel_regularizer=keras.regularizers.l1_l2(l1=0.1, l2=0.01)),
keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
@securetorobert
securetorobert / tf_2_image_dataset.py
Created May 11, 2019 19:44
A gist showing code for preprocessing images for tf.keras
AUTOTUNE = tf.data.experimental.AUTOTUNE
path_ds = tf.data.Dataset.from_tensor_slices(train_file_list)
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(train_label_list, tf.int64))
image_label_ds = tf.data.Dataset.zip((image_ds, label_ds))
ds = image_label_ds.shuffle(buffer_size=1000 * BATCH_SIZE)
ds = ds.repeat()
ds = ds.batch(BATCH_SIZE)
@securetorobert
securetorobert / bqml_create_model.sql
Created May 19, 2019 16:53
Create a Logistic Regression model with BQML
#standardSQL
CREATE MODEL `bqml_tutorial.sample_model`
OPTIONS(model_type='logistic_reg') AS
SELECT
IF(totals.transactions IS NULL, 0, 1) AS label,
IFNULL(device.operatingSystem, "") AS os,
device.isMobile AS is_mobile,
IFNULL(geoNetwork.country, "") AS country,
IFNULL(totals.pageviews, 0) AS pageviews
FROM
@securetorobert
securetorobert / bqml_evaluate_model.sql
Last active May 20, 2019 01:57
Evaluate a model with BQML
SELECT
*
FROM
ML.EVALUATE(MODEL `bqml_tutorial.sample_model`,
(
SELECT
IF(totals.transactions IS NULL, 0, 1) AS label,
IFNULL(device.operatingSystem, "") AS os,
device.isMobile AS is_mobile,
IFNULL(geoNetwork.country, "") AS country,