我正在使用 tensorflow LinearRegressor API来解决回归问题(https://www.tensorflow.org/api_docs/python/tf/estimator/LinearRegressor)。我知道模型中的偏差恰好是0。
如何强制 LinearRegressor学习零偏?
这是一个最小的示例:
import tensorflow as tf
import numpy as np
from sklearn.linear_model import SGDRegressor
模拟具有2个特征的某些数据(+偏差为0) y = 0 + 2 * x1 + 3 * x2 +噪声
np.random.seed(5332)
n = 1000
weights = np.array([
[2],
[3],
])
bias = 0
x = np.random.randn(n, np.shape(weights)[0])
y = (bias + np.matmul(x, weights) + np.random.randn(n, 1)).ravel()
在sklearn中,我将使用fit_intercept = False将偏倚强制为0:
ols = SGDRegressor(tol=0.000001, fit_intercept=False)
ols.fit(x, y)
print("True weights: {}".format(weights.ravel()))
print("Learned weights: {}".format(np.round(ols.coef_), 3))
print("True bias: {}".format([bias]))
print("Learned bias: {}".format(np.round(ols.intercept_), 3))
输出:
True weights: [2 3]
Learned weights: [2. 3.]
True bias: [0]
Learned bias: [0.]
在tensorflow中,我做了以下事情:
column = tf.feature_column.numeric_column('x', shape=np.shape(x)[1])
ols = tf.estimator.LinearRegressor(
feature_columns=[column],
optimizer=tf.train.GradientDescentOptimizer(0.0001)
)
train_input = tf.estimator.inputs.numpy_input_fn(
x={"x": x},
y=y,
shuffle=False,
num_epochs=100,
batch_size=int(len(y) / 20)
)
ols.train(train_input)
print("True weights: {}".format(weights.ravel()))
print("Learned weights: {}".format(np.round(ols.get_variable_value('linear/linear_model/x/weights').flatten(), 3)))
print("True bias: {}".format([bias]))
print("Learned bias: {}".format(np.round(ols.get_variable_value('linear/linear_model/bias_weights').flatten(), 3)))
输出:
True weights: [2 3]
Learned weights: [1.993 2.998]
True bias: [0]
Learned bias: [-0.067]
但是学习到的偏见应该是:[0],我该如何执行呢?