【问题标题】:Export a basic Tensorflow model to Google Cloud ML将基本 TensorFlow 模型导出到 Google Cloud ML
【发布时间】:2017-04-17 18:07:41
【问题描述】:

我正在尝试导出我的本地 tensorflow 模型以在 Google Cloud ML 上使用它并对其运行预测。

我正在关注tensorflow serving example with mnist data。他们处理和使用输入/输出向量的方式有很大差异,这不是您在网上的典型示例中找到的。

我不确定如何设置我的签名参数:

model_exporter.init(
    sess.graph.as_graph_def(),
    init_op = init_op,
    default_graph_signature = exporter.classification_signature(
        input_tensor = "**UNSURE**" ,
        scores_tensor = "**UNSURE**"),
    named_graph_signatures = {
        'inputs' : "**UNSURE**",
        'outputs': "**UNSURE**"
    }

    )
model_exporter.export(export_path, "**UNSURE**", sess)

这是我的其余代码:

import sys
import tensorflow as tf
from tensorflow.contrib.session_bundle import exporter

import numpy as np
from newpreprocess import create_feature_sets_and_labels

train_x,train_y,test_x,test_y = create_feature_sets_and_labels()

x = tf.placeholder('float', [None, 13])
y = tf.placeholder('float', [None, 1])

n_nodes_hl1 = 20
n_nodes_hl2 = 20

n_classes = 1
batch_size = 100

def neural_network_model(data):

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([13, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}


    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.tanh(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.tanh(l2)

    output = tf.add(tf.matmul(l2, output_layer['weights']), output_layer['biases'])
    return output



def train_neural_network(x):
    output = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(output, y))
    optimizer = tf.train.AdamOptimizer(0.003).minimize(cost)

    hm_epochs = 700

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            while i < len(train_x):
                start = i
                end = i + batch_size
                batch_x = np.array(train_x[start:end])
        batch_y = np.array(train_y[start:end])

        _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                              y: batch_y})
        epoch_loss += c
        i+=batch_size

            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss/(len(train_x)/batch_size))


        prediction = tf.sigmoid(output)
        predicted_class = tf.greater(prediction,0.5)
        correct = tf.equal(predicted_class, tf.equal(y,1.0))
        accuracy = tf.reduce_mean( tf.cast(correct, 'float') )

        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))

        export_path = "~/Documents/cloudcomputing/Project/RNN_timeseries/"
        print ("Exporting trained model to %s", %export_path)
        init_op = tf.group(tf.initialize_all_tables(), name="init_op")
        saver = tf.train.Saver(sharded = True)
        model_exporter = exporter.Exporter(saver)
        model_exporter.init(
            sess.graph.as_graph_def(),
            init_op = init_op,
            default_graph_signature = exporter.classification_signature(
                input_tensor = ,
                scores_tensor = ),
            named_graph_signatures = {
                'inputs' : ,
                'outputs': 
            }

            )
        model_exporter.export(export_path, tf.constant(1), sess)
        print("Done exporting!")



train_neural_network(x)

在 Google Cloud ML 上上传和使用它的具体步骤是什么?他们的演练似乎是针对在云本身而不是在本地机器上训练的模型。

【问题讨论】:

  • 虽然演练演示了在云上的培训,但您可以按照大部分相同的步骤在本地进行培训,然后部署到云端。无论哪种情况,您最终都会得到一个包含导出模型的目录,并且您只需要在部署模型时指向该目录(如果您不使用 gcloud,则需要确保将模型复制到GCS)。

标签: python machine-learning tensorflow tensorflow-serving google-cloud-ml


【解决方案1】:

Tensorflow Serving 和 Google Cloud ML 是两个不同的东西,不要混淆它们。 Cloud ML 是一个完全托管的解决方案(ML 即服务),而 TF Serving 需要您设置和维护您的基础架构 - 它只是一个服务器。它们是不相关的,并且在输入/输出处理方面有不同的要求。

您应该遵循的指南是this one。您可以将输入和输出添加到集合中,而不是使用图形签名。您的代码中的更改将是这样的:

import sys
import tensorflow as tf
from tensorflow.contrib.session_bundle import exporter

import numpy as np
from newpreprocess import create_feature_sets_and_labels
import json 
import os 

train_x,train_y,test_x,test_y = create_feature_sets_and_labels()

n_nodes_hl1 = 20
n_nodes_hl2 = 20
n_classes = 1
batch_size = 100

x = tf.placeholder('float', [None, 13])
y = tf.placeholder('float', [None, 1])
keys_placeholder = tf.placeholder(tf.int64, shape=(None,))

keys = tf.identity(keys_placeholder)

def neural_network_model(data):
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([13, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}
    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.tanh(l1)
    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.tanh(l2)
    output = tf.add(tf.matmul(l2, output_layer['weights']), output_layer['biases'])
    return output

output = neural_network_model(x)
prediction = tf.sigmoid(output)
predicted_class = tf.greater(prediction,0.5)


inputs = {'key': keys_placeholder.name, 'x': x.name}
tf.add_to_collection('inputs', json.dumps(inputs))

outputs = {'key': keys.name,
           'prediction': predicted_class.name}
tf.add_to_collection('outputs', json.dumps(outputs))


def train_neural_network(x):
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(output, y))
    optimizer = tf.train.AdamOptimizer(0.003).minimize(cost)
    hm_epochs = 700

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            while i < len(train_x):
                start = i
                end = i + batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                              y: batch_y})
                epoch_loss += c
                i+=batch_size
            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss/(len(train_x)/batch_size))

        correct = tf.equal(predicted_class, tf.equal(y,1.0))
        accuracy = tf.reduce_mean( tf.cast(correct, 'float') )
        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))

        export_path = "~/Documents/cloudcomputing/Project/RNN_timeseries/"
        print ("Exporting trained model to %s", %export_path)
        init_op = tf.group(tf.initialize_all_tables(), name="init_op")

        saver = tf.train.Saver(sharded = True)
        saver.save(sess, os.path.join(export_path, 'export'))

        print("Done exporting!")

train_neural_network(x)

我在你的代码中移动了一些东西(并没有实际测试过),但这应该给你一个起点。

【讨论】:

  • 运行你的代码后,我得到了checkpointexport.metaexport-00000-of-00001。最后一个是图形文件还是第一个?
  • export.meta 包含图形操作和常量的定义,其他变量的训练值。如果您没有设置sharder=True,它将没有数字,但这没什么区别。检查点有点像指针。无论如何,您可以将它们全部上传到 Storage 上的存储桶中,它会起作用;)
  • 哦。实际上我正在尝试,但我在部署部分遇到了错误。当我尝试创建此处提到的版本时:cloud.google.com/ml/docs/how-tos/deploying-models - 我遇到了一个错误:错误对不起,有问题。如果您输入了信息,请检查并重试。否则,问题可能会自行解决,请稍后再查看。
  • @rhaertel80 我让它在某个时候开始工作。我使用命令行选项并将模型文件上传到我的存储桶中的文件夹。现在正在尝试运行预测,但不确定它是如何工作的。
  • 我上传我的JSON线:{ “键”:0, “×”:[159.220001,159.929993,158.850006,159.800003,2256400,159.800003,153.94036155,162.320475227,145.560247873,159.448001,157.2819584,153.4395999, 218.990005]} 到文件并将其上传到存储桶。这是上传输入数据进行预测的正确方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2019-07-22
相关资源
最近更新 更多