Skip to content

Instantly share code, notes, and snippets.

def model_fn(features, labels, mode):
# write the model to compute predictions, loss, etc. from the model
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"probabilities": probabilities,
"classid": class_int, "class": class_str},
loss=loss,
train_op=train_op,
eval_metric_ops=evalmetrics,
@lakshmanok
lakshmanok / compute_prediction.py
Created August 13, 2018 22:59
online prediction with BQML
def compute_prediction(rowdict, numeric_weights, scaling_df, categorical_weights):
input_values = rowdict
# numeric inputs
pred = 0
for column_name in numeric_weights['input'].unique():
wt = numeric_weights[ numeric_weights['input'] == column_name ]['input_weight'].values[0]
if column_name != '__INTERCEPT__':
meanv = scaling_df[ scaling_df['input'] == column_name ]['mean'].values[0]
stddev = scaling_df[ scaling_df['input'] == column_name ]['stddev'].values[0]
scaled_value = (input_values[column_name] - meanv)/stddev
@lakshmanok
lakshmanok / model_train_eval.py
Created August 13, 2018 16:16
How to serve an embedding trained with Estimators
def train_and_evaluate(output_dir, nsteps):
estimator = tf.estimator.Estimator(
model_fn = model_fn,
model_dir = output_dir)
@lakshmanok
lakshmanok / model_premade.py
Created August 13, 2018 16:15
How to serve an embedding trained with Estimators
station_embed = tf.feature_column.embedding_column(
tf.feature_column.categorical_column_with_hash_bucket('start_station_id', 5000, tf.int32), 2)
feature_cols = [
tf.feature_column.categorical_column_with_identity('day_of_week', num_buckets = 8),
station_embed,
tf.feature_column.categorical_column_with_vocabulary_list('rainy', ['false', 'true'])
]
estimator = tf.estimator.LinearRegressor(
model_dir = output_dir,
feature_columns = feature_cols)
@lakshmanok
lakshmanok / model_fn5.py
Created August 13, 2018 16:14
How to serve an embedding trained with Estimators
# 5. Return EstimatorSpec
return spec._replace(predictions = predictions_dict,
export_outputs = export_outputs)
@lakshmanok
lakshmanok / model_fn3.py
Created August 13, 2018 16:14
How to serve an embedding trained with Estimators
# 3. Create predictions
predictions_dict = {
"predicted": predictions,
"station_embed": embed_layer
}
# 4. Create export outputs
export_outputs = {
"predict_export_outputs": tf.estimator.export.PredictOutput(outputs = predictions_dict)
}
@lakshmanok
lakshmanok / model_fn2.py
Created August 13, 2018 16:13
How to serve an embedding trained with Estimators
my_head = tf.contrib.estimator.regression_head()
spec = my_head.create_estimator_spec(
features = features, mode = mode,
labels = labels, logits = predictions,
optimizer = tf.train.FtrlOptimizer(learning_rate = 0.1)
)
@lakshmanok
lakshmanok / model_fn.py
Created August 13, 2018 16:12
How to serve an embedding trained with Estimators (#3)
def model_fn(features, labels, mode):
# linear model
station_col = tf.feature_column.categorical_column_with_hash_bucket('start_station_id', 5000, tf.int32)
station_embed = tf.feature_column.embedding_column(station_col, 2) # embed dimension
embed_layer = tf.feature_column.input_layer(features, station_embed)
cat_cols = [
tf.feature_column.categorical_column_with_identity('day_of_week', num_buckets = 8),
tf.feature_column.categorical_column_with_vocabulary_list('rainy', ['false', 'true'])
]
@lakshmanok
lakshmanok / invoke.sh
Created August 13, 2018 16:11
How to serve an embedding trained with Estimators (#2)
EXPORTDIR=./model_trained/export/exporter/
MODELDIR=$(ls $EXPORTDIR | tail -1)
gcloud ml-engine local predict --model-dir=${EXPORTDIR}/${MODELDIR} --json-instances=./test.json
@lakshmanok
lakshmanok / census_zip.sql
Created July 19, 2018 21:51
bigquery-gis
#standardsql
with zipcodes as (
SELECT
zip_census.zipcode as zipcode,
population,
WKT as geometry,
ST_CENTROID(ST_GeogFromText(WKT)) as centroid
FROM
`bigquery-public-data.census_bureau_usa.population_by_zip_2010` AS zip_census
join `bigquery-public-data-staging.zcta_test.2017` as zip_geom