Skip to content

Commit

Permalink
Merge pull request #42 from franneck94/dev
Browse files Browse the repository at this point in the history
Update to 0.2.0
  • Loading branch information
franneck94 authored Nov 28, 2020
2 parents bca1d46 + b437048 commit b3b0e67
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 161 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
build/
!docs/build/
.benchmarks/
logs/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
91 changes: 42 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ This python package aims to help with this use-case.
## API

- [GridSearch](#GridSearch-Example)
- [RandomSearch](#RandomSearch-Example)
- [GridSearchCV](#GridSearchCV-Example)
- For more examples see: [here](examples/)

### GridSearch Example
### Dataset and TensorFlow Model for the Examples

```python
import tensorflow as tf
from tensorcross.model_selection GridSearch

data = tf.data.Dataset()
dataset = tf.data.Dataset.from_tensor_slices(
(np.array([1, 2, 3]).reshape(-1, 1), # x
np.array([-1, -2, -3]).reshape(-1, 1)) # y
)

def build_model(
optimizer: tf.keras.optimizers.Optimizer,
Expand All @@ -51,6 +53,24 @@ This python package aims to help with this use-case.
)

return model
```

The dataset must be a tf.data.Dataset object and you have to define a
function/callable that returns a compiled tf.keras.models.Model object.
This object will then be trained in e.g. the GridSearch.

### GridSearch Example

Assuming you have a tf.data.Dataset object and a build_model function,
defined as above. You can run a GridSearch as below:

```python
from tensorcross.model_selection GridSearch

train_dataset, val_dataset = dataset_split(
dataset=dataset,
split_fraction=(1 / 3)
)

param_grid = {
"optimizer": [
Expand All @@ -69,72 +89,45 @@ This python package aims to help with this use-case.
)

grid_search.fit(
train_dataset=data.train_dataset,
val_dataset=data.val_dataset,
train_dataset=train_dataset,
val_dataset=val_dataset,
epochs=1,
verbose=1
)

grid_search.summary()
```

### RandomSearch Example

```python
import tensorflow as tf
from tensorcross.model_selection RandomSearch

data = tf.data.Dataset()

def build_model(
optimizer: tf.keras.optimizers.Optimizer,
learning_rate: float
) -> tf.keras.models.Model:
x_input = tf.keras.layers.Input(shape=2)
y_pred = tf.keras.layers.Dense(units=1)(x_input)
model = tf.keras.models.Model(inputs=[x_input], outputs=[y_pred])

opt = optimizer(learning_rate=learning_rate)
### GridSearchCV Example

model.compile(
loss="mse", optimizer=opt, metrics=["mse"]
)
Assuming you have a tf.data.Dataset object and a build_model function,
defined as above. You can run a GridSearchCV as below:

return model
```python
from tensorcross.model_selection GridSearchCV

param_distributions = {
param_grid = {
"optimizer": [
tf.keras.optimizers.Adam,
tf.keras.optimizers.RMSprop
],
"learning_rate": uniform(0.001, 0.0001)
"learning_rate": [0.001, 0.0001]
}

rand_search = RandomSearch(
grid_search_cv = GridSearchCV(
model_fn=build_model,
param_distributions=param_distributions,
n_iter=2,
param_grid=param_grid,
n_folds=2,
verbose=1,
num_features=num_features,
num_targets=num_targets
num_features=1,
num_targets=1
)

rand_search.fit(
train_dataset=train_dataset,
val_dataset=val_dataset,
epochs=3,
grid_search_cv.fit(
train_dataset=dataset,
epochs=1,
verbose=1
)

rand_search.summary()
grid_search_cv.summary()
```

## Issues

If you want to open an issue, it must be a bug, a feature request, or a significant problem with the documentation.
For more information see [here](.github/ISSUE_TEMPLATE/).

## Pull Requests

You are free to make contributions to the repository.
For more information see [here](.github/PULL_REQUEST_TEMPLATE/).
38 changes: 12 additions & 26 deletions examples/example_grid_search.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split

from tensorcross.model_selection import GridSearch
from tensorcross.utils import dataset_split


np.random.seed(0)
tf.random.set_seed(0)


def f(x: np.ndarray) -> np.ndarray:
return 2 * x + 1


class DATA:
def __init__(
self,
test_size: float = 0.2
) -> None:
x = np.random.uniform(low=-10.0, high=10.0, size=100)
y = f(x) + np.random.normal(size=100)
x_train, x_val, y_train, y_val = train_test_split(
x, y, test_size=test_size
)
self.train_dataset = tf.data.Dataset.from_tensor_slices(
(x_train.reshape(-1, 1), y_train.reshape(-1, 1))
)
self.val_dataset = tf.data.Dataset.from_tensor_slices(
(x_val.reshape(-1, 1), y_val.reshape(-1, 1))
)


def build_model(
num_features: int,
num_targets: int,
Expand All @@ -53,7 +31,15 @@ def build_model(


if __name__ == "__main__":
data = DATA()
dataset = tf.data.Dataset.from_tensor_slices(
(np.array([1, 2, 3]).reshape(-1, 1), # x
np.array([-1, -2, -3]).reshape(-1, 1)) # y
)

train_dataset, val_dataset = dataset_split(
dataset=dataset,
split_fraction=(1 / 3)
)

param_grid = {
"optimizer": [
Expand All @@ -72,8 +58,8 @@ def build_model(
)

grid_search.fit(
train_dataset=data.train_dataset,
val_dataset=data.val_dataset,
train_dataset=train_dataset,
val_dataset=val_dataset,
epochs=1,
verbose=1
)
Expand Down
21 changes: 5 additions & 16 deletions examples/example_grid_search_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@
tf.random.set_seed(0)


def f(x: np.ndarray) -> np.ndarray:
return 2 * x + 1


class DATA:
def __init__(self) -> None:
x = np.random.uniform(low=-10.0, high=10.0, size=100)
y = f(x) + np.random.normal(size=100)
self.train_dataset = tf.data.Dataset.from_tensor_slices(
(x.reshape(-1, 1), y.reshape(-1, 1))
)


def build_model(
num_features: int,
num_targets: int,
Expand All @@ -43,8 +30,10 @@ def build_model(


if __name__ == "__main__":
data = DATA()
train_dataset = data.train_dataset
dataset = tf.data.Dataset.from_tensor_slices(
(np.array([1, 2, 3]).reshape(-1, 1), # x
np.array([-1, -2, -3]).reshape(-1, 1)) # y
)

param_grid = {
"optimizer": [
Expand All @@ -64,7 +53,7 @@ def build_model(
)

grid_search_cv.fit(
train_dataset=train_dataset,
train_dataset=dataset,
epochs=1,
verbose=1
)
Expand Down
35 changes: 9 additions & 26 deletions examples/example_random_search.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
import numpy as np
import tensorflow as tf
from scipy.stats import uniform
from sklearn.model_selection import train_test_split

from tensorcross.model_selection import RandomSearch
from tensorcross.utils import dataset_split


np.random.seed(0)
tf.random.set_seed(0)


def f(x: np.ndarray) -> np.ndarray:
return 2 * x + 1


class DATA:
def __init__(
self,
test_size: float = 0.2
) -> None:
x = np.random.uniform(low=-10.0, high=10.0, size=100)
y = f(x) + np.random.normal(size=100)
x_train, x_val, y_train, y_val = train_test_split(
x, y, test_size=test_size
)
self.train_dataset = tf.data.Dataset.from_tensor_slices(
(x_train.reshape(-1, 1), y_train.reshape(-1, 1))
)
self.val_dataset = tf.data.Dataset.from_tensor_slices(
(x_val.reshape(-1, 1), y_val.reshape(-1, 1))
)


def build_model(
num_features: int,
num_targets: int,
Expand All @@ -54,10 +32,15 @@ def build_model(


if __name__ == "__main__":
data = DATA()
dataset = tf.data.Dataset.from_tensor_slices(
(np.array([1, 2, 3]).reshape(-1, 1), # x
np.array([-1, -2, -3]).reshape(-1, 1)) # y
)

train_dataset = data.train_dataset
val_dataset = data.val_dataset
train_dataset, val_dataset = dataset_split(
dataset=dataset,
split_fraction=(1 / 3)
)

param_distributions = {
"optimizer": [
Expand Down
21 changes: 5 additions & 16 deletions examples/example_random_search_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@
tf.random.set_seed(0)


def f(x: np.ndarray) -> np.ndarray:
return 2 * x + 1


class DATA:
def __init__(self) -> None:
x = np.random.uniform(low=-10.0, high=10.0, size=100)
y = f(x) + np.random.normal(size=100)
self.train_dataset = tf.data.Dataset.from_tensor_slices(
(x.reshape(-1, 1), y.reshape(-1, 1))
)


def build_model(
num_features: int,
num_targets: int,
Expand All @@ -44,8 +31,10 @@ def build_model(


if __name__ == "__main__":
data = DATA()
train_dataset = data.train_dataset
dataset = tf.data.Dataset.from_tensor_slices(
(np.array([1, 2, 3]).reshape(-1, 1), # x
np.array([-1, -2, -3]).reshape(-1, 1)) # y
)

param_distributions = {
"optimizer": [
Expand All @@ -66,7 +55,7 @@ def build_model(
)

rand_search_cv.fit(
train_dataset=train_dataset,
train_dataset=dataset,
epochs=1,
verbose=1
)
Expand Down
Loading

0 comments on commit b3b0e67

Please sign in to comment.