Tensorflow
After the "Hello World", you can try some more complex secure service. TEEX supports Tensorlow library in the TestNet. To show how to use it, we provide a "linear regression" demo written with Tensorflow library of Python in our container. After starting the container, it can be found by:
$ cd ~/Demo/linear_regression
The directory looks like:
\linear_regression -- client.opt.conf -- deploy.opt.conf -- linear_regression.py -- input.txt -- README.md
The client.opt.conf
and deploy.opt.conf
are configuration files, which are used to invoke and deploy the service respectively. linear_regression.py
is the service code and input.txt
contains the input string. README.md
briefly introduces the services.
Service Code
Let's first see the linear_regression.py
:
import json import numpy import tensorflow as tf from teex import * rng = numpy.random # Parameters learning_rate = 0.01 training_epochs = 1000 # Training Data train_X = numpy.asarray(json.loads(TEEX_getinput())["x"]) train_Y = numpy.asarray(json.loads(TEEX_getinput())["y"]) n_samples = train_X.shape[0] # Construct a model X = tf.placeholder("float") Y = tf.placeholder("float") W = tf.Variable(rng.randn(), name="weight") b = tf.Variable(rng.randn(), name="bias") pred = tf.add(tf.multiply(X, W), b) cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples) optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) init = tf.global_variables_initializer() # Start training with tf.Session() as sess: # Run the initializer sess.run(init) # Fit all training data for epoch in range(training_epochs): for x, y in zip(train_X, train_Y): sess.run(optimizer, feed_dict={X: x, Y: y}) TEEX_return("Y = %fX + %f" % (sess.run(W), sess.run(b)))
The "linear regression" service gets two arraies X and Y. Then it performs linear regression of them, to calculate the weight and bias.
We first import the Tensorflow library (version 1.5), and teex
library. Then we use TEEX_getinput
to receive the input string, which is a json string looks like:
{ "X": [1.1, 1.5, 2.8, 4.5], "Y": [1.4, 5.5, 7.8, 10] }
The service will get two arrays X and Y from this input string, can invoke the API of Tensorflow to construct a network which can regress a function Y = weight*X + basis
according to the input arrays. After that, the service returns the "Y = weight*X + basis" to the user by invoking TEEX_return
function.
Deployment and Invocation
The deployment and invocation of "linear regression" are same as "Hello World". To deploy the "linear regression" and get a service ID, you only need to type:
$ teex-deploy -c deploy.md
And to invoke the "linear regression", you can type:
$ teex-client -c client.md
Note
Since we limit the memory usage of a service in TEEX TestNet, if your own machine learning service (w/ tensorflow) requires too much memory, it may cause Runtime Error on our TestNet.