可以使用 TensorFlow C API 训练 Keras 模型吗? [英] Can Keras models be trained using the TensorFlow C API?

查看:35
本文介绍了可以使用 TensorFlow C API 训练 Keras 模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Keras 中定义了一个模型,我想使用 TensorFlow C API 为强化学习应用程序训练该模型.(实际上是通过 Rust 编程语言,但它直接使用 C API.)我一直在努力寻找某种方法来做到这一点,但到目前为止还没有运气.我曾期望使用 SavedModel 序列化操作或函数,这将允许训练,但我没有看到它们.甚至可以从 C 中训练 Keras 模型吗?还是一般的 TF 模型?

I have a model defined in Keras that I would like to train using the TensorFlow C API for a reinforcement learning application. (Actually by way of the Rust programming language, but it uses the C API directly.) I have struggled to find some way to do this, but as of yet no luck. I had expected ops or functions to be serialized with a SavedModel which would allow training, but I don't see them. Is it even possible to train Keras models from C? Or TF models in general?

推荐答案

您可以创建一个自定义Keras 模型,其中一个 tf.function 用于训练,另一个用于预测.

You can create a Custom Keras Model with one tf.function for training and another for predictions.

class MyModel(tf.keras.Model):
def __init__(self, name=None, **kwargs):
    super().__init__(**kwargs)
    
    #### define your layers here ####
    self.vec_layer = vectorizer
    self.preds = model

#### override call function (used for prediction) ####
@tf.function
def call(self, inputs, training=False): 
    #### define model structure ####
    x = self.vec_layer(inputs)
    return self.preds(x)
    #return {'output': self.preds(x)} #return labeled result

#### this function only calls the train_step. ####
#It can return whatever the user wants. Examples returning wither loss or nothing
@tf.function
def training(self, data):
    loss = self.train_step(data)['loss']
    return {'loss': loss}
    return {}

model = MyModel(name="end_model")

然后,您必须编译和构建模型.

Then, you have to compile and build your model.

model.compile(loss="sparse_categorical_crossentropy", optimizer="SGD", metrics=["acc"])
model.fit([["Ok."]], [1], batch_size=1, epochs=1)

在保存模型之前,您需要将您的 tf.functions 定义为具体函数并将它们作为签名函数传递.

Before saving the model, you need to define your tf.functions as concrete functions and pass them as signature functions.

call_output = model.call.get_concrete_function(tf.TensorSpec([None,1], tf.string, name='input'))
train_output = model.training.get_concrete_function((tf.TensorSpec([None,1], tf.string, name='inputs'),tf.TensorSpec([None,1], tf.float32, name='target')))
model.save("model_saved.tf", save_format="tf", signatures={'train': train_output, 'predict': call_output})

现在,您可以使用 C API 在模型中训练/预测

Now, you can train/predict in your model using the C API

TF_Graph* graph = TF_NewGraph();
TF_Status* status = TF_NewStatus();
TF_SessionOptions* SessionOpts = TF_NewSessionOptions();
TF_Buffer* RunOpts = NULL;

const char* saved_model_dir = "MODEL_DIRECTORY";
const char* tags = "serve"; //saved_model_cli

int ntags = 1;
TF_Session* session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, graph, NULL, status);

if(TF_GetCode(status) == TF_OK)
{
    printf("TF_LoadSessionFromSavedModel OK\n");
}
else
{
    printf("%s",TF_Message(status));
}

然后,在此处创建您的输入和输出张量.现在,只需调用 TF_SessionRun.

Then, create your input and output Tensors as here. Now, just call TF_SessionRun.

#### predicting ####
TF_SessionRun(session, nullptr,
              input_operator, input_values, 1,
              output_operator, output_values, 1,
              nullptr, 0, nullptr, status);

#### training ####
TF_SessionRun(session, nullptr, input_target_operators, input_target_values, 2, loss_operator, loss_values, 1, nullptr, 0, nullptr, status);

重要提示:

  • 为了获取变量名称(输入、目标、输出等),您可以使用saved_model_cli";命令,在此处进行了说明.
  • 理论上,您必须通过TF_SessionRun"传递要执行的函数的 signature_def 名称.方法.但是,我无法完成这项工作.然而,不知何故,为TF_SessionRun"传递了正确的输入和输出值.使其自动选择要使用的正确方法.
  • 上面的代码是我项目的一部分,因此它不会自行编译.但是,我希望它对您有所帮助,因为我没有足够的时间来制作一个完整的功能示例.
  • 除了已经提到的链接之外,this 解释了如何使用 TF v1 训练模型,以及这个如何在 Python 中构建和保存模型的完整示例.
  • In order to get the variable names (input, target, output, etc) you can use the "saved_model_cli" command, explained here.
  • In theory, you would have to pass the signature_def name of the function that you want to execute with the "TF_SessionRun" method. However, I could not make this work. However, somehow, passing the right inputs and output values for the "TF_SessionRun" makes it automatically select the right method to use.
  • The code above is part of my project so it won't compile on its own. However, I hope it will assist you, as I am not with enough time to make a whole functional example.
  • Other than the links already mentioned, this explains how to train the model using TF v1, and this is a more complete example of how you can construct and save your model in python.

这篇关于可以使用 TensorFlow C API 训练 Keras 模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆