线性回归问题中的权重计算

我编写了如下展示线性回归算法的脚本:

training_epochs = 100learning_rate = 0.01# the training setx_train = np.linspace(0, 10, 100)y_train = x_train + np.random.normal(0,1,100)# set up placeholders for input and outputX = tf.placeholder(tf.float32)Y = tf.placeholder(tf.float32)# set up variables for weightsw0 = tf.Variable(0.0, name="w0")w1 = tf.Variable(0.0, name="w1")y_predicted =  X*w1 + w0# Define the cost functioncostF = 0.5*tf.square(Y-y_predicted)# Define the operation that will be called on each iterationtrain_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(costF)sess = tf.Session()init = tf.global_variables_initializer()sess.run(init)# Loop through the data trainingfor 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 weightsw_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。请帮帮我!


回答:

当你进行 X*X 操作时,权重(w2, w1w0)会迅速增加到 inf,导致损失函数中出现 nan 值,训练无法进行。作为经验法则,总是要将数据标准化为零均值和单位方差。

修正后的代码

training_epochs = 100learning_rate = 0.01# the training setx_train = np.linspace(0, 10, 100)y_train = x_train + np.random.normal(0,1,100)                                     # # Normalize the datax_mean = np.mean(x_train)x_std = np.std(x_train)x_train_ = (x_train - x_mean)/x_stdX = tf.placeholder(tf.float32)Y = tf.placeholder(tf.float32)# set up variables for weightsw0 = 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 functioncostF = 0.5*tf.square(Y-y_predicted)# Define the operation that will be called on each iterationtrain_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(costF)sess = tf.Session()init = tf.global_variables_initializer()sess.run(init)# Loop through the data trainingfor 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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注