public static void Run() { Random rnd = new Random(); FieldLetterTranslator.addChar((char)10); Console.WriteLine("Generate started"); DataSet data = new TextDataSetGenerator(@"C:\Users\Kubik.HOME-PC\Dropbox\shakespear.txt"); Console.WriteLine("Generate comlpeat"); int inputDimension = FieldLetterTranslator.Letters.Length; int hiddenDimension = 128; int outputDimension = FieldLetterTranslator.Letters.Length; int hiddenLayers = 2; double learningRate = 0.0012; double initPatStdDev = 0.08; INonlinearity lin = new SigmoidUnit(); NeuralNetwork network = NetworkBuilder.MakeLstm(inputDimension, hiddenDimension, hiddenLayers, outputDimension, lin, initPatStdDev, rnd); string output; int reportEveryNthEpoch = 50; int trainingEpochs = 50; for (int i = 0; i < trainingEpochs; i++) { Trainer.train <NeuralNetwork>(1, learningRate, network, data, reportEveryNthEpoch, rnd); if (Directory.Exists(@"C:\Users\Kubik.HOME-PC\Documents\NeuralsTraing4\step" + i.ToString())) { Directory.Delete(@"C:\Users\Kubik.HOME-PC\Documents\NeuralsTraing4\step" + i.ToString(), true); } learningRate *= 0.85; Directory.CreateDirectory(@"C:\Users\Kubik.HOME-PC\Documents\NeuralsTraing4\step" + i.ToString()); NetworkBuilder.SaveLSTM(network, @"C:\Users\Kubik.HOME-PC\Documents\NeuralsTraing4\step" + i.ToString()); output = generateOutput(network, 'a', 1000); using (StreamWriter sw = new StreamWriter(Path.Combine(@"C:\Users\Kubik.HOME-PC\Documents\NeuralsTraing4\outputs", "output" + i.ToString() + ".txt"))) { sw.WriteLine(output); } } }
static void Main(string[] args) { //ExampleDsr.Run(); incomplete //Thread t = new Thread(() => { // ExampleText.Run(); //}, 1000000000); //t.Start(); //ExampleXor.Run(); Random rnd = new Random(); while (true) { NeuralNetwork network = new NeuralNetwork(null); if (ask("chcete síť načíst (y) nebo vygenerovat novou (n)?: ")) { while (true) { try { Console.Write("Zadejte cestu ke složce se uloženou sítí: "); string path = Console.ReadLine(); Console.WriteLine("-------------------------LOADING STARTED----------------------------"); network = NetworkBuilder.LoadLSTM(path); Console.WriteLine("-------------------------LOADING COMPLEAT---------------------------"); break; } catch { Console.WriteLine("Zadali jste neplatnou cestu"); } } } else { int hiddenDimension = askInt("Zadejte dimenzi skryté vrstvy: "); int hiddenLayers = askInt("Zadejte počet skrytých vrstev: "); double init = askDouble("Zadejte hodnotu, která určí rozsah vygenerovaných vah: "); network = NetworkBuilder.MakeLstm(FieldLetterTranslator.Letters.Length, hiddenDimension, hiddenLayers, FieldLetterTranslator.Letters.Length, new SigmoidUnit(), init, rnd); } if (ask("Přejete si síť naučit (y) nebo jen vygenerovat výstup (n)?: ")) { int epochs = askInt("Zadejte počet epoch, který se má síť naučit: "); double lr = askDouble("Zadejte počáteční hodnotu learning rate: "); double dr = askDouble("Zadejte hodnotu, kterou chcete learing rate po každé epoše násobit: "); while (true) { try { Console.Write("Zadejte cestu k tréninkovým datům: "); string learningPath = Console.ReadLine(); Console.Write("Zadejte cestu ke složce, kam chcete ukládat výstupy z učení: "); string savePath = Console.ReadLine(); ExampleText.Run(network, learningPath, savePath, epochs, lr, dr, rnd); break; } catch { Console.WriteLine("zadali jste špatnou cestu"); } } } else { int numOfChars = askInt("Zadejte počet znaků, které chcete vygenerovat: "); while (true) { Console.Write("Zadejte cestu k souboru, kam chcete výstup uložit: "); string path = Console.ReadLine(); char start = (char)rnd.Next(97, 123); Console.WriteLine("-------------------------GENERATE STARTED----------------"); string output = start + ExampleText.generateOutput(network, start, numOfChars); try { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path)) { sw.WriteLine(output); } break; } catch { Console.WriteLine("Zadali jste neplatnou cestu"); } } Console.WriteLine("-------------------------GENERATE COMPLEAT---------------"); } if (ask("Přejete si aplikaci ukočit?: ")) { break; } } }
public void Predict() { if (!Config.Reload && File.Exists(Config.RnnPredictedXFile) && File.Exists(Config.RnnPredictedYFile)) { return; } Random rng = new Random(Config.Random.Next()); CustomDataSet data = new CustomDataSet(Config); RnnConfig rnnConfig = (RnnConfig)Serializer.Deserialize(Config.RnnConfigFile); int inputDimension = data.Training[0].Steps[0].Input.Rows; int hiddenDimension = 30; int outputDimension = data.Training[0].Steps[0].TargetOutput.Rows; int hiddenLayers = 1; double learningRate = 0.01; double initParamsStdDev = 0.08; double dropout = 0.5; double inDropout = 0.8; INetwork nn = NetworkBuilder.MakeLstm(inputDimension, hiddenDimension, hiddenLayers, outputDimension, new LinearUnit(), initParamsStdDev, rng, dropout, inDropout, Config); //nn = NetworkBuilder.MakeFeedForward(inputDimension, // hiddenDimension, // hiddenLayers, // outputDimension, // new SigmoidUnit(), // new LinearUnit(), // initParamsStdDev, rng, dropout, inDropout, Config); int reportEveryNthEpoch = 10; int trainingEpochs = 100; Trainer.train <NeuralNetwork>(trainingEpochs, learningRate, nn, data, reportEveryNthEpoch, rng); StreamWriter predictedXFile = new StreamWriter(Config.RnnPredictedXFile); StreamWriter predictedYFile = new StreamWriter(Config.RnnPredictedYFile); for (int i = 0; i < data.Testing.First().Steps.Count; ++i) { DataStep ds = data.Testing.First().Steps[i]; Graph g = new Graph(false); // Generate in dropout bool[] dropped = new bool[ds.Input.W.Length]; for (int col = 0; col < dropped.Length; ++col) { dropped[col] = Math.Abs(rnnConfig.GetTransformed(0, i, col, ds.Input.W[col])) < 0.0000001; } Matrix input = new Matrix(ds.Input.W); Matrix output = nn.Activate(input, g, dropped); // Write into file string line1 = ""; string line2 = ""; foreach (double d in output.W) { line1 += d + ";"; } foreach (double d in ds.TargetOutput.W) { line2 += d + ";"; } predictedXFile.WriteLine(line1.Substring(0, line1.Length - 1)); predictedYFile.WriteLine(line2.Substring(0, line2.Length - 1)); } predictedXFile.Close(); predictedYFile.Close(); }