Skip to content

Instantly share code, notes, and snippets.

View BinRoot's full-sized avatar

Nishant Shukla BinRoot

View GitHub Profile
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
import os
from six.moves import urllib
import zipfile
import tensorflow as tf
import collections
import csv
class DatasetLoader:
def __init__(self):
self.url = 'http://mattmahoney.net/dc/'
import tensorflow as tf
x = tf.Variable(2, name='x', dtype=tf.float32)
log_x = tf.log(x)
y = tf.square(log_x) + 1
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(y)
init = tf.global_variables_initializer()

TensorFlow Serving in 10 minutes!

TensorFlow SERVING is Googles' recommended way to deploy TensorFlow models. Without proper computer engineering background, it can be quite intimidating, even for people who feel comfortable with TensorFlow itself. Few things that I've found particularly hard were:

  • Tutorial examples have C++ code (which I don't know)
  • Tutorials have Kubernetes, gRPG, Bezel (some of which I saw for the first time)
  • It needs to be compiled. That process takes forever!

After all, it worked just fine. Here I present an easiest possible way to deploy your models with TensorFlow Serving. You will have your self-built model running inside TF-Serving by the end of this tutorial. It will be scalable, and you will be able to query it via REST.

import tensorflow as tf
# Define a matrix and negate it
matrix = tf.constant([[1., 2.]])
negMatrix = tf.neg(matrix)
# Start the session with a special config passed into the constructor to enable logging
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
# Evaluate negMatrix
result = sess.run(negMatrix)
import tensorflow as tf
# Define an arbitrary matrix
matrix = tf.constant([[1., 2.]])
# Run the negation operator on it
neg_matrix = tf.neg(matrix)
# Start a session to be able to run operations
with tf.Session() as sess:
import tensorflow as tf
# Define an arbitrary tensor
x = tf.constant([[1, 2]])
# Negate the tensor
neg_x = tf.neg(x)
# Print the object
print(neg_x)
import tensorflow as tf
# Define a 2x1 matrix
matrix1 = tf.constant([[1., 2.]])
# Define a 1x2 matrix
matrix2 = tf.constant([[1],
[2]])
# Define a rank 3 tensor
import tensorflow as tf
# We’ll use NumPy matrices in TensorFlow
import numpy as np
# Define a 2x2 matrix in 3 different ways
m1 = [[1.0, 2.0],
[3.0, 4.0]]
m2 = np.array([[1.0, 2.0],
[3.0, 4.0]], dtype=np.float32)
m3 = tf.constant([[1.0, 2.0],
import numpy as np
revenue = np.dot(prices, amounts)