2016-04-07 19 views
-5

Şu anda basit bir tek katman algılayıcı algoritması oluşturuyorum. Bir .txt dosyasında yer almalı ve algoritma bunun üzerinden geçmelidir. Şu anda çalışıp çalışmadığını test etmek için algoritma ve sadece kodlanmış örnek veri değerlerine sahibim, ancak bir dosyadan mevcut veri değerlerini beslemem gerekiyor. Çalışmayı denedim, ancak kodlamadaki deneyimsizliğimden dolayı, başaramadım. Saçımı çekerken, bu kodun dış veri ile çalışmamı sağlayabilmesi bana yardımcı olabilirdi. Veri, aşağıdaki gibi biçimlendirilmiştir (açıkça, mermi numaraları olmadan).C# Perceptron Algoritması (Bir dosyada okuma)

  1. 0,651769
  2. 0,651604
  3. 0,651609
  4. 0,651679
  5. 0,651667
  6. 0,651699

Güncel Kodu:

using System; 
using System.IO; 
using System.Collections.Generic; 
namespace Network 
{ 
public class Network 

{ 
    static void Main() 
    { 
     // Load sample input patterns. 
     double[,] inputs = new double[,] { 

      {0.99, 0.99}, {0.99, 0.99}, {0.99, 0.99}, {0.99, 095}}; 

     // Load sample output patterns. 
     int[] outputs = new int[] {0, 1, 0, 0 }; 
     int patternCount = inputs.GetUpperBound(0) + 1; 

     // Randomise weights. 
     Random rdm = new Random(); 

     // Setting randomly generated weights between -0.5 and +0.5 
     double[] weights = {rdm.NextDouble()-0.5, rdm.NextDouble()-0.5, rdm.NextDouble()}; 

     // Set learning rate. 
     double learningRate = 1; 

     // Start iteration at 0 
     int iteration = 1; 
     double ErrorRate; 

     do 
     { 
      // Global error set to 0 
      ErrorRate = 0; 
      for (int j = 0; j < patternCount; j++) 
      { 
       // Calculate output. 
       int output = Output(weights, inputs[j, 0], inputs[j, 1]); 
       // Calculate error. 
       double localError = outputs[j] - output; 

       //if the localError is not equal to zero 
       if (localError != 0) 
       { 
        // Update weights. 
        for (int i = 0; i < 2; i++) 
        { 
         weights[i] += learningRate * localError * inputs[j, i]/2; 
        } 
       } 
       // Convert error to absolute value. 
       ErrorRate += (localError); 
      } 

      Console.WriteLine("Iteration {0}\tError {1}", iteration, ErrorRate); 
      iteration++; 

     // If the Error is equal to zero then calculate 
     } while (ErrorRate != 0); 

     // Convergence 
     Console.WriteLine(); 
     Console.WriteLine("[Input1] [Input2] [Output]"); 
     // Input1 values 
     for (double input1 = 0; input1 <= 1; input1 += 1) 
     { 
     // Input2 values 
      for (double input2 = 0; input2 <= 1; input2 += 1) 
      { 
       // Calculate output with the inputs and the randomly generated weights 
       int output = Output(weights, input1, input2); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.WriteLine(" {0}   {1}  {2}", input1, input2, (output == 1) ? "1" : "0"); 
      } 
     } 
     Console.Read(); 
    } 

    private static int Output(double[] weights, double input1, double input2) 
    { 
     // Output = input1 * weight1 + input2 * weight2 + bias * weight3 
     double sum = input1 * weights[0] + input2 * weights[1] + 1 * weights[2]; 

     // If the first condition is true, then it becomes the result. If not, the second condition becomes the result 
     return (sum >= 0) ? 1 : 0; 
    } 
} 
} 
// stackoverflow: Eğer sadece bir dosyadan satırları okumak için bir yol istiyorsanız
+3

oku arıyorsunuz (MSDN link bakınız) deneyin. com/help/mcve) örnek. Temel olarak, 'algılayıcı' ve 'algoritma' sorusundan ve buradaki tüm koddan kaldırın ve "Bir metin dosyasında oku" seçeneğini sorun. Daha sonra biri onu kopya olarak işaretleyebilir ve bazı cevaplara yönlendirebilir. Metin dosyanız hangi formatta? CSV? Satır başına bir girdi var mı? Bir metin dosyasında okumaya çalıştığınız hiçbir kod göremiyorum. – Quantic

+1

Tam olarak neyin yanlış olduğunu açıklamanız gerekir. Kodunuzun hangi kısmı çalışmıyor? –

+0

Aşağıdaki kodun, bir txt dosyasında (başka bir formatta) satır okumayı değiştirmek için aşağıdan yukarıya doğru olan, aşağıdan yukarıya doğru sıraya ihtiyacım var. Şu anda algoritma sadece çift tipli bir dizinin içindeki kodlanmış veri değerlerinden çalışıyor. çift [,] girişler = yeni çift [,] { {0.99, 0.99}, {0.99, 0.99}, {0.99, 0.99}, {0.99, 095}}; – Patrick

cevap

0

Bir [MCVE] (http oluşturma hakkında

using System.Globalization; 
    using System.IO; 
    using System.Linq 

    ... 

    double[] data = File 
    .ReadLines(@"C:\MyFile.txt") //TODO: put actual file here 
    .Select(line => Double.Parse(line, CultureInfo.InvariantCulture)) 
    .ToArray();