def mlp_config_func(): alg_name = 'mlp' build_classifier = lambda scope: MLP(n_units=[128, 128, 128, 128], scope=scope) batch_size = 32 log_device_placement = False use_gpu = False loss_type = 'xent' get_updater = Updater l2_weight = 0.000 stopping_criteria = 'loss,min' threshold = -np.inf optimizer_spec = 'adam' lr_schedule = 1e-4 noise_schedule = 0.0 max_grad_norm = 1.0 build_network = ClassificationNetwork patience = 1000000 curriculum = [ dict(), dict(lr_schedule=.3 * lr_schedule), dict(lr_schedule=.09 * lr_schedule), dict(lr_schedule=0.027 * lr_schedule) ] return locals()
def _call(self, inp, output_size, is_training): if self.bg_head is None: self.bg_head = ConvNet( layers=[ dict(filters=None, kernel_size=1, strides=1, padding="SAME"), dict(filters=None, kernel_size=1, strides=1, padding="SAME"), ], scope="bg_head" ) if self.transform_head is None: self.transform_head = MLP(n_units=[64, 64], scope="transform_head") n_attr_channels, n_transform_values = output_size processed = super()._call(inp, n_attr_channels, is_training) B, F, H, W, C = tf_shape(processed) # Map processed to shapes (B, H, W, C) and (B, F, 2) bg_attrs = self.bg_head(tf.reduce_mean(processed, axis=1), None, is_training) transform_values = self.transform_head( tf.reshape(processed, (B*F, H*W*C)), n_transform_values, is_training) transform_values = tf.reshape(transform_values, (B, F, n_transform_values)) return bg_attrs, transform_values
def _build_networks(self): self.maybe_build_subnet("d_box_network", key="d_box", builder=cfg.build_lateral) self.maybe_build_subnet("predict_attr_inp", builder=cfg.build_lateral) self.maybe_build_subnet("predict_attr_temp", builder=cfg.build_lateral) self.maybe_build_subnet("d_z_network", key="d_z", builder=cfg.build_lateral) self.maybe_build_subnet("d_obj_network", key="d_obj", builder=cfg.build_lateral) if self.do_lateral and self.lateral_network is None: self.lateral_network = SpatialAttentionLayerV2( n_hidden=self.n_hidden, build_mlp=lambda scope: MLP( n_units=[self.n_hidden, self.n_hidden], scope=scope), do_object_wise=True, scope="lateral_network", ) self.maybe_build_subnet("glimpse_prime_network", key="glimpse_prime", builder=cfg.build_lateral) self.maybe_build_subnet("glimpse_prime_encoder", builder=cfg.build_object_encoder) self.maybe_build_subnet("glimpse_encoder", builder=cfg.build_object_encoder)
def _call(self, inp, output_size, is_training): if self.backbone is None: self.backbone = Backbone() if self.mlp is None: self.mlp = MLP([100, 100]) outp = self.backbone(inp, 0, is_training) outp = self.mlp(outp, output_size, is_training) return outp
def math_prepare_func(): from dps import cfg decoder_kind = cfg.decoder_kind if decoder_kind == "mlp": cfg.build_math_network = lambda scope: MLP( n_units=[256, 256, 256, 128], scope=scope) elif decoder_kind == "recurrent": cfg.build_math_network = networks.SimpleRecurrentRegressionNetwork cfg.build_math_cell = lambda scope: tf.contrib.rnn.LSTMBlockCell( cfg.n_recurrent_units) elif decoder_kind == "seq": cfg.build_math_network = networks.SequentialRegressionNetwork cfg.build_math_cell = lambda scope: tf.contrib.rnn.LSTMBlockCell( cfg.n_recurrent_units) elif decoder_kind == "attn": cfg.build_math_network = networks.AttentionRegressionNetwork cfg.ar_n_filters = 256 elif decoder_kind == "obj": cfg.build_math_network = ObjectNetwork cfg.n_repeats = 1 elif decoder_kind == "arn": cfg.build_math_network = AttentionalRelationNetwork cfg.n_repeats = 2 elif decoder_kind == "add": cfg.build_math_network = networks.AdditionNetwork cfg.math_A = 10 n_cells = (int(np.ceil(cfg.image_shape[0] / cfg.pixels_per_cell[0])) * int(np.ceil(cfg.image_shape[1] / cfg.pixels_per_cell[1]))) cfg.largest_digit = n_cells * 9 else: raise Exception( "Unknown value for decoder_kind: '{}'".format(decoder_kind)) if "stage1_path" in cfg: import os, glob # noqa pattern = os.path.join( cfg.stage1_path, "experiments", "*idx={}*repeat={}*".format(cfg.idx, cfg.repeat)) paths = glob.glob(pattern) assert len(paths) == 1 cfg.load_path = { "network/representation": os.path.join(paths[0], "weights", "best_of_stage_0") }
def prepare_func(): from dps import cfg from auto_yolo.models import networks from dps.utils.tf import AttentionalRelationNetwork, ObjectNetwork, MLP import tensorflow as tf decoder_kind = cfg.decoder_kind if decoder_kind == "mlp": cfg.build_math_network = lambda scope: MLP( n_units=[256, 256, 256, 256, 256], scope=scope) cfg.max_possible_objects = 7 elif decoder_kind == "recurrent": cfg.build_math_network = networks.SimpleRecurrentRegressionNetwork cfg.build_math_cell = lambda scope: tf.contrib.rnn.LSTMBlockCell( cfg.n_recurrent_units) elif decoder_kind == "obj": cfg.build_math_network = ObjectNetwork cfg.build_on_input_network = lambda scope: MLP(n_units=[256, 256], scope=scope) cfg.build_on_object_network = lambda scope: MLP( n_units=[256, 256, 256], scope=scope) cfg.build_on_output_network = lambda scope: MLP( n_units=[256, 256, 256], scope=scope) elif decoder_kind == "arn1": cfg.n_repeats = 1 elif decoder_kind == "arn3": cfg.n_repeats = 3 else: raise Exception( "Unknown value for decoder_kind: '{}'".format(decoder_kind)) if decoder_kind.startswith("arn"): cfg.build_math_network = AttentionalRelationNetwork cfg.build_arn_network = lambda scope: MLP(n_units=[256, 256], scope=scope) cfg.build_arn_object_network = lambda scope: MLP( n_units=[256, 256, 256], scope=scope) if "stage1_path" in cfg: import os, glob # noqa pattern = os.path.join( cfg.stage1_path, "experiments", "*idx={}*repeat={}*".format(cfg.idx, cfg.repeat)) paths = glob.glob(pattern) assert len(paths) == 1 cfg.load_path = { "network/representation": os.path.join(paths[0], "weights", "best_of_stage_0") }
no_gradient="", attr_prior_mean=0.0, attr_prior_std=1.0, z_prior_mean=0.0, z_prior_std=1.0, A=50, n_objects_per_cell=1, train_reconstruction=True, train_kl=True, reconstruction_weight=1.0, kl_weight=1.0, math_weight=0.0, train_math=False, math_A=None, noisy=True, build_background_encoder=lambda scope: MLP(n_units=[10, 10], scope=scope), build_background_decoder=IdentityFunction, max_possible_objects=None) simple_config = alg_config.copy( alg_name="simple", build_network=simple.SimpleVAE, render_hook=simple.SimpleVAE_RenderHook(), build_encoder=networks.Backbone, build_decoder=networks.InverseBackbone, n_channels=128, n_final_layers=3, kernel_size=1, pixels_per_cell=(12, 12), )
def __call__(self, param_shape, name=None): return FeedforwardCell(MLP(), param_shape, name=name)
def __call__(self, param_shape, name=None): return FeedforwardCell(MLP(*self.args, **self.kwargs), param_shape, name=name)
config=dict(do_train=False)), short=dict( max_hosts=1, ppn=2, cpp=2, gpu_set="0", wall_time="20mins", project="rpp-bengioy", cleanup_time="1mins", slack_time="1mins", n_repeats=1, n_param_settings=4), oak=dict( host_pool=[":"], kind="parallel", max_hosts=1, ppn=2, cpp=2, gpu_set="0", wall_time="1hour", project="rpp-bengioy", cleanup_time="1mins", slack_time="1mins", step_time_limit="1hour", n_repeats=10, n_param_settings=1, config=dict(max_steps=4000)), ) config = dict( n_train=1000, clevr_background_mode=None, postprocessing="random", tile_shape=(48, 48), A=1, attr_prior_std=10.0, build_encoder=lambda scope: MLP(n_units=[100, 100], scope=scope), build_decoder=lambda scope: MLP(n_units=[100, 100], scope=scope), ) envs.run_experiment( "clevr", config, readme, alg="simple", task="clevr", durations=durations, distributions=distributions )
gpu_set="0", wall_time="20mins", project="rpp-bengioy", cleanup_time="1mins", slack_time="1mins", n_repeats=1, n_param_settings=4), ) config = dict( n_train=16000, load_path={ "network/representation": "/media/data/dps_data/logs/test-yolo-air-math_env=size=14-in-colour=False-task=arithmetic-ops=addition/" "exp_alg=yolo-air-math_seed=174419635_2018_07_18_16_20_00/weights/best_of_stage_0", }, curriculum=[dict()], build_math_input=lambda scope: MLP(n_units=[512, 256, 100, 100], scope=scope), build_network=yolo_air.YoloAir_AlternateNetwork, fixed_weights="object_encoder object_decoder box obj backbone", ) envs.run_experiment("test_math", config, readme, alg="yolo_air_2stage_math", task="arithmetic", durations=durations, distributions=distributions)
def build_net(scope): from dps.utils.tf import MLP return MLP(n_units=[10, 10], scope=scope)
def __call__(self, params_dim, name=None): return CompositeCell( tf.contrib.rnn.LSTMCell(num_units=cfg.n_controller_units), DuelingHead(MLP(), MLP()), params_dim, name=name)
log_name="cnn_grid_arithmetic", patience=5000, reward_window=0.499, load_path=-1, build_env=grid_arithmetic.sl_build_env, build_model=grid_arithmetic.feedforward_build_model, mode="standard", n_controller_units=128, build_convolutional_model=lambda: LeNet(cfg.n_controller_units), largest_digit=99, # For when mode == "pretrained" fixed=True, pretrain=True, build_feedforward_model=lambda: MLP([ cfg.n_controller_units, cfg.n_controller_units, cfg.n_controller_units ]), n_raw_features=128, ) rnn_config = SL_EXPERIMENT_CONFIG.copy() rnn_config.update(env_config) rnn_config.update( name="GridArithmeticRNN", cpu_ram_limit_mb=12 * 1024, use_gpu=True, gpu_allow_growth=True, per_process_gpu_memory_fraction=0.22, stopping_criteria="01_loss,min", get_updater=grid_arithmetic.sl_get_updater,
'show_plots': True, 'mnist': 1, 'mpl_backend': 'TkAgg', 'n_controller_units': 128, 'n_train': n_examples, 'n_val': n_examples, 'batch_size': n_examples, 'reward_window': 0.5, 'start_tensorboard': False, 'test_time_explore': 0.1, 'use_gpu': 0, 'verbose': 1, 'policy_scope': 'SimpleArithmetic_policy', 'render_rollouts': render_rollouts_static, 'controller_func': lambda n_actions: CompositeCell( tf.contrib.rnn.LSTMCell(num_units=128), MLP(), n_actions) }) def build_classifier(inp, output_size, is_training=False): logits = LeNet(256, activation_fn=tf.nn.sigmoid)(inp, output_size, is_training) return tf.nn.softmax(logits) config.build_classifier = build_classifier config.action_selection = Softmax() load_from = "/home/eric/Dropbox/projects/dps/good/solves_5x5_3digits/best_stage=2" _run("visualize", "simple_arithmetic", config, load_from)
from dps import cfg from dps.config import DEFAULT_CONFIG from dps.env import BatchGymEnv from dps.utils.tf import MLP, FeedforwardCell from dps.rl import rl_render_hook, BuildSoftmaxPolicy, BuildMlpController def build_env(): return BatchGymEnv(gym_env='MountainCar-v0') controller = lambda params_dim, name: FeedforwardCell( lambda inp, output_size: MLP( [cfg.n_controller_units, cfg.n_controller_units])(inp, output_size), params_dim, name=name) config = DEFAULT_CONFIG.copy() # So far, have not been able to solve this with a policy gradient method, the exploration problem is quite hard. config.update( env_name="mountain_car", build_env=build_env, build_controller=BuildMlpController(), build_policy=BuildSoftmaxPolicy(one_hot=False), exploration_schedule="1.0",
# alg config config.update( build_controller=build_controller, controller_type="obj", d=128, # d=256, layer_norm=True, symmetric_op="max", use_mask=True, # For obj # build_on_input_network=lambda scope: MLP([128, 128], scope=scope), # build_on_object_network=lambda scope: MLP([128, 128], scope=scope), # build_on_output_network=lambda scope: MLP([128, 128, 128], scope=scope), build_on_input_network=lambda scope: MLP([128], scope=scope), build_on_object_network=lambda scope: MLP([128], scope=scope), build_on_output_network=lambda scope: MLP([128, 128], scope=scope), # For arn build_arn_network=lambda scope: MLP([128, 128], scope=scope), build_arn_object_network=lambda scope: MLP([128, 128], scope=scope), n_heads=1, exploration_schedule=1.0, val_exploration_schedule=1.0, build_policy=build_policy, ) if __name__ == "__main__": with config: env = build_env().gym_env
def __call__(self, param_shape, name=None): return CompositeCell( tf.contrib.rnn.LSTMCell(num_units=cfg.n_controller_units), MLP(), param_shape, name=name)
def build_mlp(): return MLP([cfg.n_controller_units, cfg.n_controller_units])
alg_configs['simple_vae'] = Config( build_network=SimpleVideoVAE, attr_prior_mean=0., attr_prior_std=1.0, A=128, train_reconstruction=True, reconstruction_weight=1.0, train_kl=True, kl_weight=1.0, build_encoder=lambda scope: MLP(n_units=[128, 128, 128], scope=scope), build_decoder=lambda scope: MLP(n_units=[128, 128, 128], scope=scope), build_cell=lambda scope: CompositeCell( tf.contrib.rnn.GRUBlockCellV2(128), MLP(n_units=[128], scope="GRU"), 2*128), render_hook=SimpleVAE_RenderHook(), flat_latent=True, ) alg_configs['simple_vae_conv'] = alg_configs['simple_vae'].copy( build_encoder=lambda scope: GridConvNet( layers=[ dict(filters=64, kernel_size=4, strides=1), dict(filters=64, kernel_size=4, strides=2), dict(filters=64, kernel_size=4, strides=1),