0
나는 tensorflow를 시작하고 있습니다. 온라인에서 찾은 예제를 기반으로 간단한 선형 회귀 분석을 작성하려고합니다.단순한 텐서 흐름 선형 회귀 오류
나는 sklearn을 사용할 때 합리적인 대답을 얻을 수있었습니다.
내 MSE가 NaN을 반환하는 이유는 무엇입니까?
import pandas as pd
import tensorflow as tf
import numpy as np
# Create some fake data
size = 1000
performance_x = np.stack((np.random.uniform(24, 40, size), np.random.uniform(80, 240, size), np.random.uniform(80, 100, size), np.random.uniform(15, 25, size)), axis=1)
performance_y = np.sum(np.multiply(performance_x, [0.25, 1, 0.5, 0.75]), axis=1)
performance_y = performance_y + np.stack(np.random.uniform(-10, 10, size))
performance_y = np.reshape(performance_y, (size,1))
n_dim = performance_x.shape[1]
# Testing Tensorflow
learning_rate = 0.001
training_epochs = 1000
cost_history = np.empty(shape=[1], dtype=float)
rnd_indices = np.random.rand(len(performance_x)) < 0.80
train_x = performance_x[rnd_indices]
train_y = performance_y[rnd_indices]
test_x = performance_x[~rnd_indices]
test_y = performance_y[~rnd_indices]
X = tf.placeholder(tf.float32, [None, n_dim])
Y = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.ones([n_dim, 1]))
init = tf.global_variables_initializer()
y_ = tf.matmul(X, W)
cost = tf.reduce_mean(tf.square(y_ - Y))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
sess.run(init)
for epoch in range(training_epochs):
sess.run(training_step, feed_dict={X:train_x,Y:train_y})
pred_y = sess.run(y_, feed_dict={X: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse))
값을 0.01로 스케일 할 때 합리적인 답을 얻은 것처럼 보입니다. 왜 텐서 플로우를 사용할 때 값을 스케일해야하지만, 다음의 스켈레톤 코드는 스케일링을 필요로하지 않습니다. 'REGR linear_model.LinearRegression =() 'regr.fit (performance_x, performance_y) ' '인쇄 regr.coef_ 'TensorFlow의 경우 – Kukai
@Kukai 상기 MSE 손실'sklearn 수입 linear_model에서 ' 갈라지다 (너무 커지게 됨). 한 가지 해결책은 입력을 확장하는 것입니다. 다른 하나는 작은 학습 속도를 사용하는 것입니다. 나는 100k 단계 후에 1e-6의 학습률로 33 주위에 MSE를 얻었다. Sklearn의 경우, linear_model이 어떻게 훈련되었는지 매개 변수화하지 않았으므로, W를 계산하기 위해 그래디언트 디센트 대신에 역행렬을 사용했다고 생각합니다. –
완벽! 학습 속도를 변경하면 효과가있었습니다. 당신의 도움을 주셔서 감사합니다. – Kukai