简体   繁体   English

使用 tensorflow 进行增量模型训练

[英]Incremental model training with tensorflow

I have a simple linear model which inputs (x, y) pairs and deduces b0 and b1 in y = b0 + b1 * x ;我有一个简单的线性模型,它输入 (x, y) 对并在 y = b0 + b1 * x 中推导出 b0 和 b1 ; the key code is below.关键代码如下。 It trains on a dataset of known size.它在已知大小的数据集上进行训练。 Now I want to add the ability of training it constantly: ie add every other batch of (x, y), and update cofficients according to the new data.现在我想增加不断训练它的能力:即每隔一批(x,y)相加,并根据新数据更新系数。 There will be unlimited amount of input.将有无限量的输入。

    x = tf.placeholder(tf.float32, [data_len], name="x")
    y = ...
    b0 = tf.Variable([0.8], trainable=True)
    b1 = ...
    #the model
    y = tf.add(tf.mul(x, b1), b0)
    y_act = tf.placeholder(tf.float32, [data_len], name="y_act")
    error = tf.sqrt((y - y_act) * (y - y_act))
    train_step = tf.train.AdamOptimizer(0.01).minimize(error)
    x_in = ...
    y_in = ...
    init = tf.initialize_all_variables()
    sess.run(init)
    feed_dict = { ... }
    fetches_in = { b0: b0, b1: b1, y: y, train_step: train_step }
    for i in range(0, 50):
        fetches = sess.run(fetches_in, feed_dict)

My idea is to remember so-far-trained coefficients, init a model with them, then just repeat again the training with the new portion of data.我的想法是记住迄今为止训练过的系数,用它们初始化一个模型,然后用新的数据部分再次重复训练。 Repeat on each input.在每个输入上重复。 Is this a right way to go?这是正确的方法吗? The model will probably be promoted later to something more complex..该模型可能会在以后升级为更复杂的模型。

It sounds like you're talking about on-line training, ie continuously train a model with incoming data while simultaneously using it.听起来您在谈论在线训练,即在使用传入数据的同时不断训练模型。 You're right in that you should be able to pick up where you left off and just feed in new data.您是对的,您应该能够从上次中断的地方继续前进并输入新数据。 What you'll need is a way to save and load the variables between training sessions.您需要的是一种在培训课程之间保存和加载变量的方法。 You can use a tf.Saver to do this in "raw" tensorflow.您可以使用 tf.Saver 在“原始”张量流中执行此操作。

You can also use a tf.contrib.learn.Estimator to do this for you.您也可以使用 tf.contrib.learn.Estimator 为您执行此操作。 You just give it a model_fn that constructs your model, and a model_dir to save the model in, and it will take care of the rest.您只需给它一个用于构建模型的 model_fn 和一个用于保存模型的 model_dir,它就会处理其余的工作。 Of course, there's already a linear model in tf.contrib.learn.LinearEstimator.当然,tf.contrib.learn.LinearEstimator 中已经有一个线性模型。 With estimators, you'd just call fit(...) whenever you have new data and it will load your variables and continue running the training steps you've defined.使用估算器,只要有新数据,您只需调用 fit(...) ,它就会加载您的变量并继续运行您定义的训练步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM