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.請問您可以讓我知道這個?我認爲,我沒有在成本函數中傳遞正確的參數。等待您的回覆 –