无论如何,我为多变量预测模型输出的权重,成本和偏差始终为NaN。该模型没有退出代码,因此这是一个逻辑错误。
http://archive.ics.uci.edu/ml/datasets/Real+estate+valuation+data+set 继承人我使用的数据集
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
#params
learning_rate = 0.01
training_epochs = 1000
#not really that important. I use it so I can see what happens every 100 epochs
steps = 100
#generating random numbers from 0 to 20
变量的导入和初始化
#training sets, a bunch of random arrays with random values
cols = [2, 3, 4, 5, 6, 7]
Age = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[0])
Age = Age.iloc[:,2]
MRT = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[1])
MRT = MRT.iloc[:,3]
Conv = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[2])
Conv = Conv.iloc[:,4]
Conv = Conv.astype(float)
latitude = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[3])
latitude = latitude.iloc[:,5]
longitude = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[4])
longitude = longitude.iloc[:,6]
Price = pd.read_excel('Real estate valuation data set.xlsx', sheet_names='Sheet', usecols=cols[5])
Price = Price.iloc[:,7]
age = []
mrt = []
conv = []
lat = []
long = []
price = []
for i in range(50):
age.append(Age[i])
mrt.append(MRT[i])
conv.append(Conv[i])
lat.append(latitude[i])
long.append(longitude[i])
price.append(Price[i])
a = np.array(age)
m = np.array(mrt)
co = np.array(conv)
lo = np.array(lat)
la = np.array(long)
p = np.array(price)
上面的6个np数组是50个单位的一维数组,其中包含浮点数。 p数组将是我的因变量单元,其余的是独立的。
#I made the arrays have 50 samples each
n_samples = 50
A = tf.placeholder("float")
B = tf.placeholder("float")
C = tf.placeholder("float")
D = tf.placeholder("float")
E = tf.placeholder("float")
Y = tf.placeholder("float")
random = random.uniform(0,20)
#weights and biases
W1 = tf.Variable((random), name = "Weight1")
W2 = tf.Variable((random), name = "Weight2")
W3 = tf.Variable((random), name = "Weight3")
W4 = tf.Variable((random), name = "Weight4")
W5 = tf.Variable((random), name = "Weight5")
bias = tf.Variable((random), name = "Bias")
我的占位符变量。 A,B,C,D,E是我的自变量。 Y是我的依赖。剩下的就是我的体重和偏见
#linear model multiply x by weights and biases to get a y
pred = tf.add(tf.add(tf.add(tf.add(tf.multiply(A, W1), tf.multiply(B, W2)), tf.add(tf.multiply(C, W3), tf.multiply(D, W4))), tf.multiply(E, W5)), bias)
#cost function to reduce the error. MSE
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
#minimize cost taking steps of 0.01 down the parabola
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#initialize all variables
init = tf.global_variables_initializer()
我的预测,成本和优化程序功能。
#start running the neural network
with tf.Session() as sess:
# Run the initializer
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
#zip the training values together into tuples
for (un, deux, tres, quart, cinq, six) in zip(a, m, co, lo, la, p):
#feed the optimizer the training values
sess.run(optimizer, feed_dict={A: un, B: deux, C: tres, D: quart, E: cinq, Y: six})
我将所有训练变量都压缩到一个6个单元的元组中,并通过优化器运行它。 a,m,co,lo,la和p是我的数据集,其中包含浮点数,而A,B,C,D,E,Y是我的占位符,它们将在pred = tf.add(tf.add(tf.add(tf.add(tf.multiply(A, W1), tf.multiply(B, W2)), tf.add(tf.multiply(C, W3), tf.multiply(D, W4))), tf.multiply(E, W5)), bias)
if (epoch+1) % steps == 0:
c = sess.run(cost, feed_dict={A: a, B: m, C: co, D: lo, E: la, Y: p})
print("Epoch:" + str((epoch+1)), "cost=" + str(c), "W1=" + str(sess.run(W1)), "W2=" + str(sess.run(W2)), "W3=" + str(sess.run(W3)), "W4=" + str(sess.run(W4)), "W5=" + str(sess.run(W5)), "bias=" + str(sess.run(bias)))
此打印返回我所有时期的NaN