I am using TensorFlow on ML Engine - Google Cloud Platform to solve a regression problem. I need to send a tensor string that contains a date to ML Engine, something like "2018/06/05 23:00" and from there have my deployed model extract the features which basically are (year, month, day, hour). For the example above would be (2018, 06, 05, 23). The thing is that I need to get this performed in the deployed model in ML Engine and not in an in-between API.
First what I did is to adapt the Census Model Tutorial to my regression problem. https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction
In this tutorial, they deploy the model in ML Engine using the gcloud command gcloud ml-engine models create $MODEL_NAME ... through the terminal.
Below you will find the way I found to manipulate a string tensor that contains a date to obtain the features
import tensorflow as tf
import numpy as np
date_time = tf.placeholder(shape=(1,), dtype=tf.string, name="ph_date_time")
INPUT_COLUMNS=["year", "month", "day", "hour"]
split_date_time = tf.string_split(date_time, ' ')
date = split_date_time.values[0]
time = split_date_time.values[1]
split_date = tf.string_split([date], '-')
split_time = tf.string_split([time], ':')
year = split_date.values[0]
month = split_date.values[1]
day = split_date.values[2]
hours = split_time.values[0]
minutes = split_time.values[1]
year = tf.string_to_number(year, out_type=tf.int32, name="year_temp")
month = tf.string_to_number(month, out_type=tf.int32, name="month_temp")
day = tf.string_to_number(day, out_type=tf.int32, name="day_temp")
hours = tf.string_to_number(hours, out_type=tf.int32, name="hour_temp")
minutes = tf.string_to_number(minutes, out_type=tf.int32, name="minute_temp")
year = tf.expand_dims(year, 0, name="year")
month = tf.expand_dims(month, 0, name="month")
day = tf.expand_dims(day, 0, name="day")
hours = tf.expand_dims(hours, 0, name="hours")
minutes = tf.expand_dims(minutes, 0, name="minutes")
features = []
features = np.append(features, year)
features = np.append(features, month)
features = np.append(features, day)
features = np.append(features, hours)
# this would be the actual features to the deployed model
actual_features = dict(zip(INPUT_COLUMNS, features))
with tf.Session() as sess:
year, month, day, hours, minutes = sess.run([year, month, day, hours, minutes], feed_dict={date_time: ["2018-12-31 22:59"]})
print("Year =", year)
print("Month =", month)
print("Day =", day)
print("Hours =", hours)
print("Minutes =", minutes)
The thing is that I don't know how to tell the ML Engine to use this parsing above. I know it has to do with either the input_fn to define the model or the serving_input_fn used to export the model, but I am not sure if I have to paste my code in both or one of them, any recommendation would be highly appreciate it, and sorry if the explanation is not clear.