Ejemplo n.º 1
0
        iters += 1

    return x_proc, errors


if __name__ == "__main__":
    random.seed(8)
    numStates = 25
    numFeatures = 10
    filesLocation = "Outputs/imSaves/RMSProp/"
    print(
        "Error LeastSq,Error TP, Error LstSq - Error TP,Iterations for lower error than Least Squares"
    )
    for iter in range(10):
        A = generator.GenerateRandomMatrix(numStates, numFeatures, max=10)
        b = generator.GenerateRandomVector(numStates)
        start_x = generator.GenerateRandomVector(numFeatures)
        probDist = 1 / numStates * np.ones(numStates)
        errorType = 8
        maxIters = 5000

        np.set_printoptions(precision=3)
        lstSq = la.inv(A.T @ A) @ (A.T @ b)

        lstSqError = errorCalcs.getErrorMethod(A,
                                               b,
                                               lstSq,
                                               probDist,
                                               errorType,
                                               norm=2)
        momentumChoices = [0.5]
Ejemplo n.º 2
0
def runChoices(numberofRunsChoice, maxIterationsChoice, stochasticTypeChoice,
               sizeofLinearSystemChoice, randomInputsTypeChoice,
               stepSizeTypeChoice, momentumTypeChoice, momentumParamChoice,
               momentumParam2Choice):
    # We create lists for possible choices below:
    numberofRunsQues, numberofRunsList, maxIterationsQues, maxIterationsList, stochasticTypeQues, stochasticTypeList, \
    sizeofLinearSystemQues, sizeofLinearSystemList, randomInputTypeQues, randomInputTypeList, \
    stepSizeTypeQues, stepSizeTypeList, momentumTypeQues, momentumTypeList, \
    momentumParamQues, momentumParamList, momentumParam2Ques, momentumParam2List = interface.choicesData()

    # We retrieve all user inputs
    numberofRuns = numberofRunsList[numberofRunsChoice - 1]
    maxIterations = maxIterationsList[maxIterationsChoice - 1]
    stochasticType = stochasticTypeList[stochasticTypeChoice - 1]
    sizeofLinearSystem = sizeofLinearSystemList[sizeofLinearSystemChoice - 1]
    randomInputType = randomInputTypeList[randomInputsTypeChoice - 1]
    stepSizeType = stepSizeTypeList[stepSizeTypeChoice - 1]
    momentumType = momentumTypeList[momentumTypeChoice - 1]
    momentumParam = momentumParamList[momentumParamChoice - 1]
    momentumParam2 = momentumParam2List[momentumParam2Choice - 1]

    # We get the size of the system
    numStates, numFeatures = getSizeofLinearSystemFromSizeString(
        sizeofLinearSystem)

    # We use seed so that all files generated use same seed for repeatability
    random.seed(8)
    # Below is the location where the log files and charts generated will be stored
    filesLocation = "Outputs/runAllOutputs/"
    simName = getSimName(sizeofLinearSystem, stochasticType, randomInputType,
                         stepSizeType, momentumType, momentumTypeChoice,
                         momentumParam, momentumParam2)
    logFileName = simName + "_Log.txt"
    # We now create the file
    f = open(filesLocation + logFileName, "w")
    f.close()
    errorType = 8
    # We set some basic variables below
    maxIters = maxIterations
    totalErrors, meanErrors = np.zeros(maxIters), np.zeros(maxIters)
    totalIterations = numberofRuns
    # We loop for each iteration below
    for iter in range(totalIterations):
        # We generate the required matrices
        A, b = generateAbasPerChoiceInputs(randomInputsTypeChoice, numStates,
                                           numFeatures)
        start_x = generator.GenerateRandomVector(numFeatures)
        # This is the transition probability matrix, now set to uniform
        probDist = 1 / numStates * np.ones(numStates)
        # We set the print rules
        np.set_printoptions(precision=3)
        # This is the main caller. It calls CodeRunner from oneRing.py which retrieves all errors for choices.
        x_out, errors = oneRing.CodeRunner(A,
                                           b,
                                           start_x,
                                           probDist,
                                           maxIters,
                                           errorType=errorType,
                                           stochasticType=stochasticType,
                                           stepSizeType=stepSizeType,
                                           momentumType=momentumType,
                                           momentumParam=momentumParam,
                                           momentumParam2=momentumParam2)
        # We now write these errors to file.
        f = open(filesLocation + logFileName, "a")
        f.write("Errors for iteration: %d\n" % (iter + 1))
        for i in range(len(errors)):
            f.write("%d,%.2e\n" % (i + 1, errors[i]))
        f.close()
        # We compute the total error so far
        totalErrors = totalErrors + errors

        # We plot the errors into chart
        chartTitle = getChartName(sizeofLinearSystem, stochasticType,
                                  randomInputType, stepSizeType, momentumType,
                                  momentumTypeChoice, momentumParam,
                                  momentumParam2)
        chartFileName = str(iter + 1) + "-" + simName

        DrawingCharting.drawSavePlot(maxIters,
                                     errors,
                                     chartTitle,
                                     label='Total Projections Error',
                                     color="mediumorchid",
                                     logErrors=True,
                                     plotShow=False,
                                     location=filesLocation,
                                     fileName=chartFileName + ".png")

    # We now calculate the mean error over all the iterations
    meanErrors = totalErrors / totalIterations
    # We paste these mean errors into the log file
    f = open(filesLocation + logFileName, "a")
    f.write("Average Errors\n")
    for i in range(len(meanErrors)):
        f.write("%d,%.2e\n" % (i + 1, meanErrors[i]))
    f.close()