Skip to content

Commit

Permalink
Merge pull request #2 from godatadriven/dev
Browse files Browse the repository at this point in the history
Merge dev into master
  • Loading branch information
JasperHG90 authored Jan 5, 2021
2 parents f2f6fe0 + f136bd7 commit 3cc0840
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ This is an implementation of the model described in the following paper:
I have copied some of the code from the [paper's code base](https://github.com/elisim/piven), and cite the author's paper where this is the case.

<figure>
<img src="https://github.com/elisim/piven/blob/master/piven_architecture.jpg" height=250 width=800>
<figcaption>
<i>NN with piven layer, from from Simhayev, Gilad and Rokach (2020).</i>
</figcaption>
</figure>

## In short

A neural network with a Piven (Prediction Intervals with specific value prediction) output layer returns a point
Expand Down Expand Up @@ -69,14 +76,14 @@ This will save the metrics, keras model, and model predictions to the folder.

For additional examples, see the 'tests' and 'notebooks' folders.

## Creating your own Piven model

You can use a Piven layer on any neural network architecture. REF REPO AUTHORS example convo network.
## Creating your own model with Piven layer

This library uses the KerasRegressor wrapper from the tensorflow library to make scikit-compatible
keras models.
You can use a Piven layer on any neural network architecture. The authors of the Piven paper use it on top of
a bunch of [CNN layers](https://github.com/elisim/piven/blob/master/imdb/main.py) to predict people's age.

Suppose that you want to create an Model with a Piven output layer. You would first specify a build
Suppose that you want to create an Model with a Piven output layer. Because this module uses the
[KerasRegressor](https://www.tensorflow.org/api_docs/python/tf/keras/wrappers/scikit_learn/KerasRegressor) wrapper
from the tensorflow library to make scikit-compatible keras models, you would first specify a build
function like so:

```python
Expand Down Expand Up @@ -120,7 +127,7 @@ class MyPivenModel(PivenBaseModel):

@classmethod
def load(cls, path: str):
Model_config = MyPivenModel.load_Model_config(path)
model_config = MyPivenModel.load_model_config(path)
model = MyPivenModel.load_model_from_disk(build_fn=piven_model, path)
run = cls(**Model_config)
run.model = model
Expand All @@ -141,5 +148,3 @@ Note that the inputs to `MyPivenModel` must match the inputs to the `piven_model
You can now call all methods defined as in the PivenBaseModel class. Check the
[PivenMlpModel class](https://gitlab.com/jasperginn/piven.py/-/blob/dev/src/piven/Models/mlp_regressor.py)
for a more detailed example.


44 changes: 39 additions & 5 deletions notebooks/year_prediction_msd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -18,7 +18,7 @@
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from piven.experiments import PivenMlpExperiment\n",
"from piven.models import PivenMlpModel\n",
"import tensorflow as tf\n",
"from sklearn.compose import TransformedTargetRegressor\n",
"\n",
Expand All @@ -27,7 +27,41 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"records = []\n",
"with dpath.open(\"r\") as infile:\n",
" for line in infile.readlines():\n",
" line_split = line.replace(\"\\n\", \"\").split(\",\")\n",
" year = int(line_split[0])\n",
" inputs = [*map(lambda x: float(x), line_split[1:])]\n",
" records.append([year] + inputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"records = []\n",
"with dpath.open(\"r\") as infile:\n",
" for line in infile.readlines():\n",
" line_split = line.replace(\"\\n\", \"\").split(\",\")\n",
" year = int(line_split[0])\n",
" inputs = [*map(lambda x: float(x), line_split[1:])]\n",
" records.append([year] + inputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1620,9 +1654,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "PyCharm (pivenregressor)",
"language": "python",
"name": "python3"
"name": "pycharm-b9238b0d"
},
"language_info": {
"codemirror_mode": {
Expand Down
2 changes: 1 addition & 1 deletion src/piven/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def score(
}

@staticmethod
def load_experiment_config(path: str):
def load_model_config(path: str):
if not (Path(path) / "experiment_params.json").is_file():
raise FileNotFoundError(f"No experiment file found in {path}.")
with (Path(path) / "experiment_params.json").open("r") as infile:
Expand Down
4 changes: 2 additions & 2 deletions src/piven/models/mlp_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def build_model(self, preprocess: Union[None, Pipeline, TransformerMixin] = None

@classmethod
def load(cls, path: str):
experiment_config = PivenMlpModel.load_experiment_config(path)
model_config = PivenMlpModel.load_model_config(path)
model = PivenMlpModel.load_model_from_disk(piven_model, path)
run = cls(**experiment_config)
run = cls(**model_config)
run.model = model
return run

0 comments on commit 3cc0840

Please sign in to comment.