python - Tensorflow支持在线训练吗?

标签 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) 

附注根据来源,fitpartial_fit 是相同的

最佳答案

这看起来像学习率和/或优化。请按如下方式尝试使用它们:

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

关于python - Tensorflow支持在线训练吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821058/

相关文章:

python - 使用 Seaborn 删除一些 x 标签

python - 使用tensorflow和numpy时出现奇怪的数据类型错误

tensorflow - 缩减轴 1 的形状为空 [x,0]

r - 在 R 中从 CSV 文件执行回归

r - R中的线性回归(正态和对数数据)

python - 使用 Numpy 进行线性回归

python - 打印格式化的值集列表

python - __init__ 中的类参数测试

machine-learning - tensorflow cifar-10 评估示例 softmax 输出

python - 在测试之间自动删除 MEDIA_ROOT