【问题标题】:Constructing a Neural Network for Multiple Outputs为多个输出构建神经网络
【发布时间】:2018-01-25 01:46:42
【问题描述】:

我的输入数据如下:

AT  V   AP  RH  PE
14.96   41.76   1024.07 73.17   463.26
25.18   62.96   1020.04 59.08   444.37
5.11    39.4    1012.16 92.14   488.56
20.86   57.32   1010.24 76.64   446.48
10.82   37.5    1009.23 96.62   473.9
26.27   59.44   1012.23 58.77   443.67
15.89   43.96   1014.02 75.24   467.35
9.48    44.71   1019.12 66.43   478.42
14.64   45  1021.78 41.25   475.98
....................................

我基本上是在使用 Tensorflow 库开发 Python。 截至目前,我有一个线性模型,它适用于 4 个输入和 1 个输出。这基本上是一个回归问题。 例如:用足够的数据训练我的神经网络后(比如数据的大小是 10000 左右),然后在训练我的神经网络时,如果我将值 45、30、25、32 作为输入传递,它会返回值 46 作为输出。

我基本上有两个查询:

  1. 截至目前,在我的代码中,我使用的是参数 training_epochslearning_rate 等。我现在给 training_epochs 的值为 10000。所以,当我测试我的神经 网络通过传递四个输入值,我得到的输出为 一些 471.25,而我希望它是 460。但是如果我给 training_epochs 的值为 20000,而不是 10000,我得到 我的输出值为 120.5,与 实际值“460”。

您能否解释一下,如何在我的代码中选择training_epochslearning_rate(或任何其他参数值)的值,以便获得良好的准确性。

  1. 现在,第二个问题是,我的神经网络目前正在工作 仅适用于线性数据以及仅适用于 1 个输出。 如果我想拥有 3 个输入和 2 个输出以及一个非线性模型,什么是 我可以对我的代码进行哪些更改?

我在下面发布我的代码:

import tensorflow as tf
import numpy as np
import pandas as pd
#import matplotlib.pyplot as plt
rng = np.random


# In[180]:

# Parameters
learning_rate = 0.01
training_epochs = 10000
display_step = 1000


# In[171]:

# Read data from CSV

df = pd.read_csv("H:\MiniThessis\Sample.csv")


# In[173]:

# Seperating out dependent & independent variable

train_x = df[['AT','V','AP','RH']]
train_y = df[['PE']]
trainx = train_x.as_matrix().astype(np.float32)
trainy = train_y.as_matrix().astype(np.float32)
# In[174]:

n_input = 4
n_classes = 1
n_hidden_1 = 5
n_samples = 9569

# tf Graph Input
#Inserts a placeholder for a tensor that will be always fed.
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])

# Set model weights
W_h1 = tf.Variable(tf.random_normal([n_input, n_hidden_1]))
W_out = tf.Variable(tf.random_normal([n_hidden_1, n_classes]))
b_h1 = tf.Variable(tf.random_normal([n_hidden_1]))
b_out = tf.Variable(tf.random_normal([n_classes]))


# In[175]:

# Construct a linear model
layer_1 = tf.matmul(x, W_h1) + b_h1
layer_1 = tf.nn.relu(layer_1)
out_layer = tf.matmul(layer_1, W_out) + b_out


# In[176]:

# Mean squared error
cost = tf.reduce_sum(tf.pow(out_layer-y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)


# In[177]:

# Initializing the variables
init = tf.global_variables_initializer()


# In[181]:

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        _, c = sess.run([optimizer, cost], feed_dict={x: trainx,y: trainy})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c))

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={x: trainx,y: trainy})
    print(training_cost)

    correct_prediction = tf.equal(tf.argmax(out_layer, 1), tf.argmax(y, 1))
    best = sess.run([out_layer], feed_dict=
    {x:np.array([[14.96,41.76,1024.07,73.17]])})
    print(correct_prediction)

    print(best)

【问题讨论】:

    标签: python tensorflow neural-network artificial-intelligence


    【解决方案1】:

    1.您可以调整以下几行;

    # In general baises are either initialized as zeros or not zero constant, but not Gaussian 
    b_h1 = tf.Variable(tf.zeros([n_hidden_1]))
    b_out = tf.Variable(tf.zeros([n_classes]))
    
    # MSE error
    cost = tf.reduce_mean(tf.pow(out_layer-y, 2))/(2*n_samples)
    

    此外,将数据作为小批量提供;由于您使用的优化器已针对小批量优化进行了调整;将数据作为一个整体提供并不会产生最佳性能。

    2。 对于多个输出,您只需更改 n_classes 和成本函数 (tf.nn.softmax_cross_entropy_with_logits)。您在此处定义的模型也不是线性的;因为您使用的是非线性激活函数tf.nn.relu

    【讨论】:

    • 非常感谢您的回复。我尝试将成本函数从 reduce mean square 更改为 Softmax,我收到以下错误 print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format (c)) TypeError: unsupported format string passing to numpy.ndarray.__format__ ....我尝试打印我的成本函数,当它是减少均方和 softmax 我得到以下 RMS - > Tensor("truediv:0", shape= (), dtype=float32)。 softmax -> Tensor("Reshape_2:0", shape=(?,), dtype=float32).
    • 这里的错误只是一个普通的python语法问题。你需要修复你的print 声明
    • Ishant,我在我的帖子中发布的代码中进行了以下更改...train_x = df[['AT','V','AP']] train_y = df[ ['RH','PE']] ; n_input = 3 n_classes = 2;成本= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out_layer,标签=y));在此之后,如果我正在训练我的神经网络并尝试用其中一个训练数据测试我的神经网络,我真的会得到奇怪的结果,bcoz 在每 1000 个 Epoch,成本值非常高,几乎是 4803645 ..你能不能让我知道这件事?我想,我没有在成本函数中传递正确的参数..等待您的回复
    • 您不需要将数据作为小批量提供,提供整个数据集不是一个好主意。 labels=y 也应该是 one_hot 向量; ALSO 一个输入不应该有多个输出节点,如果你的输入有多个输出,将它们的特征组合成一个输出;这里的多个输出,我的意思是多类分类。
    • 嗨,Ishant,我将研究以小批量的形式提供数据。我无法理解成本函数的构造。您能否详细解释一下我可以在我的代码中进行的更改,以便它可以用于多个输出。在我的数据中,我有 5 列,我想将前三列作为输入,后两列作为输出。这个问题不是分类吗?不是回归问题吗?
    猜你喜欢
    • 2012-01-16
    • 2021-09-22
    • 2015-02-23
    • 2017-03-16
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多