Skip to content

Instantly share code, notes, and snippets.

View radi-cho's full-sized avatar

Radi Cho radi-cho

View GitHub Profile
@radi-cho
radi-cho / trainAndSave.js
Created September 23, 2018 19:03
Method which is used to train and save the BoW model.
const trainSave = async (model, querySnapshot) => {
if (!querySnapshot.docs.length) return false;
const xs_data = querySnapshot.docs.map(doc => fitData(doc.data().text));
const ys_data = querySnapshot.docs.map(
doc => (doc.data().y === "positive" ? [1] : [0])
);
const xs = tf.tensor2d(xs_data);
const ys = tf.tensor2d(ys_data);
@radi-cho
radi-cho / train-function.js
Created September 23, 2018 19:07
Check if training from scratch or retraining an existing model is needed.
// TFjs and gcs are using a lot of memory, so increasing it will speed up the functions
exports.train = functions
.runWith({ memory: "2GB" })
.https.onRequest(async (request, response) => {
const existJSON = await bucket.file("model.json").exists().then(ex => ex[0]);
const existBIN = await bucket.file("weights.bin").exists().then(ex => ex[0]);
if (!existJSON || !existBIN) {
const model = tf.sequential();
model.add(tf.layers.dense({ units: 2, inputShape: [vocabulary.length] }));
@radi-cho
radi-cho / proguard-user.txt
Created February 5, 2019 09:30
Proguard config for solving the common Admob Unity error ClassNotFoundException ads.UnityAdListener
-keep class com.google.unity.** {
*;
}
-keep public class com.google.android.gms.ads.**{
public *;
}
-keep public class com.google.ads.**{
public *;
}
-keepattributes *Annotation*
@radi-cho
radi-cho / safeStringify.js
Created May 6, 2020 09:17
JSON.safeStringify method
JSON.safeStringify = (obj, indent = 2) => {
let cache = [];
const retVal = JSON.stringify(
obj,
(key, value) =>
typeof value === "object" && value !== null
? cache.includes(value)
? undefined // Duplicate reference found, discard key
: cache.push(value) && value // Store value in our collection
: value,
temperature = 10
dist = tfp.distributions.RelaxedOneHotCategorical(temperature, probs=p)
dist.sample()
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.31916314, 0.34642866, 0.33440822], dtype=float32)>
temperature = 0.01
dist = tfp.distributions.RelaxedOneHotCategorical(temperature, probs=p)
dist.sample()
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 1., 0.], dtype=float32)>
tf.one_hot(tf.argmax(p), depth = len(p))
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 1., 0.], dtype=float32)>
self.normalize1 = layers.LayerNormalization(epsilon=1e-6)
self.normalize2 = layers.LayerNormalization(epsilon=1e-6)
self.channel_projection1 = keras.Sequential(
[
layers.Dense(units=embedding_dim * 2),
layers.ReLU(),
layers.Dropout(rate=dropout_rate),
]
)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
class gMLPLayer(layers.Layer):
def __init__(self, num_patches, embedding_dim, dropout_rate, *args, **kwargs):
super(gMLPLayer, self).__init__(*args, **kwargs)
def spatial_gating_unit(self, x):
# u and v shape: [batch_size, num_patchs, embedding_dim]
u, v = tf.split(x, num_or_size_splits=2, axis=2)
v = self.normalize2(v)