We are now trying to deploy our Deep Learning model onto Google Cloud. It is required to use Google Function to trigger the Deep Learning predictions. However, when pre-trained models are stored on cloud, it is impossible to get the exact directory path and restore the tensorflow session like what we did on local machine.

So we turn to use SavedModel, which is quite like a 'Prediction Mode' of tensorflow. According to official turotial: a SavedModel contains a complete TensorFlow program, including weights and computation. It does not require the original model building code to run, which makes it useful for sharing or deploying.

The Definition of our graph, just here to show the input and output tensors:

'''RNN Model Definition'''
tf.reset_default_graph()
''''''
#define inputs
tf_x = tf.placeholder(tf.float32, [None, window_size,1],name='x')
tf_y = tf.placeholder(tf.int32, [None, 2],name='y') cells = [tf.keras.layers.LSTMCell(units=n) for n in num_units]
stacked_rnn_cell = tf.keras.layers.StackedRNNCells(cells)
outputs, (h_c, h_n) = tf.nn.dynamic_rnn(
stacked_rnn_cell, # cell you have chosen
tf_x, # input
initial_state=None, # the initial hidden state
dtype=tf.float32, # must given if set initial_state = None
time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
)
l1 = tf.layers.dense(outputs[:, -1, :],32,activation=tf.nn.relu,name='l1')
l2 = tf.layers.dense(l1,8,activation=tf.nn.relu,name='l6')
pred = tf.layers.dense(l2,2,activation=tf.nn.relu,name='pred') with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf_y, logits=pred)
loss = tf.reduce_mean(cross_entropy)
tf.summary.scalar("loss",tensor=loss)
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(tf_y, axis=1), tf.argmax(pred, axis=1)), tf.float32)) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
saver = tf.train.Saver()

Train and Save the model, we use simple_save:

sess = tf.Session()
sess.run(init_op) for i in range(0,n):
sess.run(train_op,{tf_x:batch_X , tf_y:batch_y})
...
tf.saved_model.simple_save(sess, 'simple_save/model', \
inputs={"x": tf_x},outputs={"pred": pred})
sess.close()

Restore and Predict:

with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ["serve"], 'simple_save_test/model')
batch = sess.run('pred/Relu:0',feed_dict={'x:0':dataX.reshape([-1,24,1])})
print(batch)

Reference:

medium post: https://medium.com/@jsflo.dev/saving-and-loading-a-tensorflow-model-using-the-savedmodel-api-17645576527

The official tutorial of Tensorflow: https://www.tensorflow.org/guide/saved_model

最新文章

  1. GJM :Unity3d导出eclipse工程,导入Android Studio
  2. JavaScript函数的4种调用方法详解
  3. 导出 SQL SERVER 表中数据为脚本
  4. 十三、Java基础---------多线程总结
  5. xml约束DTD演示
  6. python批量改动指定文件夹文件名称
  7. A Distributed Multichannel MAC Protocol for Multihop Cognitive Radio Networks
  8. Layer 中自定义属性的动画
  9. CSS3秘笈:第十章
  10. UNIX网络编程——尝试探索基于Linux C的网卡抓包过程
  11. LAV Filter 源代码分析 4: LAV Video (2)
  12. 同步Name到Comment 及 同步 Comment 到Name
  13. 013_RomanToInteger
  14. [luogu3246][bzoj4540][HNOI2016]序列【莫队+单调栈】
  15. 吴裕雄 python深度学习与实践(9)
  16. 使用Ansible实现数据中心自动化运维管理
  17. Docker Compose 容器编排
  18. oracle.exe 内存占用过大
  19. Tomcat架构解析(三)-----Engine、host、context解析以及web应用加载
  20. HDU 5178:pairs(二分,lower_bound和upper_bound)

热门文章

  1. BZOJ 4552(二分+线段树+思维)
  2. python-docx 添加表格时很慢的解决方法
  3. A.Gennady and a Card Game
  4. win32 socket 编程(三)——TCP/IP
  5. js实现-小框框全选
  6. Django 路由层与视图层
  7. this与super的区别
  8. [HNOI2009]有趣的数列(卡塔兰数,线性筛)
  9. 占卜DIY
  10. BZOJ 1233 干草堆 (单调队列优化DP)