Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
eileen-code4fun / gcn_model.py
Last active January 6, 2022 21:41
GCN Model
from dgl.nn.tensorflow import GraphConv
class GCN(tf.keras.Model):
def __init__(self, g, feat_dim, hidden_dim, class_num):
super(GCN, self).__init__()
self.g = g
self.h1 = GraphConv(feat_dim, hidden_dim, activation=tf.nn.relu, allow_zero_in_degree=True)
self.dropout = tf.keras.layers.Dropout(0.5)
self.h2 = GraphConv(hidden_dim, class_num), allow_zero_in_degree=True
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=(1433,)))
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(7))
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
@eileen-code4fun
eileen-code4fun / load_data.py
Created January 6, 2022 14:24
Load Cora Dataset
import dgl
import tensorflow as tf
dataset = dgl.data.CoraGraphDataset()
# A DGL dataset may contain multiple graphs.
# In the case of Cora, there is only one graph.
g = dataset[0]
# g.ndata is a dictionary of nodes related data.
import tensorflow_data_validation as tfdv
from tensorflow_metadata.proto.v0 import anomalies_pb2
stats = tfdv.load_stats_binary(GCS_PATH_FOR_STATS_PB)
tfdv.visualize_statistics(stats)
# Display output in screenshot later.
schema = tfdv.load_schema_text(GCS_PATH_FOR_SCHEMA_PBTXT)
tfdv.display_schema(schema)
# Display output in screenshot later.
@eileen-code4fun
eileen-code4fun / cifar10_encode.py
Last active June 14, 2021 14:38
CIFAR10 Encode
import tensorflow as tf
def normalize(image, label):
return tf.cast(image, dtype=tf.float32) / 255.0, label
train = ds['train'].map(normalize).batch(32)
test = ds['test'].map(normalize).batch(32)
encoding_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('flatten').output)
import tensorflow as tf
# Load the saved custom model.
model = tf.keras.models.load_model(GCS_PATH_FOR_SAVED_MODEL)
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
model.summary()
'''
import numpy as np
from sklearn import decomposition
# Reuse the same ds from Tensorflow Dataset
train_data = list(ds['train'].map(lambda x, y: tf.reshape(x, [-1])))
test_data = list(ds['test'].map(lambda x, y: tf.reshape(x, [-1])))
X = tf.concat([train_data, test_data], 0).numpy()
print(np.shape(X))
# Output: (60000, 3072)
SELECT
# Display all metrics.
*
FROM
ML.EVALUATE(
MODEL `bqml.e2e-tutorial`,
(
SELECT
int64_field_0 as label,
* EXCEPT (int64_field_0)
CREATE MODEL
# Name of the model.
`bqml.e2e-tutorial`
OPTIONS
# BigQuery will detect that it's for multiclass.
( MODEL_TYPE='LOGISTIC_REG') AS
SELECT
# Use the first column as the label.
int64_field_0 as label,
# The rest are features.
SELECT * FROM `bqml.train` LIMIT 10;