Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow Nan,我错在哪里?_Python_Tensorflow_Machine Learning_Model_Regression - Fatal编程技术网

Python Tensorflow Nan,我错在哪里?

Python Tensorflow Nan,我错在哪里?,python,tensorflow,machine-learning,model,regression,Python,Tensorflow,Machine Learning,Model,Regression,我是TF的新手,请原谅我。 我的任务是创建一个模型,根据90个特征预测一些连续的数字(稍后我将把它们减少到57)。我在互联网上看到了这个例子——“波士顿房价预测”,看起来和我需要的非常相似。然而,我知道我会有麻烦(因为模型不能如此容易地被采用),现在的麻烦是,我有一个Nan作为估计值。 我的代码如下所示: from __future__ import absolute_import from __future__ import division from __future__ import pr

我是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)

我的标签数据行如下所示:(train500Y.csv)

输出:

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]
[*]============================
提前谢谢你! 将考虑任何指导方针和建议


另外,如果你有更好的想法或例子可以向我学习,请推荐我

问题在于数据规范化

我发现问题的出现是因为我的数据没有规范化,而且数字的变化非常大。然而,如果有人能给我一些如何让我的模型更好的想法,请始终在这里聆听:)
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]
[*]============================