선형 회귀의 예를 통해 TensorFlow의 기본 사항을 배우고 있습니다. 로 선형 회귀 분석을 수행 아니라 작품을 scikit 배우기 :tensorflow import가 numpy 계산 오류를 일으킴
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])
을 다음과 같은 결과 반환 : 같은 사용 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]]
을 (일반 식)도 잘 작동 :
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]]
나도 실행하면
[[ 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]]
: 다음과 같은 결과 (다른 값마다) 산출
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])
: 나는 TensorFlow을 가져올 때210
그러나, 선형 회귀를 실행하기 전에, 나는 변수 부정확 한 결과를 얻을 수 tensorflow를 가져 오기 전에 계산을 수행 한 다음 tensorflow를 가져 와서 계산을 다시 반복하면 정확한 결과를 얻을 수 있습니다.
어떤 원인인지 그리고 TensorFlow를 가져온 후 numpy/scikit-learn에서 올바른 결과를 얻을 수있는 방법은 무엇입니까?
나는 tensorflow-gpu를 사용하여 Unbuntu에서 Anaconda 4.3.30의 Python 3.5.4를 실행 중입니다.
numpy version: 1.12.1
tensorflow version: 1.3.0
[해당 이슈] (https://github.com/tensorflow/tensorflow/issues/14034)로 연결되는 [관련이있을 수 있음] (https://github.com/tensorflow/tensorflow/issues/11724) . – sascha
리드 @ 주셔서 감사합니다. 이 문제는 실제로 TensorFlow와 Anaconda 배포판과 함께 제공되는 MKL 라이브러리 간의 잘못된 상호 작용과 관련이있는 것으로 보입니다. [here] (https://github.com/tensorflow/tensorflow/issues/13004)에서 알 수 있듯이,'export MKL_NUM_THREAD = "1"을 사용하면 올바른 결과를 얻을 수있었습니다. – drgfreeman