2

我的输入数据如下:构建神经网络的多路输出

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 
.................................... 

我基本上是在使用Python Tensorflow图书馆工作。 截至目前,我有一个线性模型,它可以很好地处理4个输入和1个输出。这基本上是一个回归问题。例如:在用足够的数据训练我的神经网络之后(比如说数据的大小是10000),然后在训练我的神经网络时,如果我将值45,30,25,32作为输入,它就是返回值46作为输出。

我基本上有两个疑问:

  1. 截至目前,在我的代码,我使用的参数 training_epochslearning_rate等我到现在为止给人training_epochs的 值10000.So ,当我通过传递四个输入值来测试我的神经网络 时,我得到的输出为 约471.25,而我预计它为460.但是如果我将的值设为为20000而不是10000,我得到 我的输出值为120.5,这完全没有关闭e与 相比的实际值“460”。

能否请您解释一下,怎么能在我的代码选择的training_epochslearning_rate值(或任何其他参数值),这样我就可以得到很好的精度。

  • 现在,第二个问题是,我的神经网络以现在只对线性数据工作 以及仅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) 
    

    回答

    1

    1.you可以调整这些以下行;

    # 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

    +0

    非常感谢您的回复。我试着将我的成本函数从reduce mean square改为softmax,我得到以下错误信息(“Epoch:”,'%04d'%(epoch + 1),“cost =”,“{:.9f}”。 (c)) TypeError:不支持的格式字符串传递给numpy.ndarray .__ format__ ....我尝试打印我的成本函数时,它是减少均方和softmax我得到以下RMS - >张量(“truediv:0”,形状=(),dtype = float32)。 softmax - > Tensor(“Reshape_2:0”,shape =(?,),dtype = float32)。 –

    +0

    这里的错误只是一个普通的python语法问题。你需要修正你的'print'声明 –

    +0

    Ishant,我在我的帖子中发布的代码中做了以下更改... train_x = df [['AT','V','AP']] train_y = df [['RH','PE']]; n_input = 3 n_classes = 2; cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = out_layer,labels = y));在此之后,如果我正在训练我的神经网络并尝试使用其中一个火车数据测试我的神经网络,我真的会变得更加奇怪的结果,在第1000个时代bcoz,成本价值非常高,几乎4803645.请问您可以让我知道这个?我认为,我没有在成本函数中传递正确的参数。等待您的回复 –