This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |