Skip to content

Instantly share code, notes, and snippets.

View securetorobert's full-sized avatar

Robert John securetorobert

  • Nigeria
View GitHub Profile
@securetorobert
securetorobert / constants_transfer_learning_tf_20.py
Created September 28, 2019 17:11
Constants for transfer learning in TF 2.0
NCLASSES = 2
HEIGHT = 224
WIDTH = 224
NUM_CHANNELS = 3
BATCH_SIZE = 32
@securetorobert
securetorobert / install_tf_20.py
Created September 28, 2019 17:08
Install TF 2.0 and import libraries
!pip install tensorflow-gpu==2.0.0-beta0
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
@securetorobert
securetorobert / Dockerfile
Created August 15, 2019 12:07
A Dockerfile Sample
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
@securetorobert
securetorobert / bqml_model_predict.sql
Created May 20, 2019 02:15
Get prediction with BQML
#standardSQL
SELECT
*
FROM
ML.PREDICT(MODEL `bqml_tutorial.sample_model`, (
SELECT
IFNULL(device.operatingSystem, "") AS os,
device.isMobile AS is_mobile,
IFNULL(totals.pageviews, 0) AS pageviews,
IFNULL(geoNetwork.country, "") AS country
@securetorobert
securetorobert / bqml_confusion_matrix.sql
Created May 20, 2019 02:06
Confusion Matrix in BQML
SELECT
*
FROM
ML.CONFUSION_MATRIX(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,
@securetorobert
securetorobert / bqml_roc_curve.sql
Created May 20, 2019 02:00
Generate ROC curve in BQML
SELECT
*
FROM
ML.ROC_CURVE(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,
@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,
@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 / 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 / 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'])