-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
370 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
library: nnabla | ||
datasets: | ||
data: | ||
class: rectangle.data.Data | ||
n_splits: 4 | ||
dataset: | ||
fold: 0 | ||
model: | ||
class: rectangle.nnabla.Model | ||
hidden_sizes: [20, 30] | ||
optimizer: | ||
class: nnabla.solvers.Sgd | ||
lr: 1e-3 | ||
results: | ||
metrics: | ||
monitor: | ||
metric: val_loss | ||
early_stopping: | ||
patience: 10 | ||
trainer: | ||
loss: mse | ||
batch_size: 10 | ||
epochs: 10 | ||
shuffle: true | ||
verbose: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import nnabla as nn | ||
import nnabla.functions as F | ||
import nnabla.parametric_functions as PF | ||
|
||
import ivory.nnabla.model | ||
|
||
|
||
class Model(ivory.nnabla.model.Model): | ||
def __init__(self, hidden_sizes): | ||
super().__init__() | ||
self.hidden_sizes = hidden_sizes | ||
|
||
def forward(self, x): | ||
for k, hidden_size in enumerate(self.hidden_sizes): | ||
with nn.parameter_scope(f"layer{k}"): | ||
x = F.relu(PF.affine(x, hidden_size)) | ||
with nn.parameter_scope(f"layer{k+1}"): | ||
x = PF.affine(x, 1) | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ trainer: | |
loss: mse | ||
batch_size: 10 | ||
epochs: 10 | ||
shuffle: true | ||
verbose: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from dataclasses import dataclass | ||
|
||
from nnabla.utils.data_iterator import data_iterator_simple | ||
|
||
import ivory.core.data | ||
from ivory.core.data import Dataset | ||
|
||
|
||
@dataclass | ||
class DataLoader: | ||
dataset: Dataset | ||
batch_size: int | ||
shuffle: bool = False | ||
with_memory_cache: bool = False | ||
with_file_cache: bool = False | ||
|
||
def __post_init__(self): | ||
self.iterator = data_iterator_simple( | ||
self.load_func, | ||
len(self.dataset), | ||
self.batch_size, | ||
shuffle=self.shuffle, | ||
with_memory_cache=self.with_memory_cache, | ||
with_file_cache=self.with_file_cache, | ||
) | ||
|
||
def __len__(self): | ||
return len(self.dataset) // self.batch_size | ||
|
||
def load_func(self, index): | ||
return self.dataset[index] | ||
|
||
def __iter__(self): | ||
for _ in range(len(self)): | ||
yield next(self.iterator) | ||
|
||
|
||
class DataLoaders(ivory.core.data.DataLoaders): | ||
def get_dataloader(self, dataset, batch_size, shuffle): | ||
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import nnabla.functions as F | ||
|
||
|
||
def mse(input, target): | ||
return F.mean(F.squared_error(input, target)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ivory.callbacks.metrics | ||
from ivory.core.run import Run | ||
|
||
|
||
class Metrics(ivory.callbacks.metrics.BatchMetrics): | ||
def metrics_dict(self, run: Run): | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import nnabla as nn | ||
|
||
import ivory.core.collections | ||
|
||
|
||
class Model: | ||
NUM_MODELS = 0 | ||
|
||
def __init__(self): | ||
self.training = True | ||
self.scope = f"model{self.__class__.NUM_MODELS}" | ||
self.__class__.NUM_MODELS += 1 | ||
|
||
def train(self, mode: bool = True): | ||
self.training = mode | ||
|
||
def eval(self): | ||
self.train(False) | ||
|
||
def build(self, loss, dataset, batch_size): | ||
index, input, target = dataset[0] | ||
input_shape = [batch_size] + list(input.shape) | ||
target_shape = [batch_size] + list(target.shape) | ||
self.input = ivory.core.collections.Dict() | ||
self.output = ivory.core.collections.Dict() | ||
self.target = ivory.core.collections.Dict() | ||
self.loss = ivory.core.collections.Dict() | ||
for mode in ["train", "test"]: | ||
self.input[mode] = nn.Variable(input_shape) | ||
self.target[mode] = nn.Variable(target_shape) | ||
with nn.parameter_scope(self.scope): | ||
self.training = mode == "train" | ||
self.output[mode] = self.forward(self.input[mode]) | ||
if mode == "train": | ||
self.output[mode].persistent = True | ||
self.loss[mode] = loss(self.output[mode], self.target[mode]) | ||
self.training = True | ||
|
||
def parameters(self): | ||
with nn.parameter_scope(self.scope): | ||
return nn.get_parameters() | ||
|
||
def forward(self, input): | ||
raise NotImplementedError | ||
|
||
def __call__(self, input, target=None): | ||
if self.training: | ||
mode = "train" | ||
else: | ||
mode = "test" | ||
self.input[mode].data.data = input | ||
if target is not None: | ||
self.target[mode].data.data = target | ||
node = self.loss[mode] | ||
else: | ||
node = self.output[mode] | ||
if mode == "train": | ||
node.forward() # clear_no_need_grad=True) | ||
else: | ||
node.forward() # clear_buffer=True) | ||
output = self.output[mode].data.data.copy() | ||
if target is None: | ||
return output | ||
else: | ||
return output, self.loss[mode].data.data.copy() | ||
|
||
def backward(self): | ||
self.loss["train"].backward() # clear_buffer=True) |
Oops, something went wrong.