Skip to content

Instantly share code, notes, and snippets.

@e-lin
e-lin / training.py
Last active October 1, 2019 09:39
Training Phrases for Chatbot
training_phrases = {
'when_is_open' : ' '.join([
'when is the restaurant open?',
'when to start open?'
]),
'where_is_the_restaurant' : ' '.join([
'where is the restaurant',
'what is the location of the restaurant?',
@e-lin
e-lin / trainSingle.py
Created July 9, 2019 12:17
Training one Autoencoder at a time in a single graph
optimizer = tf.train.AdamOptimizer(learning_rate)
with tf.name_scope("phase1"):
phase1_outputs = tf.matmul(hidden1, weights4) + biases4 # bypass hidden2 and hidden3
phase1_reconstruction_loss = tf.reduce_mean(tf.square(phase1_outputs - X))
phase1_reg_loss = regularizer(weights1) + regularizer(weights4)
phase1_loss = phase1_reconstruction_loss + phase1_reg_loss
phase1_training_op = optimizer.minimize(phase1_loss)
with tf.name_scope("phase2"):
@e-lin
e-lin / trainMulti3.py
Last active July 9, 2019 12:17
AutoEncoder: Training one Autoencoder at a time in multiple graphs
# image input
n_inputs = 28*28
# merged to one stacked AE
X = tf.placeholder(tf.float32, shape=[None, n_inputs])
hidden1 = tf.nn.elu(tf.matmul(X, W1) + b1)
hidden2 = tf.nn.elu(tf.matmul(hidden1, W2) + b2)
hidden3 = tf.nn.elu(tf.matmul(hidden2, W3) + b3)
outputs = tf.matmul(hidden3, W4) + b4
@e-lin
e-lin / trainMulti2.py
Last active July 9, 2019 12:17
AutoEncoder: Training one Autoencoder at a time in multiple graphs
# first phase of AE, trained on the training data
hidden_output, W1, b1, W4, b4 = train_autoencoder(mnist.train.images, n_neurons=300, n_epochs=4, batch_size=150,
# second phase of AE, trained on the previous Autoencoder's hidden layer output
_, W2, b2, W3, b3 = train_autoencoder(hidden_output, n_neurons=150, n_epochs=4, batch_size=150)
@e-lin
e-lin / trainMulti1.py
Last active July 9, 2019 12:18
AutoEncoder: Training one Autoencoder at a time in multiple graphs
def train_autoencoder(X_train, n_neurons, n_epochs, batch_size,
learning_rate = 0.01, l2_reg = 0.0005, seed=42,
hidden_activation=tf.nn.elu,
output_activation=tf.nn.elu):
graph = tf.Graph()
with graph.as_default():
tf.set_random_seed(seed)
n_inputs = X_train.shape[1]
@e-lin
e-lin / tyingWeights.py
Created July 9, 2019 09:20
AutoEncoder: Tying weights
X = tf.placeholder(tf.float32, shape=[None, n_inputs])
weights1_init = initializer([n_inputs, n_hidden1])
weights2_init = initializer([n_hidden1, n_hidden2])
weights1 = tf.Variable(weights1_init, dtype=tf.float32, name="weights1")
weights2 = tf.Variable(weights2_init, dtype=tf.float32, name="weights2")
weights3 = tf.transpose(weights2, name="weights3") # tied weights
weights4 = tf.transpose(weights1, name="weights4") # tied weights
@e-lin
e-lin / stitch.js
Created July 2, 2019 21:24
Mongo Stitch Service
exports = async function (payload) {
const mongodb = context.services.get("mongodb-atlas");
const exampledb = mongodb.db("exampledb");
const examplecoll = exampledb.collection("examplecoll");
const args = payload.query.text.split(" ");
switch (args[0]) {
case "stash":
const result = await examplecoll.insertOne({
@e-lin
e-lin / thinglogin.java
Created June 26, 2019 10:34
Log in Thing with Google and Firebase APIs
private fun trySignIn() {
Log.d(TAG, "Signing in with tocken" + tocken)
val credential = GoogleAuthProvider.getCredential(token, null)
FirebaseAuth.getInstance().signInWithCredential(credential)
.addOnSuccessListener(this, { result ->
val user = result.user
Log.d(TAG, "signInWithCredential ${user.displayName} ${user.email}")
finish()
})
.addOnFailureListener(this, { e ->
@e-lin
e-lin / applogin2.java
Created June 26, 2019 10:26
App login with FirebaseUI - handle sign in results
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
// handle login
}
}
}
@e-lin
e-lin / applogin.java
Created June 26, 2019 10:23
App login with FirebaseUI - launch UI flow
findViewById<Button>(R.id.btn_sign_in).setOnClickListener {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(listOf(AuthUI.IdpConfig.GoogleBuilder().build()))
.build(),
RC_SIGN_IN
)
}