Вычисление весов в задаче линейной регрессии - PullRequest
2 голосов
/ 05 мая 2019

Я написал скрипт, который демонстрирует алгоритм линейной регрессии следующим образом:

training_epochs = 100
learning_rate = 0.01
# the training set
x_train = np.linspace(0, 10, 100)
y_train = x_train + np.random.normal(0,1,100)
# set up placeholders for input and output
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# set up variables for weights
w0 = tf.Variable(0.0, name="w0")
w1 = tf.Variable(0.0, name="w1")
y_predicted =  X*w1 + w0
# Define the cost function
costF = 0.5*tf.square(Y-y_predicted)
# Define the operation that will be called on each iteration
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(costF)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Loop through the data training
for epoch in range(training_epochs):
       for (x, y) in zip(x_train, y_train):
              sess.run(train_op, feed_dict={X: x, Y: y})
# get values of the final weights
w_val_0,w_val_1 = sess.run([w0,w1])
sess.close()

С помощью этого скрипта выше я мог бы легко вычислить w_val_1 и w_val_0.Но если я что-то изменил с помощью y_predicted:

w0 = tf.Variable(0.0, name="w0")
w1 = tf.Variable(0.0, name="w1")
w2 = tf.Variable(0.0, name="w2")
y_predicted =  X*X*w2 + X*w1 + w0
...
w_val_0,w_val_1,w_val_2 = sess.run([w0,w1,w2])

, то я не смог бы вычислить w_val_0, w_val_1, w_val_2.Пожалуйста, помогите мне!

1 Ответ

3 голосов
/ 05 мая 2019

Когда вы выполняете X*X, вес (w2, w1 и w0) быстро увеличивается, достигая inf, что приводит к потерям nan и никакой тренировки не происходит. Как правило, данные всегда нормализуют данные до 0 и отклонение от единицы.

Фиксированный код

training_epochs = 100
learning_rate = 0.01
# the training set
x_train = np.linspace(0, 10, 100)
y_train = x_train + np.random.normal(0,1,100)

# # Normalize the data
x_mean = np.mean(x_train)
x_std = np.std(x_train)
x_train_ = (x_train - x_mean)/x_std

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# set up variables for weights
w0 = tf.Variable(0.0, name="w0")
w1 = tf.Variable(0.0, name="w1")
w2 = tf.Variable(0.0, name="w3")

y_predicted =  X*X*w1 + X*w2 + w0
# Define the cost function
costF = 0.5*tf.square(Y-y_predicted)
# Define the operation that will be called on each iteration
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(costF)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Loop through the data training
for epoch in range(training_epochs):
       for (x, y) in zip(x_train_, y_train):
            sess.run(train_op, feed_dict={X: x, Y: y})                                


y_hat = sess.run(y_predicted, feed_dict={X: x_train_})
print (sess.run([w0,w1,w2]))
sess.close()

plt.plot(x_train, y_train)
plt.plot(x_train, y_hat)
plt.show()

выход:

[4.9228806, -0.08735728, 3.029659]

enter image description here

...