gpt4 book ai didi

python - Tensorflow Nan,我哪里错了?

转载 作者:行者123 更新时间:2023-11-30 09:49:05 25 4
gpt4 key购买 nike

我是 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]
[*]============================

提前谢谢您!任何指导方针和建议都会被考虑。

附注如果您有更好的想法或例子可以向我学习,请推荐给我。

最佳答案

问题在于数据标准化

关于python - Tensorflow Nan,我哪里错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com