python - 在 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)
它是一个错误吗?还是我的代码有错误?
解决方案
这既不是错误也不是错误 - 这些是完全相同的结果,只是在第二种情况下,它们是用所谓的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
推荐阅读
- visual-studio-code - VisualStudioCode 在调试时打印到终端时引发错误
- blockchain - 坚固性 v0.6.0。后备功能。他们需要什么?
- angular - httpClient 上的 Angular 循环
- yocto - gprolog 配方的 Autoreconf 失败
- ios - iOS推送通知,可以决定不触发通知吗?
- google-chrome - Chrome ffmpeg.js 无法制作超过 33 秒的视频
- python - 由用户介绍的坐标创建的关闭区域
- reactjs - 如何在 React 中更改数组状态的特定项目?
- node.js - 可以通过 VSCode 启动 Angular
- html - 如何使 div 中的链接在另一个 div 下可点击,但不更改 div 宽度?