Exemple #1
0
    def __init__(self, creator: TensorCreator, network: nn.Module,
                 optimizer: Optimizer, storage: ObservationStorage,
                 params: NNetParams):
        """Unit constructor.

        Args:
            creator: creator of this node
            network: pytorch neural network module
            optimizer: pytorch optimizer object
            storage: baselines observation storage object
            params: baselines parameter object
        """
        super().__init__(creator.device)

        self.params = params
        self.network = network
        self.optimizer = optimizer
        self.storage = storage
        self.device = creator.device

        self.output = creator.zeros(
            SpaceEngineersConnectorConfig().task_to_agent_buffer_size,
            dtype=self._float_dtype,
            device=self._device)
        self.label = creator.zeros(
            SpaceEngineersConnectorConfig().task_to_agent_buffer_size,
            dtype=self._float_dtype,
            device=self._device)
        self.task_control = creator.zeros(4, device=self._device)

        self.cur_train_step = 0
        self.last_train_loss = 0
    def __init__(self, use_dataset: bool = True):
        super().__init__("Task 0 - Base topology",
                         inputs=Task0BaseGroupInputs(self),
                         outputs=Task0BaseGroupOutputs(self))

        if use_dataset:
            params = DatasetSeObjectsParams(
                dataset_config=DatasetConfig.TRAIN_TEST, save_gpu_memory=True)
            self.se_node = DatasetSeObjectsNode(params)
        else:
            se_config = SpaceEngineersConnectorConfig()
            se_config.curriculum = list((0, -1))
            actions_descriptor = SpaceEngineersActionsDescriptor()
            self.se_node = SpaceEngineersConnectorNode(actions_descriptor,
                                                       se_config)
    def __init__(self):
        super().__init__(device='cuda')
        self._actions_descriptor = SpaceEngineersActionsDescriptor()
        self._se_config = SpaceEngineersConnectorConfig()

        self._node_se_connector = SpaceEngineersConnectorNode(
            self._actions_descriptor, self._se_config)
        self._node_action_monitor = ActionMonitorNode(self._actions_descriptor)

        self._blank_action = ConstantNode(
            shape=self._actions_descriptor.ACTION_COUNT, constant=0)
        self._blank_task_data = ConstantNode(
            shape=self._se_config.agent_to_task_buffer_size, constant=0)
        self._blank_task_control = ConstantNode(
            shape=self._se_config.TASK_CONTROL_SIZE, constant=0)

        self.add_node(self._node_se_connector)
        self.add_node(self._node_action_monitor)
        self.add_node(self._blank_action)
        self.add_node(self._blank_task_data)
        self.add_node(self._blank_task_control)

        Connector.connect(self._blank_action.outputs.output,
                          self._node_action_monitor.inputs.action_in)
        Connector.connect(self._node_action_monitor.outputs.action_out,
                          self._node_se_connector.inputs.agent_action)
        Connector.connect(self._blank_task_data.outputs.output,
                          self._node_se_connector.inputs.agent_to_task_label)
        Connector.connect(self._blank_task_control.outputs.output,
                          self._node_se_connector.inputs.task_control)
Exemple #4
0
    def __init__(self):
        super().__init__(device='cuda')

        self._se_config = SpaceEngineersConnectorConfig()
        self.SX = self._se_config.render_width
        self.SY = self._se_config.render_height

        # setup the params
        # SeToyArchDebugTopology.config_se_communication()
        expert_params = self.get_expert_params()

        # create the nodes
        self._actions_descriptor = SpaceEngineersActionsDescriptor()
        self._node_se_connector = SpaceEngineersConnectorNode(
            self._actions_descriptor, self._se_config)
        self._node_action_monitor = ActionMonitorNode(self._actions_descriptor)

        self._blank_action = ConstantNode(
            shape=self._actions_descriptor.ACTION_COUNT, constant=0)
        self._blank_task_data = ConstantNode(
            shape=self._se_config.agent_to_task_buffer_size, constant=0)
        self._blank_task_control = ConstantNode(
            shape=self._se_config.TASK_CONTROL_SIZE, constant=0)

        # parent_rf_dims = (self.lrf_width, self.lrf_height, 3)
        self._node_lrf = ReceptiveFieldNode((self.SY, self.SX, 3),
                                            Size2D(self.SX // EOX,
                                                   self.SY // EOY))

        self._node_flock = ExpertFlockNode(expert_params)

        self.add_node(self._node_se_connector)
        self.add_node(self._node_action_monitor)
        self.add_node(self._blank_action)
        self.add_node(self._blank_task_data)
        self.add_node(self._blank_task_control)
        self.add_node(self._node_flock)
        self.add_node(self._node_lrf)

        Connector.connect(self._blank_action.outputs.output,
                          self._node_action_monitor.inputs.action_in)
        Connector.connect(self._node_action_monitor.outputs.action_out,
                          self._node_se_connector.inputs.agent_action)

        Connector.connect(self._blank_task_data.outputs.output,
                          self._node_se_connector.inputs.agent_to_task_label)
        Connector.connect(self._blank_task_control.outputs.output,
                          self._node_se_connector.inputs.task_control)

        # SE -> Flock (no LRF)
        # Connector.connect(self._node_se_connector.outputs.image_output,
        # self._node_flock.inputs.sp.data_input)

        # Image -> LRF -> Flock
        Connector.connect(self._node_se_connector.outputs.image_output,
                          self._node_lrf.inputs[0])
        Connector.connect(self._node_lrf.outputs[0],
                          self._node_flock.inputs.sp.data_input)
Exemple #5
0
    def __init__(self):
        super().__init__(device='cuda')
        self._actions_descriptor = SpaceEngineersActionsDescriptor()
        self._se_config = SpaceEngineersConnectorConfig()
        self._se_config.skip_frames = 1
        self._se_config.render_width = self.SIZE.value
        self._se_config.render_height = self.SIZE.value
        num_training_trajectories = 3000
        num_testing_trajectories = 100

        overseer = SampleCollectionOverseer(self._se_config.render_width,
                                            self._se_config.render_height,
                                            num_training_trajectories,
                                            num_testing_trajectories)

        self._node_se_connector = SpaceEngineersConnectorNode(
            self._actions_descriptor,
            self._se_config,
            sample_collection_overseer=overseer)
        self._node_action_monitor = ActionMonitorNode(self._actions_descriptor)

        self._blank_action = ConstantNode(
            shape=self._actions_descriptor.ACTION_COUNT, constant=0)
        self._blank_task_data = ConstantNode(
            shape=self._se_config.agent_to_task_buffer_size, constant=0)
        self._blank_task_control = ConstantNode(
            shape=self._se_config.TASK_CONTROL_SIZE, constant=0)

        self.add_node(self._node_se_connector)
        self.add_node(self._node_action_monitor)
        self.add_node(self._blank_action)
        self.add_node(self._blank_task_data)
        self.add_node(self._blank_task_control)

        Connector.connect(self._blank_action.outputs.output,
                          self._node_action_monitor.inputs.action_in)
        Connector.connect(self._node_action_monitor.outputs.action_out,
                          self._node_se_connector.inputs.agent_action)
        Connector.connect(self._blank_task_data.outputs.output,
                          self._node_se_connector.inputs.agent_to_task_label)
        Connector.connect(self._blank_task_control.outputs.output,
                          self._node_se_connector.inputs.task_control)
Exemple #6
0
 def __init__(self, curriculum: tuple = (0, -1)):
     self._curriculum = curriculum
     self.se_config = SpaceEngineersConnectorConfig()
     self.se_config.curriculum = list(self._curriculum)
Exemple #7
0
    def __init__(self,
                 seed: int = 0,
                 device: str = 'cuda',
                 eox: int = 2,
                 eoy: int = 2,
                 num_cc: int = 100,
                 batch_s=300,
                 tp_learn_period=50,
                 tp_max_enc_seq=1000,
                 se_skip_frames=9):
        super().__init__(eox, eoy)

        self._se_config = SpaceEngineersConnectorConfig()
        self._se_config.skip_frames = se_skip_frames
        self._se_config.curriculum = [0, -1]

        # compute/setup parameters of the model
        _, self._sy, self._sx, self._no_channels = init_se_dataset_world_params(
            random_order=False)
        flock_size, input_size = compute_flock_sizes(self._sy, self._sx,
                                                     self._no_channels,
                                                     self._eoy, self._eox)
        expert_params = setup_flock_params(no_clusters=num_cc,
                                           buffer_size=batch_s * 2,
                                           batch_size=batch_s,
                                           tp_learn_period=tp_learn_period,
                                           max_enc_seq=tp_max_enc_seq,
                                           flock_size=flock_size,
                                           input_size=input_size)
        flock_input_size, flock_output_size = compute_lrf_params(
            self._sy, self._sx, self._no_channels, self._eoy, self._eox)

        # SE nodes
        self._actions_descriptor = SpaceEngineersActionsDescriptor()
        self._node_se_connector = SpaceEngineersConnectorNode(
            self._actions_descriptor, self._se_config)
        self._node_action_monitor = ActionMonitorNode(self._actions_descriptor)
        self._blank_action = ConstantNode(
            shape=self._actions_descriptor.ACTION_COUNT, constant=0)
        self._blank_task_data = ConstantNode(
            shape=self._se_config.agent_to_task_buffer_size, constant=0)

        # flock-related nodes
        self._lrf_node = ReceptiveFieldNode(flock_input_size,
                                            flock_output_size)
        self._flock_node = ExpertFlockNode(expert_params, seed=seed)
        self._zero_context = ConstantNode(
            shape=(expert_params.flock_size, NUMBER_OF_CONTEXT_TYPES,
                   expert_params.temporal.incoming_context_size),
            constant=0)
        self._blank_task_control = ConstantNode(
            shape=self._se_config.TASK_CONTROL_SIZE, constant=0)

        # add nodes to the graph
        self.add_node(self._lrf_node)
        self.add_node(self._flock_node)
        self.add_node(self._zero_context)
        self.add_node(self._node_se_connector)
        self.add_node(self._node_action_monitor)
        self.add_node(self._blank_action)
        self.add_node(self._blank_task_data)
        self.add_node(self._blank_task_control)

        # connect SE -> LRF -> SP
        Connector.connect(self._node_se_connector.outputs.image_output,
                          self._lrf_node.inputs[0])
        Connector.connect(self._lrf_node.outputs[0],
                          self._flock_node.inputs.sp.data_input)
        Connector.connect(self._zero_context.outputs.output,
                          self._flock_node.inputs.tp.context_input)

        # connect NOOP -> action_override
        Connector.connect(self._blank_action.outputs.output,
                          self._node_action_monitor.inputs.action_in)
        Connector.connect(self._node_action_monitor.outputs.action_out,
                          self._node_se_connector.inputs.agent_action)

        # connect blank_task_data -> SE aux input
        Connector.connect(self._blank_task_data.outputs.output,
                          self._node_se_connector.inputs.agent_to_task_label)
        Connector.connect(self._blank_task_control.outputs.output,
                          self._node_se_connector.inputs.task_control)

        # prepare for run
        set_global_seeds(seed)
        self._last_step_duration = 0
    def __init__(self, curriculum: tuple = (1, -1)):
        super().__init__()

        se_config = SpaceEngineersConnectorConfig()
        se_config.render_width = 16
        se_config.render_height = 16
        se_config.curriculum = list(curriculum)

        base_expert_params = ExpertParams()
        base_expert_params.flock_size = 1
        base_expert_params.n_cluster_centers = 100
        base_expert_params.compute_reconstruction = False
        base_expert_params.spatial.cluster_boost_threshold = 1000
        base_expert_params.spatial.learning_rate = 0.2
        base_expert_params.spatial.batch_size = 1000
        base_expert_params.spatial.buffer_size = 1010
        base_expert_params.spatial.learning_period = 100

        base_expert_params.temporal.batch_size = 1000
        base_expert_params.temporal.buffer_size = 1010
        base_expert_params.temporal.learning_period = 200
        base_expert_params.temporal.forgetting_limit = 20000

        # parent_expert_params = ExpertParams()
        # parent_expert_params.flock_size = 1
        # parent_expert_params.n_cluster_centers = 20
        # parent_expert_params.compute_reconstruction = True
        # parent_expert_params.temporal.exploration_probability = 0.9
        # parent_expert_params.spatial.cluster_boost_threshold = 1000
        # parent_expert_params.spatial.learning_rate = 0.2
        # parent_expert_params.spatial.batch_size = 1000
        # parent_expert_params.spatial.buffer_size = 1010
        # parent_expert_params.spatial.learning_period = 100
        # parent_expert_params.temporal.context_without_rewards_size = se_config.LOCATION_SIZE_ONE_HOT

        # SE nodes
        actions_descriptor = SpaceEngineersActionsDescriptor()
        node_se_connector = SpaceEngineersConnectorNode(
            actions_descriptor, se_config)
        node_action_monitor = ActionMonitorNode(actions_descriptor)

        # flock-related nodes
        flock_node = ExpertFlockNode(base_expert_params)
        blank_task_control = ConstantNode((se_config.TASK_CONTROL_SIZE, ))
        blank_task_labels = ConstantNode((20, ))

        # parent_flock_node = ExpertFlockNode(parent_expert_params)

        join_node = JoinNode(flatten=True)

        actions = ['FORWARD', 'BACKWARD', 'LEFT', 'RIGHT']
        action_count = len(actions)

        pass_actions_node = PassNode(output_shape=(action_count, ),
                                     name="pass actions")
        fork_node = ForkNode(
            0, [base_expert_params.n_cluster_centers, action_count])

        def squeeze(inputs, outputs):
            outputs[0].copy_(inputs[0].squeeze())

        squeeze_node = LambdaNode(
            squeeze,
            1, [(base_expert_params.n_cluster_centers + action_count, )],
            name="squeeze lambda node")

        def stack_and_unsqueeze(inputs, outputs):
            outputs[0].copy_(torch.stack([inputs[0], inputs[1]]).unsqueeze(0))

        stack_unsqueeze_node = LambdaNode(
            stack_and_unsqueeze,
            2, [(1, 2, se_config.LOCATION_SIZE_ONE_HOT)],
            name="stack and unsqueeze node")

        to_one_hot_node = ToOneHotNode()

        action_parser_node = AgentActionsParserNode(actions_descriptor,
                                                    actions)

        random_node = RandomNumberNode(0,
                                       action_count,
                                       name="random action generator",
                                       generate_new_every_n=5,
                                       randomize_intervals=True)

        switch_node = SwitchNode(2)

        # add nodes to the graph
        self.add_node(flock_node)
        # self.add_node(parent_flock_node)
        self.add_node(node_se_connector)
        self.add_node(node_action_monitor)
        self.add_node(blank_task_control)
        self.add_node(blank_task_labels)
        # self.add_node(join_node)
        # self.add_node(fork_node)
        # self.add_node(pass_actions_node)
        # self.add_node(squeeze_node)
        # self.add_node(to_one_hot_node)
        # self.add_node(stack_unsqueeze_node)
        self.add_node(action_parser_node)
        self.add_node(random_node)
        # self.add_node(switch_node)

        # first layer
        Connector.connect(node_se_connector.outputs.image_output,
                          flock_node.inputs.sp.data_input)

        # Connector.connect(
        #     flock_node.outputs.tp.projection_outputs,
        #     join_node.inputs[0]
        # )
        # Connector.connect(
        #     pass_actions_node.outputs.output,
        #     join_node.inputs[1]
        # )

        # # second layer
        # Connector.connect(
        #     join_node.outputs.output,
        #     parent_flock_node.inputs.sp.data_input
        # )

        # Connector.connect(
        #     node_se_connector.outputs.task_to_agent_location_one_hot,
        #     stack_unsqueeze_node.inputs[0]
        # )
        # Connector.connect(
        #     node_se_connector.outputs.task_to_agent_location_target_one_hot,
        #     stack_unsqueeze_node.inputs[1]
        # )
        # Connector.connect(
        #     stack_unsqueeze_node.outputs[0],
        #     parent_flock_node.inputs.tp.context_input
        # )
        #
        # # actions
        # Connector.connect(
        #     parent_flock_node.outputs.sp.predicted_reconstructed_input,
        #     squeeze_node.inputs[0]
        # )
        # Connector.connect(
        #     squeeze_node.outputs[0],
        #     fork_node.inputs.input
        # )
        # Connector.connect(
        #     fork_node.outputs[1],
        #     to_one_hot_node.inputs.input
        # )
        # Connector.connect(
        #     random_node.outputs.one_hot_output,
        #     switch_node.inputs[0]
        # )
        # Connector.connect(
        #     to_one_hot_node.outputs.output,
        #     switch_node.inputs[1]
        # )
        # Connector.connect(
        #     switch_node.outputs.output,
        #     action_parser_node.inputs.input
        # )
        # directly use random exploration
        Connector.connect(random_node.outputs.one_hot_output,
                          action_parser_node.inputs.input)

        Connector.connect(action_parser_node.outputs.output,
                          node_action_monitor.inputs.action_in)
        # Connector.connect(
        #     switch_node.outputs.output,
        #     pass_actions_node.inputs.input,
        #     is_low_priority=True
        # )
        Connector.connect(
            node_action_monitor.outputs.action_out,
            node_se_connector.inputs.agent_action,
            # is_low_priority=True
            is_backward=False)

        # blank connection
        Connector.connect(blank_task_control.outputs.output,
                          node_se_connector.inputs.task_control)
        Connector.connect(blank_task_labels.outputs.output,
                          node_se_connector.inputs.agent_to_task_label)

        # Save the SE connector so we can check testing/training phase.
        # When the se_io interface has been added, this can be removed.
        self._node_se_connector = node_se_connector
    def __init__(self, curriculum: tuple = (1, -1)):
        super().__init__("Task 1 - Base topology world", inputs=Task1BaseGroupWorldInputs(self),
                         outputs=Task1BaseGroupWorldOutputs(self))

        actions_descriptor = SpaceEngineersActionsDescriptor()

        actions = ['FORWARD', 'BACKWARD', 'LEFT', 'RIGHT']
        action_count = len(actions)

        # SE nodes
        se_config = SpaceEngineersConnectorConfig()
        se_config.curriculum = list(curriculum)

        node_se_connector = SpaceEngineersConnectorNode(actions_descriptor, se_config)

        def node_se_connector_is_learning() -> bool:
            if node_se_connector.outputs.metadata_testing_phase.tensor is None:
                return False
            else:
                return node_se_connector.outputs.metadata_testing_phase.tensor.cpu().item() == 1

        node_se_connector.is_learning = node_se_connector_is_learning

        self.node_se_connector = node_se_connector

        blank_task_control = ConstantNode((se_config.TASK_CONTROL_SIZE,))

        pass_actions_node = PassNode(output_shape=(action_count,), name="pass actions")

        blank_task_labels = ConstantNode((20,))

        self.add_node(node_se_connector)
        self.add_node(blank_task_control)
        self.add_node(pass_actions_node)
        self.add_node(blank_task_labels)

        Connector.connect(
            self.inputs.last_actions.output,
            pass_actions_node.inputs.input
        )

        Connector.connect(
            self.inputs.current_actions.output,
            node_se_connector.inputs.agent_action
        )

        Connector.connect(
            pass_actions_node.outputs.output,
            self.outputs.actions.input
        )

        Connector.connect(
            node_se_connector.outputs.image_output,
            self.outputs.image.input
        )

        Connector.connect(
            node_se_connector.outputs.task_to_agent_location_one_hot,
            self.outputs.current_location.input
        )

        Connector.connect(
            node_se_connector.outputs.task_to_agent_location_target_one_hot,
            self.outputs.target_location.input
        )

        # blank connection
        Connector.connect(blank_task_control.outputs.output,
                          node_se_connector.inputs.task_control)
        Connector.connect(blank_task_labels.outputs.output,
                          node_se_connector.inputs.agent_to_task_label)
Exemple #10
0
    def __init__(self, **kwargs):
        super().__init__(device='cpu')

        self._current_step = 0

        # set topology params and configs
        self._params = NNetParams(NNetParams.default_params())
        self._params.set_params(_nn_node_params)  # params defined in this file
        self._params.set_params(kwargs)  # params defined in GUI

        # SE config and setup
        self._se_config = SpaceEngineersConnectorConfig()
        self._se_config.curriculum = list(self._params.curriculum)
        self._actions_descriptor = SpaceEngineersActionsDescriptor()
        # set SE specific params automatically
        self._params.set_params({
            'input_shape':
            (3, self._se_config.render_width, self._se_config.render_height),
            'output_size':
            self._se_config.agent_to_task_buffer_size
        })

        # observation storage params
        self._observation_types = {
            'x': (self._params.buffer_size,
                  *self._params.input_shape),  # observations
            'y':
            (self._params.buffer_size, self._params.output_size),  # labels
        }

        # data storage
        self._storage = ObservationStorage(self._params.buffer_size,
                                           self._observation_types)
        self._storage.to('cpu' if self._params.mixed_mode else self.device)

        # network needs to have the global seeds to have set before creating (outside of the node in this case)
        set_global_seeds(seed=self._params.seed)

        # ==================================================
        # NOTE: Replace here with your own architecture.
        #       It needs to be able to take the correct
        #       input and output (shape/size)
        # ==================================================
        # neural network setup
        self._network = NNet(
            input_shape=self._params.input_shape,
            output_shape=self._params.output_size).to(
                'cuda' if self._params.mixed_mode else self.device)
        # ==================================================

        # neural net optimizer
        self._optimizer = optim.Adam(self._network.parameters(),
                                     lr=self._params.lr)

        # SE Node
        self._se_connector = SpaceEngineersConnectorNode(
            self._actions_descriptor, self._se_config)

        # NNet Node
        self._nnet_node = NNetNode(self._network,
                                   self._optimizer,
                                   self._storage,
                                   self._params,
                                   name='Neural Network Node')

        # add nodes to the topology
        self.add_node(self._nnet_node)
        self.add_node(self._se_connector)

        # connect it all up
        Connector.connect(self._se_connector.outputs.image_output,
                          self._nnet_node.inputs.input)

        Connector.connect(self._se_connector.outputs.task_to_agent_label,
                          self._nnet_node.inputs.label)

        Connector.connect(self._se_connector.outputs.metadata_testing_phase,
                          self._nnet_node.inputs.testing_phase)

        Connector.connect(self._nnet_node.outputs.output,
                          self._se_connector.inputs.agent_action,
                          is_backward=True)

        Connector.connect(self._nnet_node.outputs.label,
                          self._se_connector.inputs.agent_to_task_label,
                          is_backward=True)

        # necessary, but not used connector
        # TODO: remove once node is not needing this
        Connector.connect(self._nnet_node.outputs.task_control,
                          self._se_connector.inputs.task_control,
                          is_backward=True)