首页 > 解决方案 > 在 TF1 和 TF2 中为相同的操作获得不同的输出

问题描述

我想对数据集执行一些矩阵运算,并在 TF1 和 TF2 中尝试了相同的代码。但是得到了不同的输出。

特遣部队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]]

特遣部队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)

它是一个错误吗?还是我的代码有错误?

标签: pythontensorflowtensorflow2.0

解决方案


这既不是错误也不是错误 - 这些是完全相同的结果,只是在第二种情况下,它们是用所谓的E-notation编写的:以 结尾的浮点数e+01表示乘以10^1,因此-3.68962631e+01等于-36.8962631; 同样,e-03表示乘以10^(-3)(即除以10^3=1000),因此-3.78797273e-03等于-0.00378797273等。

用您显示的结果正式检查它并不难:

import numpy as np

# your TF2 results:
a = np.array([[-36.8962631],
              [0.436777472],
              [0.0094444938],
              [-0.00378797273],
              [-0.420847952],
              [-0.434020907]])

# your TF1 results:    
b = np.array([[-3.68962631e+01],
              [ 4.36777472e-01],
              [ 9.44449380e-03],
              [-3.78797273e-03],
              [-4.20847952e-01],
              [-4.34020907e-01]])

np.all(a==b)
# True

推荐阅读