【发布时间】:2020-09-11 00:20:40
【问题描述】:
我想对数据集执行一些矩阵运算,并在 TF1 和 TF2 中尝试了相同的代码。 但得到不同的输出。
TF 2
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
m,n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]
X = tf.constant(housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
XT = tf.transpose(X)
theta = tf.matmul(tf.matmul(tf.linalg.inv(tf.matmul(XT, X)), XT), y)
tf.print(theta)
输出:
[[-36.8962631]
[0.436777472]
[0.0094444938]
...
[-0.00378797273]
[-0.420847952]
[-0.434020907]]
TF 1:
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]
X = tf.constant(housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
XT = tf.transpose(X)
theta = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(XT, X)), XT), y)
with tf.Session() as sess:
theta_value = theta.eval()
theta_value
输出:
array([[-3.68962631e+01],
[ 4.36777472e-01],
[ 9.44449380e-03],
[-1.07348785e-01],
[ 6.44962370e-01],
[-3.94082872e-06],
[-3.78797273e-03],
[-4.20847952e-01],
[-4.34020907e-01]], dtype=float32)
这是一个错误吗?还是我的代码有错误?
【问题讨论】:
标签: python tensorflow tensorflow2.0