tensorflow导入导致numpy计算错误 [英] tensorflow import causing numpy calculation errors

查看:128
本文介绍了tensorflow导入导致numpy计算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过线性回归示例来学习TensorFlow的基础知识.使用scikit-learn执行线性回归效果很好:

I'm learning the basics of TensorFlow thru an example of linear regression. Performing the linear regression with scikit-learn works well:

import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression

housing = fetch_california_housing()

lin_reg = LinearRegression()
lin_reg.fit(housing.data, housing.target.reshape(-1, 1))

print(np.r_[lin_reg.intercept_.reshape(-1, 1), lin_reg.coef_.T])

返回以下结果:

[[ -3.69419202e+01]
 [  4.36693293e-01]
 [  9.43577803e-03]
 [ -1.07322041e-01]
 [  6.45065694e-01]
 [ -3.97638942e-06]
 [ -3.78654265e-03]
 [ -4.21314378e-01]
 [ -4.34513755e-01]]

使用numpy(正则方程)执行相同操作也可以正常工作:

Performing the same using numpy (Normal equation) also works fine:

m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

X = housing_data_plus_bias
y = housing.target.reshape(-1, 1)
theta_numpy = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)

print(theta_numpy)

输出:

[[ -3.69419202e+01]
 [  4.36693293e-01]
 [  9.43577803e-03]
 [ -1.07322041e-01]
 [  6.45065694e-01]
 [ -3.97638942e-06]
 [ -3.78654265e-03]
 [ -4.21314378e-01]
 [ -4.34513755e-01]]

但是,当我在运行线性回归之前导入TensorFlow时,会得到可变且不准确的结果:

However, when I import TensorFlow prior running the linear regression, I get variable and inaccurate results:

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression

housing = fetch_california_housing()

lin_reg = LinearRegression()
lin_reg.fit(housing.data, housing.target.reshape(-1, 1))

print(np.r_[lin_reg.intercept_.reshape(-1, 1), lin_reg.coef_.T])

产生以下结果(每次都不同的值):

yielding the following results (different values every time):

[[  2.91247440e+32]
 [ -1.62971964e+11]
 [  1.42425463e+14]
 [ -4.82459003e+16]
 [ -1.33258747e+17]
 [ -2.04315813e+29]
 [  5.51179654e+14]
 [  5.92729561e+20]
 [  8.86284674e+21]]

如果我在导入tensorflow之前先运行任何一个计算,然后导入tensorflow并再次重复计算,我将获得正确的结果.

If I run either of the calculations before importing tensorflow, then import tensorflow and repeat the calculations again, I get the correct results.

任何想法是什么原因,以及如何确保在导入TensorFlow之后从numpy/scikit-learn中获得正确的结果?

Any idea what is the cause and how I can ensure I get correct results from numpy/scikit-learn after importing TensorFlow?

我正在使用tensorflow-gpu在Ubuntu上从Anaconda 4.3.30运行Python 3.5.4.

I'm running Python 3.5.4 from Anaconda 4.3.30 on Ubuntu with tensorflow-gpu.

numpy version: 1.12.1
tensorflow version: 1.3.0

推荐答案

Anaconda发行版默认使用英特尔的数学内核库(MKL),当与TensorFlow结合使用时,似乎会导致Numpy和SciPy出现多个问题此问题和其他参考问题.

The Anaconda distribution uses Intel's Math Kernel Libraries (MKL) by default which seems to result in multiple issues with Numpy and SciPy when used in conjunction with TensorFlow as reported in this issue and in other referenced issues.

从pip重新安装Numpy和SciPy可解决问题:

Re-installing Numpy and SciPy from pip resolves the issue:

首先,使用conda使用所需的软件包创建一个新环境:

First, create a new environment with the required packages using conda:

$ conda create --name env_name python=3.5 tensorflow-gpu scikit-learn

激活环境:

$ source activate env_name

使用pip重新安装Numpy和SciPy:

Re-install Numpy and SciPy using pip:

$ pip install --ignore-installed --upgrade numpy scipy

这样做的缺点是您无法从MKL提供的性能提升中受益.例如,使用Scikit-Learn构建的支持向量机需要6分钟的训练时间,而在没有MKL的环境中,需要11分钟才能训练出的MKL.但是,您可以在不需要TensorFlow的情况下创建另一个具有MKL的环境(默认情况下).

The drawback to this is that you cannot benefit from the performence increase provided by the MKL. As an example, a support vector machine built with Scikit-Learn that takes 6 minutes to train with MKL trained in 11 minutes in an environment without MKL. You can however create another environment that has MKL (by default) to use when TensorFlow is not required.

这篇关于tensorflow导入导致numpy计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆