Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CSVSummary callback #173

Merged
merged 14 commits into from
Nov 20, 2020
17 changes: 15 additions & 2 deletions docs/usage/3_perform_fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
" ProgressBar,\n",
" TFSummary,\n",
" YAMLSummary,\n",
" CSVSummary,\n",
")\n",
"from tensorwaves.optimizer.minuit import Minuit2\n",
"\n",
Expand All @@ -235,6 +236,7 @@
" ProgressBar(),\n",
" TFSummary(),\n",
" YAMLSummary(\"current_fit_result.yaml\", estimator),\n",
" CSVSummary(\"current_fit_result.csv\", estimator),\n",
" ]\n",
" )\n",
")\n",
Expand All @@ -254,6 +256,17 @@
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df=pd.read_csv(\"current_fit_result.csv\")\n",
"df.plot(\"Iteration\", \"Estimator_Value\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -398,9 +411,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.6-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
43 changes: 43 additions & 0 deletions src/tensorwaves/optimizer/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import tensorflow as tf
import yaml
from pandas import DataFrame
redeboer marked this conversation as resolved.
Show resolved Hide resolved
from tqdm import tqdm

from tensorwaves.interfaces import Estimator
Expand Down Expand Up @@ -84,6 +85,48 @@ def finalize(self) -> None:
self.__stream.close()


class CSVSummary(Callback):
def __init__(
self,
filename: str,
estimator: Estimator,
step_size: int = 10,
) -> None:
"""Log fit parameters and the estimator value to a CSV file."""
self.__function_call = 0
self.__step_size = step_size
self.__stream = open(filename, "w")
_empty_file(self.__stream)
if not isinstance(estimator, Estimator):
raise TypeError(f"Requires an in {Estimator.__name__} instance")
self.__estimator_type: str = estimator.__class__.__name__

def __call__(self, parameters: dict, estimator_value: float) -> None:
self.__function_call += 1
if self.__function_call % self.__step_size != 0:
return
output_dict = {
"Time": datetime.now(),
"Iteration": self.__function_call,
"Estimator_Type": self.__estimator_type,
"Estimator_Value": float(estimator_value),
redeboer marked this conversation as resolved.
Show resolved Hide resolved
# name: float(value) for name, value in parameters.items()
redeboer marked this conversation as resolved.
Show resolved Hide resolved
}
for name, value in parameters.items():
redeboer marked this conversation as resolved.
Show resolved Hide resolved
output_dict[name] = float(value)

df = DataFrame(output_dict, index=[self.__function_call])
df.to_csv(
self.__stream,
mode="a",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
header=self.__function_call == 10,
redeboer marked this conversation as resolved.
Show resolved Hide resolved
index=False,
)

def finalize(self) -> None:
self.__stream.close()


class TFSummary(Callback):
def __init__(
self,
Expand Down