def test_problem_1(): REF_XTRAIN = "fixtures/X_expected" REF_YTRAIN = "fixtures/Y_expected" (xtrain, ytrain) = hw1.load_data("data/digitstest.txt") ref_xtrain = pickle.load(open(REF_XTRAIN, "rb"), **PICKLE_KWARGS) ref_ytrain = pickle.load(open(REF_YTRAIN, "rb"), **PICKLE_KWARGS) assert (ref_xtrain.shape == xtrain.shape) assert (ref_ytrain.shape == ytrain.shape) assert (isAllClose(ref_ytrain, ytrain)) assert (isAllClose(ref_xtrain, xtrain))
def test_problem_4(): STARTING_WEIGHTS_PATH = "fixtures/start_weights_problem4" ENDING_WEIGHTS_PATH = "fixtures/final_weights_problem4" STARTING_BIAS_PATH = "fixtures/start_bias_problem4" ENDING_BIAS_PATH = "fixtures/final_bias_problem4" PARAMS_PATH = "fixtures/problem4.params.json" params = json.loads(open(PARAMS_PATH, "r").read()) (X, Y)= hw1.load_data("data/digitstrain.txt") inputWeights = pickle.load(open(STARTING_WEIGHTS_PATH, "rb"), **PICKLE_KWARGS) finalWeights = pickle.load(open(ENDING_WEIGHTS_PATH, "rb"), **PICKLE_KWARGS) inputBias = pickle.load(open(STARTING_BIAS_PATH, "rb"), **PICKLE_KWARGS) finalBias = pickle.load(open(ENDING_BIAS_PATH, "rb"), **PICKLE_KWARGS) weightsToTest, biasToTest = hw1.update_weights_double_layer(X, Y, inputWeights, inputBias, float(params["LEARNING_RATE"])) assert(isAllClose(finalWeights, weightsToTest)) assert(isAllClose(finalBias, biasToTest))
def test_problem_6(): STARTING_WEIGHTS_PATH = "fixtures/start_weights_problem6" ENDING_WEIGHTS_PATH = "fixtures/final_weights_problem6" STARTING_BIAS_PATH = "fixtures/start_bias_problem6" ENDING_BIAS_PATH = "fixtures/final_bias_problem6" PARAMS_PATH = "fixtures/problem6.params.json" params = json.loads(open(PARAMS_PATH, "r").read()) (X, Y) = hw1.load_data("data/digitstrain.txt") inputWeights = pickle.load(open(STARTING_WEIGHTS_PATH, "rb"), **PICKLE_KWARGS) finalWeights = pickle.load(open(ENDING_WEIGHTS_PATH, "rb"), **PICKLE_KWARGS) inputBias = pickle.load(open(STARTING_BIAS_PATH, "rb"), **PICKLE_KWARGS) finalBias = pickle.load(open(ENDING_BIAS_PATH, "rb"), **PICKLE_KWARGS) inputLoss, inputAccuracy = forward_and_compute_loss( X, Y, inputWeights, inputBias, float(params["LEARNING_RATE"]), params["ACTIVATION"]) weightsToTest, biasToTest = hw1.update_weights_double_layer_act_mom( X, Y, inputWeights, inputBias, float(params["LEARNING_RATE"]), params["ACTIVATION"], float(params["MOMENTUM"]), int(params["EPOCH_COUNT"])) correctLoss, correctAccuracy = forward_and_compute_loss( X, Y, finalWeights, finalBias, float(params["LEARNING_RATE"]), params["ACTIVATION"]) myLoss, myAccuracy = forward_and_compute_loss( X, Y, weightsToTest, biasToTest, float(params["LEARNING_RATE"]), params["ACTIVATION"]) print("inputLoss: ", inputLoss, " inputAccuracy: ", inputAccuracy) print("correctLoss: ", correctLoss, " correctAccuracy: ", correctAccuracy) print("myLoss: ", myLoss, " myAccuracy: ", myAccuracy) assert (isAllClose(finalWeights, weightsToTest)) assert (isAllClose(finalBias, biasToTest))