def main(): """Entry point""" pwl = GaussianSpheresPWLP(epoch_size=1000, input_dim=2, output_dim=2, clusters=[ PointWithLabel(point=torch.tensor( (-1, 0), dtype=torch.double), label=0), PointWithLabel(point=torch.tensor( (1, 0), dtype=torch.double), label=1) ], std_dev=0.4, mean=0) layers = [(50, True, False)] layer_names = ['input', 'hidden', 'output'] network = FeedforwardLarge.create(input_dim=2, output_dim=2, weights=wi.GaussianWeightInitializer( mean=0, vari=0.1, normalize_dim=1), biases=wi.ZerosWeightInitializer(), layer_sizes=layers, nonlinearity='linear', train_readout_weights=False, train_readout_bias=False) trainer = tnr.GenericTrainer( train_pwl=pwl, test_pwl=pwl, teacher=FFTeacher(), batch_size=1, learning_rate=0.003, optimizer=torch.optim.Adam( [p for p in network.parameters() if p.requires_grad], lr=0.003), criterion=mycrits.create_meansqerr_regul( noise_strength=0.5) #torch.nn.CrossEntropyLoss() ) pca3d_throughtrain.FRAMES_PER_TRAIN = 1 pca3d_throughtrain.SKIP_TRAINS = 0 pca3d_throughtrain.NUM_FRAME_WORKERS = 4 dig = npmp.NPDigestor('train_one', 5) #pca_3d.plot_ff(pca_ff.find_trajectory(network, pwl, 3), os.path.join(SAVEDIR, 'pca_3d_start'), True, # digestor=dig, frame_time=FRAME_TIME, layer_names=layer_names) dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') pca_throughtrain_dir = os.path.join(SAVEDIR, 'pca_throughtrain') (trainer.reg(tnr.EpochsTracker()).reg(tnr.EpochsStopper(10)).reg( tnr.DecayTracker()) #.reg(tnr.DecayStopper(8)) #.reg(tnr.LRMultiplicativeDecayer()) .reg(tnr.DecayOnPlateau()).reg(tnr.AccuracyTracker(5, 1000, True)) #.reg(tnr.WeightNoiser( # wi.GaussianWeightInitializer(mean=0, vari=0.02, normalize_dim=None), # lambda ctxt: ctxt.model.layers[-1].weight.data)) .reg( tnr.OnEpochCaller.create_every( satur.during_training(satur_training_dir, True, dig), skip=10)).reg( tnr.OnEpochCaller.create_every( dtt.during_training_ff(dtt_training_dir, True, dig), skip=10)).reg( tnr.OnEpochCaller.create_every( pca_ff.during_training(pca_training_dir, True, dig, alpha=0.8), skip=10)).reg( tnr.OnEpochCaller.create_every( pr.during_training_ff( pr_training_dir, True, dig), skip=1000)).reg( tnr.OnEpochCaller.create_every( svm.during_training_ff( svm_training_dir, True, dig), skip=1000)) #.reg(pca3d_throughtrain.PCAThroughTrain(pca_throughtrain_dir, layer_names, True)) .reg(tnr.OnFinishCaller(lambda *args, **kwargs: dig.join())).reg( tnr.ZipDirOnFinish(dtt_training_dir)).reg( tnr.ZipDirOnFinish(pca_training_dir)).reg( tnr.ZipDirOnFinish(pr_training_dir)).reg( tnr.ZipDirOnFinish(svm_training_dir)).reg( tnr.ZipDirOnFinish(satur_training_dir))) trainer.train(network) #pca_3d.plot_ff(pca_ff.find_trajectory(network, pwl, 3), os.path.join(SAVEDIR, 'pca_3d_end'), True, # digestor=dig, frame_time=FRAME_TIME, layer_names=layer_names) dig.archive_raw_inputs(os.path.join(SAVEDIR, 'raw_digestor.zip'))
def main(): """Entry point""" pwl = GaussianSpheresPWLP.create(epoch_size=2700, input_dim=INPUT_DIM, output_dim=OUTPUT_DIM, cube_half_side_len=2, num_clusters=10, std_dev=0.04, mean=0, min_sep=0.1) nets = cu.FluentShape(INPUT_DIM).verbose() network = FeedforwardComplex(INPUT_DIM, OUTPUT_DIM, [ nets.linear_(90), nets.nonlin('isrlu'), nets.linear_(OUTPUT_DIM), ]) trainer = tnr.GenericTrainer( train_pwl=pwl, test_pwl=pwl, teacher=FFTeacher(), batch_size=45, learning_rate=0.001, optimizer=torch.optim.Adam( [p for p in network.parameters() if p.requires_grad], lr=0.001), criterion=torch.nn.CrossEntropyLoss()) dig = npmp.NPDigestor('train_one_complex', 16) #pca_3d.plot_ff(pca_ff.find_trajectory(network, pwl, 3), os.path.join(SAVEDIR, 'pca_3d_start'), True, dig3d) #dig3d.join() #exit() dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') (trainer.reg(tnr.EpochsTracker()).reg(tnr.EpochsStopper(150)).reg( tnr.DecayTracker()).reg(tnr.DecayStopper(3)).reg( tnr.LRMultiplicativeDecayer()).reg(tnr.DecayOnPlateau()).reg( tnr.AccuracyTracker(5, 1000, True)).reg( tnr.OnEpochCaller.create_every(dtt.during_training_ff( dtt_training_dir, True), skip=1000)) #.reg(tnr.OnEpochCaller.create_every(pca_ff.during_training(pca_training_dir, True), skip=1000)) .reg( tnr.OnEpochCaller.create_every( pr.during_training_ff(pr_training_dir, True), skip=1000)).reg( tnr.OnEpochCaller.create_every( svm.during_training_ff(svm_training_dir, True), skip=1000)).reg( tnr.OnEpochCaller.create_every(satur.during_training( satur_training_dir, True), skip=1000)). reg(tnr.ZipDirOnFinish(dtt_training_dir)).reg( tnr.ZipDirOnFinish(pca_training_dir)).reg( tnr.ZipDirOnFinish(pr_training_dir)).reg( tnr.ZipDirOnFinish(svm_training_dir)).reg( tnr.ZipDirOnFinish(satur_training_dir))) trainer.train(network) torch.save(network.state_dict(), os.path.join(SAVEDIR, 'trained_network.pt'))
def main(): """Entry point""" nets = cu.FluentShape(28 * 28).verbose() network = FeedforwardComplex(INPUT_DIM, OUTPUT_DIM, [ nets.linear_(HIDDEN_DIM), nets.tanh(), nets.linear_(OUTPUT_DIM), nets.tanh() ]) train_pwl = MNISTData.load_train().to_pwl().restrict_to(set( range(10))).rescale() test_pwl = MNISTData.load_test().to_pwl().restrict_to(set( range(10))).rescale() layer_names = ('Input', 'Hidden', 'Output') trainer = tnr.GenericTrainer( train_pwl=train_pwl, test_pwl=test_pwl, teacher=FFTeacher(), batch_size=45, learning_rate=0.001, optimizer=torch.optim.Adam( [p for p in network.parameters() if p.requires_grad], lr=0.001), criterion=mycrits.meansqerr #torch.nn.CrossEntropyLoss() ) dig = npmp.NPDigestor('train_one_complex', 35) dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pca3d_training_dir = os.path.join(SAVEDIR, 'pca3d') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') trained_net_dir = os.path.join(SAVEDIR, 'trained_model') pca_throughtrain_dir = os.path.join(SAVEDIR, 'pca_throughtrain') wds_training_dir = os.path.join(SAVEDIR, 'weightdeltas') logpath = os.path.join(SAVEDIR, 'log.txt') (trainer.reg(tnr.EpochsTracker()).reg(tnr.EpochsStopper(3)).reg( tnr.DecayTracker()).reg(tnr.DecayStopper(8)).reg( tnr.LRMultiplicativeDecayer()).reg(tnr.DecayOnPlateau()). reg(tnr.AccuracyTracker(5, 1000, True)).reg( tnr.WeightNoiser( wi.GaussianWeightInitializer(mean=0, vari=0.1), (lambda ctx: ctx.model.layers[0].action.weight.data.detach()), 'scale', (lambda noise: wi.GaussianWeightInitializer(0, noise.vari * 0.5) ))).reg( tnr.OnEpochCaller.create_every(dtt.during_training_ff( dtt_training_dir, True, dig), skip=100)) #.reg(tnr.OnEpochCaller.create_every(pca_3d.during_training(pca3d_training_dir, True, dig, plot_kwargs={'layer_names': layer_names}), start=1000, skip=1000)) .reg( tnr.OnEpochCaller.create_every( pca_ff.during_training(pca_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( pr.during_training_ff(pr_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( svm.during_training_ff(svm_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( satur.during_training( satur_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( tnr.save_model(trained_net_dir), skip=100)). reg( wds.Binned2Norm( (lambda ctx: ctx.model.layers[0].action.weight.data.detach()), dig, wds_training_dir, 'Induced Changes in $W^{(1)}$')) #.reg(pca3d_throughtrain.PCAThroughTrain(pca_throughtrain_dir, layer_names, True, layer_indices=plot_layers)) .reg(tnr.OnFinishCaller(lambda *args, **kwargs: dig.join())).reg( tnr.CopyLogOnFinish(logpath)).reg( tnr.ZipDirOnFinish(dtt_training_dir)).reg( tnr.ZipDirOnFinish(pca_training_dir)).reg( tnr.ZipDirOnFinish(pca3d_training_dir)).reg( tnr.ZipDirOnFinish(pr_training_dir)).reg( tnr.ZipDirOnFinish(svm_training_dir)).reg( tnr.ZipDirOnFinish(satur_training_dir)).reg( tnr.ZipDirOnFinish(trained_net_dir))) trainer.train(network) dig.archive_raw_inputs(os.path.join(SAVEDIR, 'digestor_raw.zip'))
def main(): """Entry point""" train_pwl = MNISTData.load_train().to_pwl().restrict_to(set( range(10))).rescale() test_pwl = MNISTData.load_test().to_pwl().restrict_to(set( range(10))).rescale() layers_and_nonlins = ( (90, 'relu'), (90, 'relu'), (90, 'relu'), (90, 'relu'), (90, 'relu'), ) layers = [lyr[0] for lyr in layers_and_nonlins] nonlins = [lyr[1] for lyr in layers_and_nonlins] nonlins.append('relu') # output #layer_names = [f'{lyr[1]} (layer {idx})' for idx, lyr in enumerate(layers_and_nonlins)] layer_names = [ f'Layer {idx+1}' for idx, lyr in enumerate(layers_and_nonlins) ] layer_names.insert(0, 'Input') layer_names.append('Output') network = FeedforwardLarge.create(input_dim=train_pwl.input_dim, output_dim=train_pwl.output_dim, weights=wi.GaussianWeightInitializer( mean=0, vari=0.3, normalize_dim=0), biases=wi.ZerosWeightInitializer(), layer_sizes=layers, nonlinearity=nonlins #layer_sizes=[500, 200] ) trainer = tnr.GenericTrainer( train_pwl=train_pwl, test_pwl=test_pwl, teacher=FFTeacher(), batch_size=30, learning_rate=0.003, optimizer=torch.optim.Adam( [p for p in network.parameters() if p.requires_grad], lr=0.003), criterion=torch.nn.CrossEntropyLoss( ) #mycrits.meansqerr#torch.nn.CrossEntropyLoss()# ) #pca3d_throughtrain.FRAMES_PER_TRAIN = 4 #pca3d_throughtrain.SKIP_TRAINS = 0 #pca3d_throughtrain.NUM_FRAME_WORKERS = 6 dig = npmp.NPDigestor('train_one', 8) dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pca3d_training_dir = os.path.join(SAVEDIR, 'pca3d') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') trained_net_dir = os.path.join(SAVEDIR, 'trained_model') pca_throughtrain_dir = os.path.join(SAVEDIR, 'pca_throughtrain') logpath = os.path.join(SAVEDIR, 'log.txt') (trainer.reg(tnr.EpochsTracker()).reg(tnr.EpochsStopper(3)).reg( tnr.DecayTracker()).reg(tnr.DecayStopper(5)).reg( tnr.LRMultiplicativeDecayer()) #.reg(tnr.DecayOnPlateau()) .reg(tnr.DecayEvery(5)).reg(tnr.AccuracyTracker(5, 1000, True)).reg( tnr.OnEpochCaller.create_every(dtt.during_training_ff( dtt_training_dir, True, dig), skip=100)) #.reg(tnr.OnEpochCaller.create_every(pca_3d.during_training(pca3d_training_dir, True, dig, plot_kwargs={'layer_names': layer_names}), start=500, skip=100)) .reg( tnr.OnEpochCaller.create_every( pca_ff.during_training(pca_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( pr.during_training_ff(pr_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( svm.during_training_ff(svm_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( satur.during_training( satur_training_dir, True, dig), skip=100)).reg( tnr.OnEpochCaller.create_every( tnr.save_model(trained_net_dir), skip=100)) #.reg(pca3d_throughtrain.PCAThroughTrain(pca_throughtrain_dir, layer_names, True)) .reg(tnr.OnFinishCaller(lambda *args, **kwargs: dig.join())).reg( tnr.CopyLogOnFinish(logpath)).reg( tnr.ZipDirOnFinish(dtt_training_dir)).reg( tnr.ZipDirOnFinish(pca_training_dir)).reg( tnr.ZipDirOnFinish(pca3d_training_dir)).reg( tnr.ZipDirOnFinish(pr_training_dir)).reg( tnr.ZipDirOnFinish(svm_training_dir)).reg( tnr.ZipDirOnFinish(satur_training_dir)).reg( tnr.ZipDirOnFinish(trained_net_dir))) trainer.train(network) dig.archive_raw_inputs(os.path.join(SAVEDIR, 'digestor_raw.zip'))
def main(): """Entry point""" cu.DEFAULT_LINEAR_BIAS_INIT = wi.ZerosWeightInitializer() cu.DEFAULT_LINEAR_WEIGHT_INIT = wi.GaussianWeightInitializer( mean=0, vari=0.3, normalize_dim=0) nets = cu.FluentShape(32 * 32 * 3).verbose() network = FeedforwardComplex(INPUT_DIM, OUTPUT_DIM, [ nets.linear_(32 * 32 * 6), nets.nonlin('isrlu'), nets.linear_(500), nets.nonlin('tanh'), nets.linear_(250), nets.nonlin('tanh'), nets.linear_(250), nets.nonlin('tanh'), nets.linear_(100), nets.tanh(), nets.linear_(100), nets.tanh(), nets.linear_(100), nets.tanh(), nets.linear_(OUTPUT_DIM), nets.nonlin('isrlu'), ]) train_pwl = CIFARData.load_train().to_pwl().restrict_to(set( range(10))).rescale() test_pwl = CIFARData.load_test().to_pwl().restrict_to(set( range(10))).rescale() layer_names = ('input', 'FC -> 32*32*6 (ISRLU)', 'FC -> 500 (tanh)', 'FC -> 250 (tang)', 'FC -> 250 (tanh)', 'FC -> 100 (tanh)', 'FC -> 100 (tanh)', 'FC -> 100 (tanh)', f'FC -> {OUTPUT_DIM} (ISRLU)') plot_layers = tuple(i for i in range(2, len(layer_names) - 1)) trainer = tnr.GenericTrainer( train_pwl=train_pwl, test_pwl=test_pwl, teacher=FFTeacher(), batch_size=45, learning_rate=0.001, optimizer=torch.optim.Adam( [p for p in network.parameters() if p.requires_grad], lr=0.001), criterion=torch.nn.CrossEntropyLoss()) pca3d_throughtrain.FRAMES_PER_TRAIN = 1 pca3d_throughtrain.SKIP_TRAINS = 16 pca3d_throughtrain.NUM_FRAME_WORKERS = 1 dig = npmp.NPDigestor('train_one_complex', 5) dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pca3d_training_dir = os.path.join(SAVEDIR, 'pca3d') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') trained_net_dir = os.path.join(SAVEDIR, 'trained_model') pca_throughtrain_dir = os.path.join(SAVEDIR, 'pca_throughtrain') logpath = os.path.join(SAVEDIR, 'log.txt') (trainer.reg(tnr.EpochsTracker()).reg(tnr.EpochsStopper(STOP_EPOCH)).reg( tnr.DecayTracker()).reg(tnr.DecayStopper(8)).reg( tnr.EpochProgress(print_every=120, hint_end_epoch=STOP_EPOCH)).reg( tnr.LRMultiplicativeDecayer()).reg( tnr.DecayOnPlateau(patience=3)).reg( tnr.AccuracyTracker(1, 1000, True)).reg( tnr.OnEpochCaller.create_every( dtt.during_training_ff(dtt_training_dir, True, dig), skip=5)).reg( tnr.OnEpochCaller.create_every( pca_3d.during_training( pca3d_training_dir, True, dig, plot_kwargs={ 'layer_names': layer_names }), start=10, skip=100)). reg( tnr.OnEpochCaller.create_every( pca_ff.during_training(pca_training_dir, True, dig), skip=5)).reg( tnr.OnEpochCaller.create_every( pr.during_training_ff(pr_training_dir, True, dig, labels=False), skip=5)).reg( tnr.OnEpochCaller.create_every( svm.during_training_ff(svm_training_dir, True, dig), skip=5)).reg( tnr.OnEpochCaller.create_every( satur.during_training( satur_training_dir, True, dig), skip=5)).reg( tnr.OnEpochCaller.create_every( tnr.save_model(trained_net_dir), skip=5)) #.reg(pca3d_throughtrain.PCAThroughTrain(pca_throughtrain_dir, layer_names, True, layer_indices=plot_layers)) .reg(tnr.OnFinishCaller(lambda *args, **kwargs: dig.join())).reg( tnr.ZipDirOnFinish(dtt_training_dir)).reg( tnr.ZipDirOnFinish(pca_training_dir)).reg( tnr.ZipDirOnFinish(pca3d_training_dir)).reg( tnr.ZipDirOnFinish(pr_training_dir)).reg( tnr.ZipDirOnFinish(svm_training_dir)).reg( tnr.ZipDirOnFinish(satur_training_dir)).reg( tnr.ZipDirOnFinish(trained_net_dir)).reg( tnr.CopyLogOnFinish(logpath))) trainer.train(network) dig.archive_raw_inputs(os.path.join(SAVEDIR, 'digestor_raw.zip'))
def main(): """Entry point""" nets = cu.FluentShape(28*28) network = FeedforwardComplex( INPUT_DIM, OUTPUT_DIM, [ nets.unflatten_conv_(1, 28, 28), nets.conv_(5, 5, 5), nets.relu(), nets.maxpool_(2), nets.flatten_(invokes_callback=True), nets.linear_(nets.dims[0]), nets.tanh(), nets.linear_(OUTPUT_DIM), nets.tanh() ] ) #breakpoint() train_pwl = MNISTData.load_train().to_pwl().restrict_to(set(range(10))).rescale() test_pwl = MNISTData.load_test().to_pwl().restrict_to(set(range(10))).rescale() layer_names = ('input', 'conv2d-relu', 'maxpool', 'tanh', 'output') plot_layers = (3,) trainer = tnr.GenericTrainer( train_pwl=train_pwl, test_pwl=test_pwl, teacher=FFTeacher(), batch_size=45, learning_rate=0.001, optimizer=torch.optim.Adam([p for p in network.parameters() if p.requires_grad], lr=0.001), criterion=torch.nn.CrossEntropyLoss() ) pca3d_throughtrain.FRAMES_PER_TRAIN = 1 pca3d_throughtrain.SKIP_TRAINS = 0 pca3d_throughtrain.NUM_FRAME_WORKERS = 6 dig = npmp.NPDigestor('train_one_complex', 35) dtt_training_dir = os.path.join(SAVEDIR, 'dtt') pca_training_dir = os.path.join(SAVEDIR, 'pca') pca3d_training_dir = os.path.join(SAVEDIR, 'pca3d') pr_training_dir = os.path.join(SAVEDIR, 'pr') svm_training_dir = os.path.join(SAVEDIR, 'svm') satur_training_dir = os.path.join(SAVEDIR, 'saturation') trained_net_dir = os.path.join(SAVEDIR, 'trained_model') pca_throughtrain_dir = os.path.join(SAVEDIR, 'pca_throughtrain') (trainer .reg(tnr.EpochsTracker()) .reg(tnr.EpochsStopper(5)) .reg(tnr.DecayTracker()) .reg(tnr.DecayStopper(8)) .reg(tnr.LRMultiplicativeDecayer()) .reg(tnr.DecayOnPlateau()) .reg(tnr.AccuracyTracker(5, 1000, True)) .reg(tnr.OnEpochCaller.create_every(dtt.during_training_ff(dtt_training_dir, True, dig), skip=100)) #.reg(tnr.OnEpochCaller.create_every(pca_3d.during_training(pca3d_training_dir, True, dig, plot_kwargs={'layer_names': layer_names}), skip=100)) #.reg(tnr.OnEpochCaller.create_every(pca_ff.during_training(pca_training_dir, True, dig), skip=100)) .reg(tnr.OnEpochCaller.create_every(pr.during_training_ff(pr_training_dir, True, dig), skip=100)) .reg(tnr.OnEpochCaller.create_every(svm.during_training_ff(svm_training_dir, True, dig), skip=100)) .reg(tnr.OnEpochCaller.create_every(satur.during_training(satur_training_dir, True, dig), skip=100)) .reg(tnr.OnEpochCaller.create_every(tnr.save_model(trained_net_dir), skip=100)) .reg(pca3d_throughtrain.PCAThroughTrain(pca_throughtrain_dir, layer_names, True, layer_indices=plot_layers)) .reg(tnr.OnFinishCaller(lambda *args, **kwargs: dig.join())) .reg(tnr.ZipDirOnFinish(dtt_training_dir)) .reg(tnr.ZipDirOnFinish(pca_training_dir)) .reg(tnr.ZipDirOnFinish(pca3d_training_dir)) .reg(tnr.ZipDirOnFinish(pr_training_dir)) .reg(tnr.ZipDirOnFinish(svm_training_dir)) .reg(tnr.ZipDirOnFinish(satur_training_dir)) .reg(tnr.ZipDirOnFinish(trained_net_dir)) ) trainer.train(network) dig.archive_raw_inputs(os.path.join(SAVEDIR, 'digestor_raw.zip'))