我是tensorflow的初学者。我对文章的代码进行了一些小的更改,但权重是零。我在这里发布了正确的代码和修改后的代码。请帮助我找出问题所在。 正确的代码:
import tensorflow as tf
import numpy as np
import pandas as pd
df=pd.read_csv('D:/newcars.csv')
# mileage, cylinder,doors are placeholders for our training data
mileage = tf.placeholder("float")
cylinder = tf.placeholder("float")
doors = tf.placeholder("float")
price = tf.placeholder("float")
a=tf.Variable(1.0)
b=tf.Variable(1.0)
c=tf.Variable(1.0)
d=tf.Variable(1.0)
y_model = tf.multiply(mileage, a) + tf.multiply(cylinder, b) +
tf.multiply(doors, c) + d
error = tf.square(price - y_model)
train_op = tf.train.GradientDescentOptimizer(0.5).minimize(error)
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
for i in range(len(df)):
mileage_value = np.random.rand()
cylinder_value = np.random.rand()
doors_value = np.random.rand()
price_value = 2*mileage_value+ 3*cylinder_value+4*doors_value+1
print(str(mileage_value) + ' ' + str(cylinder_value) + ' ' +
str(doors_value) + ' ' + str(price_value))
session.run(train_op, feed_dict={mileage: mileage_value, cylinder:
cylinder_value,doors:doors_value,price:price_value})
a_value = session.run(a)
b_value = session.run(b)
c_value = session.run(c)
d_value = session.run(d)
print(str(a_value)+' ' + str(b_value)+ ' ' + str(c_value) + ' ' +
str(d_value))
修改后的代码: 我只更换了
mileage_value = np.random.rand()
cylinder_value = np.random.rand()
doors_value = np.random.rand()
price_value = 2*mileage_value+ 3*cylinder_value+4*doors_value+1
具有:
mileage_value = df.loc[i]['Mileage']
cylinder_value = df.loc[i]['Cylinder']
doors_value = df.loc[i]['Doors']
price_value = df.loc[i]['Price']
尽管Mileage_value,汽缸值,doors_value和price_value都正确,但发生了和nan错误。
答案 0 :(得分:0)
最后,我找出了问题的根本原因。原因是我没有规范化输入数据。因此,输入数据的波动幅度很大。结果,在几次迭代之后,权重被更新,但是直到某个时刻,这些权重如此之小或如此之大,最终它们变为inf或nan。 在对输入数据进行归一化之后,该模型可以得出预期的结果。