/// <summary> /// Create new NeuralNetworkTrainer. /// </summary> /// <param name="nn">NeuralNetwork to train.</param> /// <param name="inputDat">Input data.</param> /// <param name="targetDat">Target data.</param> /// <param name="maxUnrollLen">Memory state unroll times, for recurring layers.</param> /// <param name="losType">Loss calculation type, NeuralNetworkTrainer.LOSS_TYPE_AVERAGE/MAX/CROSSENTROPY.</param> public NeuralNetworkTrainer(NeuralNetwork nn, float[][] inputDat, float[][] targetDat, int maxUnrollLen, int losType) { neuralNetwork = nn; inputData = inputDat; targetData = targetDat; maxUnrollLength = maxUnrollLen; if (maxUnrollLength < 1) { maxUnrollLength = 1; } lossType = losType; //check for recurring layer, if need to stack and unroll if (nn.outputLayer.recurring) { hasRecurring = true; } else { for (int i = 0; i < nn.hiddenLayers.Length; i++) { if (nn.hiddenLayers[i].recurring) { hasRecurring = true; break; } } } derivatives.Setup(nn); adagradMemory.Setup(nn); adagradMemory.Reset(); int tunrollLen = maxUnrollLength; if (!hasRecurring) { tunrollLen = 1; } stackedRuntimeContext = new NeuralNetworkContext[tunrollLen]; stackedFullContext = new NeuralNetworkFullContext[tunrollLen]; stackedDerivativeMemory = new NeuralNetworkPropagationState[tunrollLen]; for (int i = 0; i < stackedRuntimeContext.Length; i++) { stackedRuntimeContext[i] = new NeuralNetworkContext(); stackedRuntimeContext[i].Setup(nn); stackedFullContext[i] = new NeuralNetworkFullContext(); stackedFullContext[i].Setup(nn); stackedDerivativeMemory[i] = new NeuralNetworkPropagationState(); stackedDerivativeMemory[i].Setup(nn, stackedRuntimeContext[i], stackedFullContext[i], derivatives); } }
/// <summary> /// Create new NeuralNetworkQLearning system. /// </summary> /// <param name="nn">NeuralNetwork to train.</param> /// <param name="inputDat">Input data.</param> /// <param name="targetDat">Target data.</param> public NeuralNetworkQLearning(NeuralNetwork nn, int maxUnrollLen, string sessionsFileName) { neuralNetwork = nn; learningSessionsFile = sessionsFileName; maxUnrollLength = maxUnrollLen; if (maxUnrollLength < 1) { maxUnrollLength = 1; } //check for recurring layer, if need to stack and unroll for (int i = 0; i < nn.hiddenLayers.Length; i++) { if (nn.hiddenLayers[i].recurring) { hasRecurring = true; break; } } derivatives.Setup(nn); if (!hasRecurring) { maxUnrollLength = 1; } stackedRuntimeContext = new NeuralNetworkContext[maxUnrollLength]; stackedFullContext = new NeuralNetworkFullContext[maxUnrollLength]; stackedDerivativeMemory = new NeuralNetworkPropagationState[maxUnrollLength]; for (int i = 0; i < stackedRuntimeContext.Length; i++) { stackedRuntimeContext[i] = new NeuralNetworkContext(); stackedRuntimeContext[i].Setup(nn); stackedFullContext[i] = new NeuralNetworkFullContext(); stackedFullContext[i].Setup(nn); stackedDerivativeMemory[i] = new NeuralNetworkPropagationState(); stackedDerivativeMemory[i].Setup(nn, stackedRuntimeContext[i], stackedFullContext[i], derivatives); } /* * if (hasRecurring) * { * recurringMemoryState = new float[nn.hiddenLayers.Length][]; * for (int i = 0; i < nn.hiddenLayers.Length; i++) * { * if (nn.hiddenLayers[i].recurring) * { * recurringMemoryState[i] = new float[nn.hiddenLayers[i].numberOfNeurons]; * } * } * }*/ }
/// <summary> /// Create new NeuralNetworkGenerator. /// </summary> /// <param name="nn">NeuralNetwork to train.</param> public NeuralNetworkGenerator(NeuralNetwork nn, int maxUnrollLen) { neuralNetwork = nn; maxUnrollLength = maxUnrollLen; //check for recurring layer, if need to stack and unroll for (int i = 0; i < nn.hiddenLayers.Length; i++) { if (nn.hiddenLayers[i].recurring) { hasRecurring = true; break; } } derivatives.Setup(nn); if (hasRecurring) { recurringBPBuffer = new float[nn.hiddenLayers.Length][]; for (int i = 0; i < recurringBPBuffer.Length - 1; i++) { if (nn.hiddenLayers[i].recurring) { recurringBPBuffer[i] = new float[nn.hiddenLayers[i].numberOfNeurons]; } } } else { maxUnrollLen = 1; } stackedRuntimeContext = new NeuralNetworkContext[maxUnrollLen]; stackedFullContext = new NeuralNetworkFullContext[maxUnrollLen]; stackedDerivativeMemory = new NeuralNetworkPropagationState[maxUnrollLen]; for (int i = 0; i < maxUnrollLen; i++) { stackedRuntimeContext[i] = new NeuralNetworkContext(); stackedRuntimeContext[i].Setup(nn); stackedFullContext[i] = new NeuralNetworkFullContext(); stackedFullContext[i].Setup(nn); stackedDerivativeMemory[i] = new NeuralNetworkPropagationState(); stackedDerivativeMemory[i].Setup(nn, stackedRuntimeContext[i], stackedFullContext[i], derivatives); stackedDerivativeMemory[i].inputMem = new float[nn.inputLayer.numberOfNeurons]; } }