tensorflow是否支持在线培训?

时间:2017-01-24 06:03:22

标签: python tensorflow linear-regression

我尝试逐个样本地提供数据。结果在不同数据集上完全错误或非常近似(25-50%绝对误差)。如果一次性训练,结果对所有数据集都很好。

import itertools as itools
import numpy as np
import tensorflow as tf
from sklearn import preprocessing

class Test:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self._i = 0 

    def do_test(self):
        x_col = tf.contrib.layers.real_valued_column("x", dimension=1) 
        model = tf.contrib.learn.LinearRegressor(feature_columns=[x_col])
        print("Fitting")
        max_steps = 80
        for _ in range(0, len(self.x)):
            model.fit(input_fn=self.input_split, steps=max_steps)
        print("Predicting")
        scaled_out = model.predict(input_fn=self.eval_fn)
        print(self._inverse_y(list(itools.islice(scaled_out, self.eval_len))))

    def input_split(self):
        if 0 == self._i:
            self.x_std, self.y_std = self._transform(self.x, self.y)
        if len(self.x_std) == self._i:
            raise StopIteration
        x = self.x_std[self._i]
        y = self.y_std[self._i]
        self._i += 1
        feature_cols = {"x": tf.constant([x], dtype=tf.float32), 
                        }
        print(x, y)
        label = tf.constant([y], dtype=tf.float32)
        return feature_cols, label

    def eval_fn(self):
        x = [0, 1, 5, 10]
        y = np.zeros(len(x))

        self.eval_len = len(x)

        x_std, y_std = self._transform(x, y)
        feature_cols = {"x": tf.constant(x_std, dtype=tf.float32), 
                 }
        label = tf.constant(y_std, dtype=tf.float32)
        return feature_cols, label


    def _transform(self, x_in, y_in):
        if not hasattr(self, "x_scaler"):
            self.x_scaler = preprocessing.StandardScaler().fit(x_in)
            self.y_scaler = preprocessing.StandardScaler().fit(y_in)
        x_std = self.x_scaler.transform(x_in)
        y_std = self.y_scaler.transform(y_in)
        return x_std, y_std

    def _inverse_y(self, y_std):
       return self.y_scaler.inverse_transform(y_std) 

P.S。 fitpartial_fit根据来源

是相同的

1 个答案:

答案 0 :(得分:1)

这看起来像learning_rate和/或优化。请按以下方式尝试:

model = tf.contrib.learn.LinearRegressor(..., optimizer=tf.train.YOUR_OPTIMIZER(YOUR_LEARNING_RATE)))