【问题标题】:Getting different output for same operation in TF1 and TF2在 TF1 和 TF2 中为相同的操作获得不同的输出
【发布时间】: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


    【解决方案1】:

    这既不是错误也不是错误 - 这些是完全相同的结果,只是在第二种情况下它们被写成所谓的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2022-01-11
      • 2016-06-19
      • 2017-09-25
      相关资源
      最近更新 更多