コード例 #1
0
    def _serializeAndExportTransferAgent(self, toAgentParams):
        with open('ToAgentExtracted/data') as jsonFile:
            data = json.load(jsonFile)

        serializedData = data_to_json(data)
        serializedParams = params_to_bytes(toAgentParams)

        serializedParamList = json.dumps(
            list(toAgentParams.keys()),
            indent=4
        )

        # Add zip to save path
        if isinstance(self.transferAgentSavePath, str):
            _, ext = os.path.splitext(self.transferAgentSavePath)
            if ext == "":
                self.transferAgentSavePath += ".zip"

        # Serialize data and write to zip file
        with zipfile.ZipFile(self.transferAgentSavePath, "w") as file_:
            if serializedData is not None:
                file_.writestr("data", serializedData)
            if toAgentParams is not None:
                file_.writestr("parameters", serializedParams)
                file_.writestr("parameter_list", serializedParamList)
コード例 #2
0
def save_to_file_zip(save_path, data=None, params=None):
    """Save model to a .zip archive
        :param save_path: (str or file-like) Where to store the model
        :param data: (OrderedDict) Class parameters being stored
        :param params: (OrderedDict) Model parameters being stored
        """
    # data/params can be None, so do not
    # try to serialize them blindly
    if data is not None:
        serialized_data = data_to_json(data)
    if params is not None:
        serialized_params = params_to_bytes(params)
        # We also have to store list of the parameters
        # to store the ordering for OrderedDict.
        # We can trust these to be strings as they
        # are taken from the Tensorflow graph.
        serialized_param_list = json.dumps(list(params.keys()), indent=4)

    # Check postfix if save_path is a string
    if isinstance(save_path, str):
        _, ext = os.path.splitext(save_path)
        if ext == "":
            save_path += ".zip"

    # Create a zip-archive and write our objects
    # there. This works when save_path
    # is either str or a file-like
    with zipfile.ZipFile(save_path, "w") as file_:
        # Do not try to save "None" elements
        if data is not None:
            file_.writestr("data", serialized_data)
        if params is not None:
            file_.writestr("parameters", serialized_params)
            file_.writestr("parameter_list", serialized_param_list)
コード例 #3
0
    def save(self, save_path):
        if self.is_pickle():
            assert save_path.endswith(".pkl")

            with open(save_path, "wb") as file_:
                cloudpickle.dump((self.data, self.params), file_)
        else:
            assert save_path.endswith(".zip")

            data = self.data
            params = self.params

            if data is not None:
                serialized_data = data_to_json(data)
            if params is not None:
                serialized_params = params_to_bytes(params)
                # We also have to store list of the parameters
                # to store the ordering for OrderedDict.
                # We can trust these to be strings as they
                # are taken from the Tensorflow graph.
                serialized_param_list = json.dumps(list(params.keys()),
                                                   indent=4)

            # Create a zip-archive and write our objects
            # there. This works when save_path
            # is either str or a file-like
            with zipfile.ZipFile(save_path, "w") as file_:
                # Do not try to save "None" elements
                if data is not None:
                    file_.writestr("data", serialized_data)
                if params is not None:
                    file_.writestr("parameters", serialized_params)
                    file_.writestr("parameter_list", serialized_param_list)