我是TF新手所以请原谅我。 我的任务是创建一个基于90个特征预测一些连续数字的模型(稍后我会将它们减少到57个)。我在互联网上看到了这个例子 - '波士顿房价预测'看起来和我需要的非常相似。但是我知道我会遇到麻烦(因为一个模型不能那么容易被采用)而现在的麻烦是,我有一个Nan作为估计值。 我的代码如下:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
X_train = np.genfromtxt('data/train500X.csv', delimiter=',', dtype=float)
Y_train = np.genfromtxt('data/train500Y.csv', delimiter=',', dtype=float)
X_test = np.genfromtxt('data/test100X.csv', delimiter=',', dtype=float)
Y_test = np.genfromtxt('data/test100Y.csv', delimiter=',', dtype=float)
total_len = X_train.shape[0]
# Parameters
learning_rate = 0.001
training_epochs = 500
batch_size = 10
display_step = 1
dropout_rate = 0.9
# Network Parameters
n_hidden_1 = 90 # 1st layer number of features
n_hidden_2 = 200 # 2nd layer number of features
n_hidden_3 = 200
n_hidden_4 = 256
n_input = X_train.shape[1]
n_classes = 1
# tf Graph input
x = tf.placeholder("float32", [None, 90])
y = tf.placeholder("float32", [None])
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Hidden layer with RELU activation
layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
layer_3 = tf.nn.relu(layer_3)
# Hidden layer with RELU activation
layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4'])
layer_4 = tf.nn.relu(layer_4)
# Output layer with linear activation
out_layer = tf.matmul(layer_4, weights['out']) + biases['out']
return out_layer
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 0.1)),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 0.1)),
'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], 0, 0.1)),
'h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4], 0, 0.1)),
'out': tf.Variable(tf.random_normal([n_hidden_4, n_classes], 0, 0.1))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1], 0, 0.1)),
'b2': tf.Variable(tf.random_normal([n_hidden_2], 0, 0.1)),
'b3': tf.Variable(tf.random_normal([n_hidden_3], 0, 0.1)),
'b4': tf.Variable(tf.random_normal([n_hidden_4], 0, 0.1)),
'out': tf.Variable(tf.random_normal([n_classes], 0, 0.1))
}
# Construct model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.square(tf.transpose(pred) - y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Launch the graph
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(total_len / batch_size)
# Loop over all batches
for i in range(total_batch - 1):
batch_x = X_train[i * batch_size:(i + 1) * batch_size]
batch_y = Y_train[i * batch_size:(i + 1) * batch_size]
# Run optimization op (backprop) and cost op (to get loss value)
_, c, p = sess.run([optimizer, cost, pred], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
c += c / total_batch
# print(c) #c = nan???? total_batch = 50
# print("what is here")
# print(tf.is_finite(c, name=None))
# sample prediction
label_value = batch_y
estimate = p
err = label_value - estimate
print("num batch:", total_batch)
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch + 1), "cost=", \
"{:.9f}".format(avg_cost))
print("[*]----------------------------")
for i in range(3):
print("label value:", label_value[i], \
"estimated value:", estimate[i])
print("[*]============================")
exit()
print("Optimization Finished!")
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(S, "float32"))
print("Accuracy:", accuracy.eval({x: X_test, y: Y_test}))
我的列车数据如下所示:(train500X.csv)
0,1,1,1,1,0,20,36,4194304,8,7,1,4,3420,79691776,528594,3191,525403,349114,176,7,0.47922,0.700034,84.54,0,1,14.68,0,0,0,0,0,11215940,5091688,31.22,0,0,0,72,0,0,0,4,1000000000,4,17179869184,2133000000,4194300,0,0,57.14,0,3.39,37.52,0,0,0,0,0,61645484,4206508,6.39,33.49,213.6,40881.085,7,0,0,0,4,2500000000,8,68719476736,2133000000,8388604,0,0,0,752.51953125,2463.5,5523,46881,54734,1146164,194866,0.001020011479174,10.90673828125,0,1529.19102,367799.963702
我的标签数据行如下所示:(train500Y.csv)
24407
输出:
num batch: 50
Epoch: 0017 cost= 0.000000000
[*]----------------------------
label value: 7228.0 estimated value: [ nan]
label value: 43743.0 estimated value: [ nan]
label value: 15087.0 estimated value: [ nan]
[*]============================
提前谢谢! 任何指导方针和建议都将被考虑。
P.S。如果您有更好的想法或示例我可以向您学习,请推荐我。
答案 0 :(得分:1)
问题在于数据规范化