2016-08-13 73 views
1

我想使用Encog來訓練關於此數據的神經網絡link。有17個輸入功能(2個數字,15個分類)和2個輸出功能(均爲分類)。使用Encog在紙牌遊戲中出價的神經網絡

我想創建一個基本的前饋網絡來解決這個問題,但我迄今爲止的努力未能收斂。我的網絡的設計是:

  • 輸入層:57個節點
    • 2個節點爲在列中的得分爲AB列CE現有出價
    • 3節點(使用-1無, 0表示通過,2爲二,和3對三)在列中的6張牌
    • 52節點GQ(在「六熱」矢量)
  • 隱藏層:104層的節點(只是一個猜測基於2 * 57)
  • 輸出層:13個節點(3非通出價* 4個套裝+ 1通出價)

我使用的tanh作爲激活函數和已啓用的偏壓節點。建立這個網絡(在C#)的調用是:

Encog.Util.Simple.EncogUtility.SimpleFeedForward(nInputs, nHidden, 0, nOutputs, true) 

我新的神經網絡,所以我真的不知道如何處理這一點。到目前爲止,我剛剛使用了試驗和錯誤,但我希望有更好的方法。謝謝。

回答

0

我在下面的代碼中輸入了您的數據。我收到培訓錯誤,並會看到我是否可以解決問題。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.OleDb; 
using System.IO; 


using Encog.Neural.Networks; 
using Encog.Neural.Networks.Layers; 
using Encog.Engine.Network.Activation; 
using Encog.ML.Data; 
using Encog.Neural.Networks.Training.Propagation.Resilient; 
using Encog.ML.Train; 
using Encog.ML.Data.Basic; 
using Encog; 
using System.Data; 
using System.Data.OleDb; 
using System.IO; 


namespace encog_sample_csharp 
{ 
    internal class Program 
    { 
     /// <summary> 
     /// Input for the XOR function. 
     /// </summary> 
     const string FILENAME = @"c:\temp\BidTraining.csv"; 

     static DataTable dt = null; 
     private static void Main(string[] args) 
     { 

      CSVReader reader = new CSVReader(); 
      DataSet ds = reader.ReadCSVFile(FILENAME, true); 
      dt = ds.Tables["Table1"]; 


      // create a neural network, without using a factory 
      var network = new BasicNetwork(); 
      network.AddLayer(new BasicLayer(null, true, 17)); 
      network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 2)); 
      network.Structure.FinalizeStructure(); 
      network.Reset(); 
      Dictionarys dict = new Dictionarys(); 

      // create training dat 
      IMLDataSet dataSet = dict.GetDataSet(dt); 

      // train the neural network 
      IMLTrain train = new ResilientPropagation(network, dataSet); 

      int epoch = 1; 

      do 
      { 
       train.Iteration(); 
       Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); 
       epoch++; 
      } while (train.Error > 0.01); 

      train.FinishTraining(); 

      // test the neural network 
      Console.WriteLine(@"Neural Network Results:"); 
      foreach (IMLDataPair pair in dataSet) 
      { 
       IMLData output = network.Compute(pair.Input); 
       Console.WriteLine(pair.Input[0] + @"," + pair.Input[1] 
            + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]); 
      } 

      EncogFramework.Instance.Shutdown(); 
     } 
    } 
    public class Dictionarys 
    { 
     public double[][] inputNeurons; 
     public double[][] outputNeurons; 
     public static Dictionary<string, double> bid = new Dictionary<string, double>(){ 
      {"None", -1.0}, 
      {"Pass", 0.0}, 
      {"One", 1.0}, 
      {"Two", 2.0}, 
      {"Three", 3.0}, 
      {"Four", 4.0}, 
      {"Five", 5.0}, 
      {"Six", 6.0} 
     }; 
     public static Dictionary<string, double> rank = new Dictionary<string, double>() { 
      {"Ace", 1.0}, 
      {"Two", 2.0}, 
      {"Three", 3.0}, 
      {"Four", 4.0}, 
      {"Five", 5.0}, 
      {"Six", 6.0}, 
      {"Seven", 7.0}, 
      {"Eight", 8.0}, 
      {"Nine", 9.0}, 
      {"Ten", 10.0}, 
      {"Jack", 11.0}, 
      {"Queen", 12.0}, 
      {"King", 13.0} 
     }; 
     public static Dictionary<string, double> suit = new Dictionary<string, double>() { 
      {"None",-1.0}, 
      {"SuitA",1.0}, 
      {"SuitB",2.0}, 
      {"SuitC",3.0}, 
      {"SuitD",4.0} 
     }; 

     public IMLDataSet GetDataSet(DataTable dt) 
     { 
      inputNeurons = dt.AsEnumerable().Select(x => new[] { 
       (double)x.Field<int>(0), 
       (double)x.Field<int>(1), 
       bid[x.Field<string>(2)], 
       bid[x.Field<string>(3)], 
       bid[x.Field<string>(4)], 
       rank[x.Field<string>(5)], 
       suit[x.Field<string>(6)], 
       rank[x.Field<string>(7)], 
       suit[x.Field<string>(8)], 
       rank[x.Field<string>(9)], 
       suit[x.Field<string>(10)], 
       rank[x.Field<string>(11)], 
       suit[x.Field<string>(12)], 
       rank[x.Field<string>(13)], 
       suit[x.Field<string>(14)], 
       rank[x.Field<string>(15)], 
       suit[x.Field<string>(16)] 
      }).ToArray(); 

      outputNeurons = dt.AsEnumerable().Select(x => new[] { 
       bid[x.Field<string>(17)], 
       suit[x.Field<string>(18)] 
      }).ToArray(); 

      IMLDataSet trainingSet = new BasicMLDataSet(inputNeurons, outputNeurons);  
      return trainingSet; 
     } 


    } 
    public class CSVReader 
    { 

     public DataSet ReadCSVFile(string fullPath, bool headerRow) 
     { 

      string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1); 
      string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1); 
      DataSet ds = new DataSet(); 

      try 
      { 
       if (File.Exists(fullPath)) 
       { 
        string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No"); 
        string SQL = string.Format("SELECT * FROM {0}", filename); 
        OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr); 
        adapter.Fill(ds, "TextFile"); 
        ds.Tables[0].TableName = "Table1"; 
       } 
       foreach (DataColumn col in ds.Tables["Table1"].Columns) 
       { 
        col.ColumnName = col.ColumnName.Replace(" ", "_"); 
       } 
      } 

      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
      return ds; 
     } 
    } 
} 
+0

該代碼有效,但訓練永遠循環。我沒有使用SimpleFeedForward(),它可能比Encog網頁上的示例代碼訓練得更好。我想我有你有相同的收斂錯誤。 – jdweng

+0

感謝您的嘗試。 – brianberns

+0

我的代碼正在收斂〜2。如果你改變while循環條件(train.Error> 2.1而不是0.01),你可能會得到結果。我基於代碼的例子是執行100%定義的異或。您的輸入不是100%定義的,因此收斂數字更高。隨着更多出價被添加到輸入,火車錯誤應該下降.. – jdweng