<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 In this example, we will use tensorflow v1 (version 1.15) to create a simple MLP model, and transfer the application to Cluster Serving step by step.
This tutorial is recommended for Tensorflow v1 user only. If you are not Tensorflow v1 user, the keras tutorial here is more recommended.
Original Tensorflow v1 Application
Step1:  We first define the Tensorflow graph, and create some data.
Step2:  Export TensorFlow SavedModel
Then, we train the graph and in the with tf.Session, we save the graph to SavedModel. The detailed code is following, and we could see the prediction result is [1] with input [1,2].
Step3:  Deploy Cluster Serving
After model prepared, we start to deploy it on Cluster Serving.
First install Cluster Serving
Step4:  We config the model path in config.yaml to following (the detail of config is at Cluster Serving Configuration)
Step5:  Start Cluster Serving
Cluster Serving requires Flink and Redis installed, and corresponded environment variables set, check Cluster Serving Installation Guide for detail.
Flink cluster should start before Cluster Serving starts, if Flink cluster is not started, call following to start a local Flink cluster.
Step6:  After configuration, start Cluster Serving by cluster-serving-start (the detail is at Cluster Serving Programming Guide)
Step7:  Prediction using Cluster Serving
Next we start Cluster Serving code at python client.
<ASSISTANT_TASK:>
Python Code:
import tensorflow as tf
tf.__version__
Explanation: In this example, we will use tensorflow v1 (version 1.15) to create a simple MLP model, and transfer the application to Cluster Serving step by step.
This tutorial is recommended for Tensorflow v1 user only. If you are not Tensorflow v1 user, the keras tutorial here is more recommended.
Original Tensorflow v1 Application
End of explanation
g = tf.Graph()
with g.as_default():
   
    # Graph Inputs
    features = tf.placeholder(dtype=tf.float32, 
                              shape=[None, 2], name='features')
    targets = tf.placeholder(dtype=tf.float32, 
                             shape=[None, 1], name='targets')
    # Model Parameters
    weights = tf.Variable(tf.zeros(shape=[2, 1], 
                          dtype=tf.float32), name='weights')
    bias = tf.Variable([[0.]], dtype=tf.float32, name='bias')
    
    
    # Forward Pass
    linear = tf.add(tf.matmul(features, weights), bias, name='linear')
    ones = tf.ones(shape=tf.shape(linear)) 
    zeros = tf.zeros(shape=tf.shape(linear))
    prediction = tf.where(condition=tf.less(linear, 0.),
                          x=zeros, 
                          y=ones, 
                          name='prediction')
    
    # Backward Pass
    errors = targets - prediction
    weight_update = tf.assign_add(weights, 
                                  tf.reshape(errors * features, (2, 1)),
                                  name='weight_update')
    bias_update = tf.assign_add(bias, errors,
                                name='bias_update')
    
    train = tf.group(weight_update, bias_update, name='train')
    
    saver = tf.train.Saver(name='saver')
import numpy as np
x_train, y_train = np.array([[1,2],[3,4],[1,3]]), np.array([1,2,1])
x_train.shape, y_train.shape
Explanation: We first define the Tensorflow graph, and create some data.
End of explanation
with tf.Session(graph=g) as sess:
    
    sess.run(tf.global_variables_initializer())
    
    for epoch in range(5):
        for example, target in zip(x_train, y_train):
            feed_dict = {'features:0': example.reshape(-1, 2),
                         'targets:0': target.reshape(-1, 1)}
            _ = sess.run(['train'], feed_dict=feed_dict)
    w, b = sess.run(['weights:0', 'bias:0'])    
    print('Model parameters:\n')
    print('Weights:\n', w)
    print('Bias:', b)
    saver.save(sess, save_path='perceptron')
    
    pred = sess.run('prediction:0', feed_dict={features: x_train})
    print(pred)
    
    # in this session, save the model to savedModel format
    inputs = dict([(features.name, features)])
    outputs = dict([(prediction.name, prediction)])
    inputs, outputs
    tf.saved_model.simple_save(sess, "/tmp/mlp_tf1", inputs, outputs)
Explanation: Export TensorFlow SavedModel
Then, we train the graph and in the with tf.Session, we save the graph to SavedModel. The detailed code is following, and we could see the prediction result is [1] with input [1,2].
End of explanation
! pip install analytics-zoo-serving
import os
! mkdir cluster-serving
os.chdir('cluster-serving')
! cluster-serving-init
! tail wget-log
# if you encounter slow download issue like above, you can just use following command to download
# ! wget https://repo1.maven.org/maven2/com/intel/analytics/zoo/analytics-zoo-bigdl_0.12.1-spark_2.4.3/0.9.0/analytics-zoo-bigdl_0.12.1-spark_2.4.3-0.9.0-serving.jar
# if you are using wget to download, or get "analytics-zoo-xxx-serving.jar" after "ls", please call mv *serving.jar zoo.jar after downloaded.
# After initialization finished, check the directory
! ls
# Call mv *serving.jar zoo.jar as mentioned above
! mv *serving.jar zoo.jar
! ls
Explanation: Deploy Cluster Serving
After model prepared, we start to deploy it on Cluster Serving.
First install Cluster Serving
End of explanation
## Analytics-zoo Cluster Serving
model:
  # model path must be provided
  path: /tmp/mlp_tf1
! head config.yaml
Explanation: We config the model path in config.yaml to following (the detail of config is at Cluster Serving Configuration)
End of explanation
! $FLINK_HOME/bin/start-cluster.sh
Explanation: Start Cluster Serving
Cluster Serving requires Flink and Redis installed, and corresponded environment variables set, check Cluster Serving Installation Guide for detail.
Flink cluster should start before Cluster Serving starts, if Flink cluster is not started, call following to start a local Flink cluster.
End of explanation
! cluster-serving-start
Explanation: After configuration, start Cluster Serving by cluster-serving-start (the detail is at Cluster Serving Programming Guide)
End of explanation
from zoo.serving.client import InputQueue, OutputQueue
input_queue = InputQueue()
# Use async api to put and get, you have pass a name arg and use the name to get
arr = np.array([1,2])
input_queue.enqueue('my-input', t=arr)
output_queue = OutputQueue()
prediction = output_queue.query('my-input')
# Use sync api to predict, this will block until the result is get or timeout
prediction = input_queue.predict(arr)
prediction
Explanation: Prediction using Cluster Serving
Next we start Cluster Serving code at python client.
End of explanation
<END_TASK>