Skip to content

Instantly share code, notes, and snippets.

@e-lin
e-lin / FirestoreTrigger1.java
Created June 26, 2019 08:24
Firestore trigger 1 - send answer to Android Thing
export const onAnswer = functions.firestore.document('/pic/{picId}').onUpdate(_onAnswer)
async function _onAnswer(change: functions.Change<DocumentSnapshot>): Promise<any> {
const picId = change.before.id "//20180624120000"
const previous = change.before.data() as Pic
const pic = change.after.data() as Pic
// Only interested in pics that have a new answer
if (previous.answer || !pic.answer) {
console.log("This is not the update you are looking for.")
return Promise.resolve()
@e-lin
e-lin / FirestoreTrigger2.java
Created June 26, 2019 08:28
Firestore trigger 2 - send answer to Android Thing
const payload = {
data: {
disposition: pic.answer.disposition.toString(),
pic_id: picId
}
}
try {
const response = await fcm.sendToTopic('answers',payload)
@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
)
}
@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 / 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 / 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 / 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 / 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 / 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 / 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