예제 #1
0
def main():
  # Initialize experiment options and parameters
  suite = MNISTSparseExperiment()
  suite.parse_opt()
  suite.parse_cfg()
  experiments = suite.options.experiments or suite.cfgparser.sections()

  pool = multiprocessing.Pool()

  for expName in experiments:
    path = suite.get_exp(expName)[0]
    results = suite.get_exps(path=path)

    # Build argument list for multiprocessing pool
    args = []
    for exp in results:
      params = suite.get_params(exp)
      for i, minWeight in enumerate(np.linspace(0.0, 0.1, 21)):
        args.append((exp, params, minWeight, -1, i))

    args = np.array(args)

    # Analyze weight pruning alone. No dutycycle pruning
    args[:, 3] = -1  # set minDutycycle to -1 for all experiments
    run(pool, expName, "Weight Pruning", args)

    # Analyze dutycycle pruning units with dutycycle below 5% from target density
    args[:, 3] = 0.05  # set minDutycycle to 5% for all experiments
    run(pool, expName, "Dutycycle Pruning (5%)", args)


    # Analyze dutycycle pruning units with dutycycle below 10% from target density
    args[:, 3] = 0.10  # set minDutycycle to 10% for all experiments
    run(pool, expName, "Dutycycle Pruning (10%)", args)
예제 #2
0
def main():
    # Set fixed seed. Only works on cpu
    random.seed(42)
    np.random.seed(42)
    torch.manual_seed(42)

    dataset = create_union_mnist_dataset()

    # Load experiment
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    suite = MNISTSparseExperiment()
    suite.parse_opt()
    suite.parse_cfg()
    experiments = suite.options.experiments or suite.cfgparser.sections()
    table = {}
    progress = tqdm(experiments)
    for expName in progress:
        progress.set_description(expName)
        path = suite.get_exp(expName)[0]
        results = suite.get_exps(path=path)
        for exp in results:
            model_file = os.path.join(exp, "model.pt")
            if os.path.exists(model_file):
                model = torch.load(model_file, map_location=device)
                params = suite.get_params(exp)
                test_loader = torch.utils.data.DataLoader(
                    dataset,
                    shuffle=True,
                    batch_size=params["test_batch_size"])
                table[params['name']] = evaluate(model=model,
                                                 loader=test_loader,
                                                 device=device)

    # Random model
    test_loader = torch.utils.data.DataLoader(dataset,
                                              shuffle=True,
                                              batch_size=4)
    table["random"] = evaluate(model=random_model,
                               loader=test_loader,
                               device=device)

    # Save results
    df = pd.DataFrame.from_dict(table)
    df.to_csv("union_experiment_results.csv")
    print(tabulate(df, tablefmt='fancy_grid', headers='keys',
                   numalign="right"))

    # Plot first 100 images in the dataset
    fig = plt.figure(figsize=(10, 10))
    for i in range(100):
        ax = fig.add_subplot(10, 10, i + 1)
        ax.set_axis_off()
        img, label = dataset[i]
        ax.imshow(img.numpy().reshape((28, 28)), cmap='gray')
        ax.set_title(str(label.numpy()))
    plt.tight_layout()
    plt.savefig("union_experiment_sample_images.png")
    plt.close()
예제 #3
0
def main():
  # Initialize experiment options and parameters
  suite = MNISTSparseExperiment()
  suite.parse_opt()
  suite.parse_cfg()
  experiments = suite.options.experiments or suite.cfgparser.sections()

  pool = multiprocessing.Pool()

  for expName in experiments:
    path = suite.get_exp(expName)[0]
    results = suite.get_exps(path=path)

    # Build argument list for multiprocessing pool
    args = []
    for exp in results:
      params = suite.get_params(exp)
      for i, minWeight in enumerate(np.linspace(0.0, 0.1, 21)):
        args.append((exp, params, minWeight, -1, i))

    args = np.array(args)

    # Analyze weight pruning alone. No dutycycle pruning
    args[:, 3] = -1  # set minDutycycle to -1 for all experiments
    run(pool, expName, "weight_prunning", args)

    # Analyze dutycycle pruning units with dutycycle below 5% from target density
    args[:, 3] = 0.05  # set minDutycycle to 5% for all experiments
    run(pool, expName, "dutycycle_pruning", args)
예제 #4
0
    testScores = np.zeros(numExps)
    noiseScores = np.zeros(numExps)
    for i, scoresForRepetition in enumerate(
            zip(results["testerror"], results["totalCorrect"])):
        maxTestScore, bestEpoch, maxNoiseScore = bestScore(scoresForRepetition)
        testScores[i] = maxTestScore
        noiseScores[i] = maxNoiseScore

    return {
        "test_score": (testScores.mean(), testScores.std()),
        "noise_score": (noiseScores.mean(), noiseScores.std())
    }


if __name__ == '__main__':
    suite = MNISTSparseExperiment()
    suite.parse_opt()
    suite.parse_cfg()
    experiments = suite.options.experiments or suite.cfgparser.sections()

    testScoresTable = [["Network", "Test Score", "Noise Score"]]
    for expName in experiments:
        results = suite.get_exp(expName)
        for exp in results:
            if not os.path.exists(exp):
                continue

            errorBars = getErrorBars(exp, suite)
            test_score = u"{0:.2f} ± {1:.2f}".format(*errorBars["test_score"])
            noise_score = u"{0:,.0f} ± {1:.2f}".format(
                *errorBars["noise_score"])
예제 #5
0
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

from htmresearch.frameworks.pytorch.mnist_sparse_experiment import \
  MNISTSparseExperiment

# Run from htmresearch:
# domino run projects/sdr_paper/pytorch_experiments/run_experiment.py -c projects/sdr_paper/pytorch_experiments/experiments.cfg
#   OR
# python projects/sdr_paper/pytorch_experiments/run_experiment.py -c projects/sdr_paper/pytorch_experiments/experiments.cfg -d
if __name__ == '__main__':
    suite = MNISTSparseExperiment()
    suite.start()
예제 #6
0
        noiseScores[i] = maxTotalAccuracy

    print("")
    print("Experiment:", expPath, "Number of sub-experiments", len(exps))
    print("test score mean and standard deviation:", testScores.mean(),
          testScores.std())
    print("noise score mean and standard deviation:", noiseScores.mean(),
          noiseScores.std())


# To run it from htmresearch top level:
# python projects/sdr_paper/pytorch_experiments/analyze_experiment.py -c projects/sdr_paper/pytorch_experiments/experiments.cfg

if __name__ == '__main__':

    suite = MNISTSparseExperiment()

    # summarizeResults("./results", suite)
    #
    for expName in [
            # "./results/standardOneLayer",

            # Best sparse CNN net so far
            # "./results/bestSparseNetLuiz",
            # "./results/bestSparseNet",
            # "./results/bestSparseCNN",

            # This is the best sparse net (non CNN) so far, as of Jan 7.
            # "./results/bestSparseNet/k50.0n500.0",
            # "./results/DenseCNN1Seeds/seed47.0",
    ]:
예제 #7
0
# See sparse_net.py
KERNEL_SIZE = 5



def computeMaxPool(input_width):
  """
  Compute CNN max pool width. see 'cnn_sdr.py'
  """
  wout = math.floor((input_width + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1)
  return int(math.floor(wout / 2.0))



if __name__ == '__main__':
  suite = MNISTSparseExperiment()
  suite.parse_opt()
  suite.parse_cfg()
  experiments = suite.options.experiments or suite.cfgparser.sections()

  paramsTable = [["Network",
                  "L1 F", "L1 Sparsity",
                  "L2 F", "L2 Sparsity",
                  "L3 N", "L3 Sparsity",
                  "Wt Sparsity"]]
  for name in experiments:
    exps = suite.get_exps(suite.get_exp(name)[0])
    for exp in exps:
      if not os.path.exists(exp):
        continue
      params = suite.get_params(exp=exp)
예제 #8
0
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

from htmresearch.frameworks.pytorch.mnist_sparse_experiment import \
  MNISTSparseExperiment


# Run from htmresearch:
# domino run projects/sdr_paper/pytorch_experiments/run_experiment.py -c projects/sdr_paper/pytorch_experiments/experiments.cfg
#   OR
# python projects/sdr_paper/pytorch_experiments/run_experiment.py -c projects/sdr_paper/pytorch_experiments/experiments.cfg -d
if __name__ == '__main__':
  suite = MNISTSparseExperiment()
  suite.start()
예제 #9
0
            transform.transforms.pop()

            ax = fig.add_subplot(4, num_noise, y * num_noise + x + 1)
            ax.set_axis_off()
            ax.imshow(img.numpy().reshape((28, 28)), cmap='gray')
            if y == 0:
                ax.set_title("{0}%".format(noise_values[x] * 100))

    plt.tight_layout()
    plt.savefig(plotPath)
    plt.close()


if __name__ == '__main__':
    # Initialize experiment options and parameters
    suite = MNISTSparseExperiment()
    suite.parse_opt()
    suite.parse_cfg()
    path = suite.cfgparser.defaults()['path']

    # Plot Noise Curve (MNIST)
    results, format = configureNoisePlot(
        suite,
        experiments=[
            # "BestDenseOneLayer2",
            "DenseCNN1Seeds/seed49.0",
            "twoLayerDenseCNNs/k1000.0n1000.0",
            "bestSparseCNNOneLayerSeeds/seed48.0",
            "bestSparseCNNTwoLayerSeeds/seed50.0",
        ],
        labels=["dense-CNN1", "dense-CNN2", "sparse-CNN1", "sparse-CNN2"],