Aufgrund eines Versionsupdates wird GitLab am 01.04. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 01.04. between 9:00 and 9:30 a.m.

CNNCreator_mountaincar_agent_mountaincarCritic.py 2.29 KB
Newer Older
Nicola Gatto's avatar
Nicola Gatto committed
1 2 3
import mxnet as mx
import logging
import os
4
from CNNNet_mountaincar_agent_mountaincarCritic import Net_0
Nicola Gatto's avatar
Nicola Gatto committed
5

6 7
class CNNCreator_mountaincar_agent_mountaincarCritic:
    _model_dir_ = "model/mountaincar.agent.MountaincarCritic/"
Nicola Gatto's avatar
Nicola Gatto committed
8 9 10 11
    _model_prefix_ = "model"

    def __init__(self):
        self.weight_initializer = mx.init.Normal()
Nicola Gatto's avatar
Nicola Gatto committed
12
        self.networks = {}
Nicola Gatto's avatar
Nicola Gatto committed
13 14

    def load(self, context):
Nicola Gatto's avatar
Nicola Gatto committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        earliestLastEpoch = None

        for i, network in self.networks.items():
            lastEpoch = 0
            param_file = None

            try:
                os.remove(self._model_dir_ + self._model_prefix_ + "_" + str(i) + "_newest-0000.params")
            except OSError:
                pass
            try:
                os.remove(self._model_dir_ + self._model_prefix_ + "_" + str(i) + "_newest-symbol.json")
            except OSError:
                pass

            if os.path.isdir(self._model_dir_):
                for file in os.listdir(self._model_dir_):
                    if ".params" in file and self._model_prefix_ + "_" + str(i) in file:
                        epochStr = file.replace(".params","").replace(self._model_prefix_ + "_" + str(i) + "-","")
                        epoch = int(epochStr)
                        if epoch > lastEpoch:
                            lastEpoch = epoch
                            param_file = file
            if param_file is None:
                earliestLastEpoch = 0
            else:
                logging.info("Loading checkpoint: " + param_file)
                network.load_parameters(self._model_dir_ + param_file)

                if earliestLastEpoch == None or lastEpoch < earliestLastEpoch:
                    earliestLastEpoch = lastEpoch

        return earliestLastEpoch
Nicola Gatto's avatar
Nicola Gatto committed
48 49

    def construct(self, context, data_mean=None, data_std=None):
Nicola Gatto's avatar
Nicola Gatto committed
50 51 52
        self.networks[0] = Net_0(data_mean=data_mean, data_std=data_std)
        self.networks[0].collect_params().initialize(self.weight_initializer, ctx=context)
        self.networks[0].hybridize()
Nicola Gatto's avatar
Nicola Gatto committed
53
        self.networks[0](mx.nd.zeros((1, 2,), ctx=context), mx.nd.zeros((1, 1,), ctx=context))
Nicola Gatto's avatar
Nicola Gatto committed
54 55 56 57

        if not os.path.exists(self._model_dir_):
            os.makedirs(self._model_dir_)

Nicola Gatto's avatar
Nicola Gatto committed
58 59
        for i, network in self.networks.items():
            network.export(self._model_dir_ + self._model_prefix_ + "_" + str(i), epoch=0)