成本函数不断增加

时间:2018-12-03 15:09:51

标签: python tensorflow linear-regression data-science

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv("FuelConsumption.csv")
df = df[['ENGINE SIZE','CYLINDERS', 'Mcity', 'Mhwy', 'Mcmb', 'McmbMPG', 'CO2']]

features = np.asanyarray(df[['ENGINE SIZE','CYLINDERS', 'Mcity', 'Mhwy', 'Mcmb', 'McmbMPG']])
label    = np.asanyarray(df[['CO2']])

mu = np.mean(features,axis=0)
sigma = np.std(features,axis=0)
feature_normalized = (features - mu)/sigma

n_training_samples = feature_normalized.shape[0]
n_dim = feature_normalized.shape[1]

feature_reshaped = np.reshape(features,[n_training_samples,n_dim])
label_reshaped = np.reshape(label,[n_training_samples,1])

train_X, test_X, train_Y, test_Y = train_test_split\
                 (feature_reshaped, label_reshaped,shuffle = True , test_size=0.25, random_state=42)
print("shape of training input = ", train_X.shape)
print("shape of training output = ", train_Y.shape)


numFeatures = train_X.shape[1]
print("Number of features = ", numFeatures)
numLabels = train_Y.shape[1]
print("Number of labels = ", numLabels)

learning_rate = 0.01
training_epochs = 1000

X = tf.placeholder(tf.float32,[None,numFeatures])
Y = tf.placeholder(tf.float32,[None,numLabels])
W = tf.Variable(tf.ones([numFeatures,numLabels]))
B = tf.Variable(tf.ones([1,numLabels]))

init = tf.global_variables_initializer()

Y_model = tf.add(tf.matmul(X, W, name="apply_weights"), B, name="add_bias")
cost = tf.reduce_mean(tf.square(Y_model - Y))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

sess = tf.Session()
sess.run(init)

loss_values = []
train_data = []
for epoch in range(training_epochs):
_, loss_val, a_val, b_val = sess.run([training_step, cost, W, B],feed_dict={X: train_X,Y: train_Y})
loss_values.append(loss_val)
if epoch % 20 == 0:
    print(epoch, loss_val, a_val, b_val)
    train_data.append([a_val, b_val])


plt.plot(loss_values, '--')
plt.show()

我正尝试使用线性回归等多个变量(例如缸数,里程数,发动机尺寸等)来预测CO2排放量。我在tensorflow中使用了上面的代码。当我尝试运行模型损失值时,权重和偏差仅针对20次迭代进行更新,此后它们为infinity(Nan)。这里的问题是代码或成本函数/优化器的选择?

我绘制了损耗值,看起来像this

如果仅使用引擎尺寸和气缸作为特征,则效果良好。任何其他功能(城市中的里程,高速公路中的里程,里程的组合)都会导致上述问题。我正在尝试Mhwy的Mcity的分散地块。数据本身有问题吗?请查看城市和高速公路上零星的里程图。enter image description here enter image description here

0 个答案:

没有答案