我尝试逐样本提供数据。在不同的数据集上,结果要么完全错误,要么非常近似(绝对错误为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)
附言
fit
和partial_fit
根据来源相同 最佳答案
看起来像learning_rate和/或优化。请按以下方式尝试:model = tf.contrib.learn.LinearRegressor(..., optimizer=tf.train.YOUR_OPTIMIZER(YOUR_LEARNING_RATE)))
关于python - tensorflow 是否支持在线培训?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41821058/