Ejemplo n.º 1
0
 def __init__(self):
     """
     Default constructor.
     """
     super(BaseAgent, self).__init__()
     self._log = logger.get_logger(self.__class__.__name__)
     self._load_configuration()
Ejemplo n.º 2
0
 def __init__(self):
     """
     Default constructor.
     """
     self._config_handler = KatherineApplication.get_application_config()
     if not self._config_handler:
         raise ValueError("No config_handler specified")
     self._log = logger.get_logger(self.__class__.__name__)
Ejemplo n.º 3
0
    def __init__(self, config_uri: str):
        """
        Default constructor.

        :param config_uri:
            RFC-3986 Uniform Resource Identifier (URI)
        """
        self._log = logger.get_logger(self.__class__.__name__)
        self.yaml = ConfigProcessor()
        self.settings = {}
        self._load_configuration(config_uri)
Ejemplo n.º 4
0
    def __init__(self, name: Optional[str] = None):
        """Creates an instance of `Network`.

        Args:
          name: A string representing the name of the network.
        """
        self._log = logger.get_logger(self.__class__.__name__)
        self._config_handler = KatherineApplication.get_application_config()
        self._serializer = KatherineApplication.get_application_factory(
        ).build_model_serializer()
        self._name = name or None
        self._load_configuration()
Ejemplo n.º 5
0
 def __init__(self):
     """
     Default constructor.
     """
     self._log = logger.get_logger(self.__class__.__name__)
     self._config_handler = KatherineApplication.get_application_config()
     if not self._config_handler:
         raise ValueError("No config specified.")
     self._game = KatherineApplication.get_application_factory().build_game()
     self._agent = KatherineApplication.get_application_factory().build_agent()
     self._metrics = KatherineApplication.get_application_factory().build_metrics_tracer()
     self._load_configuration()
Ejemplo n.º 6
0
 def __init__(self):
     """
     Default constructor.
     """
     self._log = logger.get_logger(self.__class__.__name__)
     config_handler = KatherineApplication.get_application_config()
     self.enabled_metrics = config_handler.get_config_property(
         TensorBoardConfigurationProperty.ENABLED_NETWORK_METRICS,
         TensorBoardConfigurationProperty.ENABLED_NETWORK_METRICS.prop_type)
     self.is_profiler_enabled = config_handler.get_config_property(
         TensorBoardConfigurationProperty.GPU_PROFILER_ENABLED,
         TensorBoardConfigurationProperty.GPU_PROFILER_ENABLED.prop_type)
     self.work_directory = fileio.build_metrics_work_directory(WORKING_DIRECTORY_PREFIX)
     self.initialized = False
Ejemplo n.º 7
0
# |    \ / _` | __| '_ \ / _ \ '__| | '_ \ / _ \ #
# | |\  \ (_| | |_| | | |  __/ |  | | | | |  __/ #
# \_| \_/\__,_|\__|_| |_|\___|_|  |_|_| |_|\___| #
#                                                #
# General Video Game AI                          #
# Copyright (C) 2020-2021 d33are                 #
##################################################

from kat_api import IPropertyDescriptor, ITensorDescriptor, IObservation
from kat_typing import Tensor
from kat_framework.core.descriptors import TensorDescriptor
from kat_framework.util import logger
from typing import Collection
import numpy as np

log = logger.get_logger(__name__)


def check_same_tensor_structure(input_tensor: Tensor,
                                property_desc: ITensorDescriptor,
                                reduce_batch_dim: bool = False) -> bool:
    """
    Checks that the specified tensor is compatible or not with the provided descriptor.

    :param input_tensor:
        tensor to check
    :param property_desc:
        descriptor checked by
    :param reduce_batch_dim:
        batch dimension is needed or not
    :return:
Ejemplo n.º 8
0
#  _   __      _   _               _             #
# | | / /     | | | |             (_)            #
# | |/ /  __ _| |_| |__   ___ _ __ _ _ __   ___  #
# |    \ / _` | __| '_ \ / _ \ '__| | '_ \ / _ \ #
# | |\  \ (_| | |_| | | |  __/ |  | | | | |  __/ #
# \_| \_/\__,_|\__|_| |_|\___|_|  |_|_| |_|\___| #
#                                                #
# General Video Game AI                          #
# Copyright (C) 2020-2021 d33are                 #
##################################################

from kat_api import IReadOnlyMemory, IReplayMemory
from kat_typing import IterableDataset
from kat_framework.util import logger

log = logger.get_logger(__name__ + ".MemoryAccessor")


class MemoryAccessor(IReadOnlyMemory):
    """
    Read only replay memory wrapper.
    """

    # protected members

    _replay_memory: IReplayMemory = None

    # public members

    def __init__(self):
        """
Ejemplo n.º 9
0
 def __init__(self):
     """
     Default constructor.
     """
     super(UniformMemory, self).__init__()
     self._log = logger.get_logger(self.__class__.__name__)
Ejemplo n.º 10
0
from kat_framework.util import reflection, logger

LOGO_STRING = \
    "\n ##################################################\n \
#  _   __      _   _               _             #\n \
# | | / /     | | | |             (_)            #\n \
# | |/ /  __ _| |_| |__   ___ _ __ _ _ __   ___  #\n \
# |    \\ / _` | __| '_ \\ / _ \\ '__| | '_ \\ / _ \\ #\n \
# | |\\  \\ (_| | |_| | | |  __/ |  | | | | |  __/ #\n \
# \\_| \\_/\\__,_|\\__|_| |_|\\___|_|  |_|_| |_|\\___| #\n \
#                                                #\n \
# General Video Game AI                          #\n \
# Copyright (C) 2020-2021 d33are                 #\n \
##################################################\n"

log = logger.get_logger(__name__ + ".KatherineApplication")


class KatherineApplication:
    """
    Katherine application entrypoint.
    """
    application_factory: IFactory
    config_handler: IConfigurationHandler

    @staticmethod
    def init(factory_class: str, config_handler_class: str,
             config_uri: str) -> None:
        """
        Runtime initialization.
Ejemplo n.º 11
0
class TensorboardTracer(IMetricTracer):
    """
    Tensorflow based metrics collector implementation.

    # see : IMetricTracer
    """

    _log = logger.get_logger(__name__ + ".TensorboardTracer")
    config_handler: IConfigurationHandler = None
    enabled_metrics: Collection[KatMetrics] = None
    is_profiler_enabled: bool = False
    work_directory: str = None
    strategy: DistributionStrategy = None
    metrics: dict = None
    summary_writer: tf.summary.SummaryWriter = None
    initialized: bool = False

    def __init__(self):
        """
        Default constructor.
        """
        config_handler = KatherineApplication.get_application_config()
        self.enabled_metrics = config_handler.get_config_property(
            TensorBoardConfigurationProperty.ENABLED_NETWORK_METRICS,
            TensorBoardConfigurationProperty.ENABLED_NETWORK_METRICS.prop_type)
        self.is_profiler_enabled = config_handler.get_config_property(
            TensorBoardConfigurationProperty.GPU_PROFILER_ENABLED,
            TensorBoardConfigurationProperty.GPU_PROFILER_ENABLED.prop_type)
        self.work_directory = fileio.build_metrics_work_directory(WORKING_DIRECTORY_PREFIX)

    @overrides
    def is_initialized(self) -> bool:
        """
        Initialization flag.

        # see : IMetricTracer.is_initialized()
        """
        return self.initialized

    @overrides
    def init(self, distribution_strategy: Optional[DistributionStrategy] = None):
        """
        Object initialization.

        # see : IMetricTracer.init(distribution_strategy)
        """
        if distribution_strategy is None:
            self.strategy = tf.distribute.get_strategy()
        else:
            self.strategy = distribution_strategy
        with self.strategy.scope():
            self.metrics = self._build_metrics()
            self.summary_writer = tf.summary.create_file_writer(self.work_directory)
        self.initialized = True

    @overrides
    def start_profiler(self):
        """
        # see : IMetricTracer.start_profiler()
        """
        if self.is_profiler_enabled:
            tf.profiler.experimental.start(self.work_directory)

    @overrides
    def stop_profiler(self):
        """
        # see : IMetricTracer.stop_profiler()
        """
        if self.is_profiler_enabled:
            tf.profiler.experimental.stop()

    @overrides
    def flush_metrics(self, epoch_number: int) -> None:
        """
        # see : IMetricTracer.flush_metrics(epoch_number)
        """
        with self.summary_writer.as_default():
            for key, value in self.metrics.items():
                if MetricType.SCALAR == key.metric_type:
                    if key.metric_clazz is None:
                        tf.summary.scalar(key.label, data=value, step=epoch_number)
                    else:
                        tf.summary.scalar(key.label, value.result(), step=epoch_number)
                elif MetricType.IMAGE == key.metric_type:
                    tf.summary.image(key.label, value, step=epoch_number)
                elif MetricType.HYPER_PARAMETER == key.metric_type:
                    raise NotImplemented
                elif MetricType.FAIRNESS_INDICATOR == key.metric_type:
                    raise NotImplemented
                elif MetricType.EMBEDDING == key.metric_type:
                    raise NotImplemented

    @overrides
    def update_metric(self, metadata: TraceableMetric, data: MetricData) -> None:
        """
        # see : IMetricTracer.update_metric(metadata, data)
        """
        if metadata is None:
            raise ValueError("No metadata specified")
        if data is None:
            raise ValueError("No data specified")
        metric = self.metrics.get(metadata)
        if metric is not None and metadata.metric_clazz is not None:
            metric(data)
        elif metric is not None:
            self.metrics[metadata] = data
        else:
            if self._log.isEnabledFor(DEBUG):
                self._log.debug("For metric {} the tracing is not enabled from configuration.".format(metadata))

    # protected member functions

    def _build_metrics(self):
        """
        Builds the metrics dictionary based on the provided metadata.
        """
        return dict(map(lambda m: (m, metrics.build_metric_from_metadata(m, tf.keras.metrics.Metric)),
                        self.enabled_metrics))