Skip to content

Commit

Permalink
feat: infer input size of forward and LSTM layers (#808)
Browse files Browse the repository at this point in the history
### Summary of Changes

* Users no longer need to specify input sizes of forward and LSTM
layers.
* The constructor parameter `output_size` of these two layers has been
renamed to `neuron_count`.
  • Loading branch information
lars-reimann authored May 29, 2024
1 parent 69a780c commit 098a07f
Show file tree
Hide file tree
Showing 26 changed files with 504 additions and 648 deletions.
32 changes: 16 additions & 16 deletions docs/tutorials/classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -55,8 +55,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -80,8 +80,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -98,8 +98,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -118,8 +118,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -139,8 +139,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -166,8 +166,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -189,8 +189,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from safeds.data.image.containers import ImageList\n",
"\n",
"images, filepaths = ImageList.from_files(\"data/shapes\", return_filenames=True)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -71,7 +71,6 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import re\n",
"from safeds.data.tabular.containers import Column\n",
Expand All @@ -85,7 +84,8 @@
"collapsed": false
},
"id": "66dcf95a3fa51f23",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -99,7 +99,6 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from safeds.data.labeled.containers import ImageDataset\n",
"\n",
Expand All @@ -109,7 +108,8 @@
"collapsed": false
},
"id": "32056ddf5396e070",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -131,7 +131,6 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from safeds.ml.nn.layers import (Convolutional2DLayer, FlattenLayer,\n",
" ForwardLayer, MaxPooling2DLayer)\n",
Expand All @@ -142,15 +141,16 @@
" Convolutional2DLayer(output_channel=32, kernel_size=3, padding=1),\n",
" MaxPooling2DLayer(kernel_size=2, stride=2),\n",
" FlattenLayer(),\n",
" ForwardLayer(output_size=128),\n",
" ForwardLayer(output_size=3),\n",
" ForwardLayer(neuron_count=128),\n",
" ForwardLayer(neuron_count=3),\n",
"]"
],
"metadata": {
"collapsed": false
},
"id": "806a8091249d533a",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -162,7 +162,6 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from safeds.ml.nn import NeuralNetworkClassifier\n",
"from safeds.ml.nn.converters import InputConversionImageToColumn\n",
Expand All @@ -176,7 +175,8 @@
"collapsed": false
},
"id": "af68cc0d32655d32",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -198,15 +198,15 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"cnn_fitted = cnn.fit(dataset, epoch_size=32, batch_size=16)"
],
"metadata": {
"collapsed": false
},
"id": "381627a94d500675",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -220,15 +220,15 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"prediction = cnn_fitted.predict(dataset.get_input())"
],
"metadata": {
"collapsed": false
},
"id": "62f63dd68362c8b7",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -242,15 +242,15 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"shuffled_prediction = prediction.shuffle()"
],
"metadata": {
"collapsed": false
},
"id": "779277d73e30554d",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -264,15 +264,15 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"shuffled_prediction.get_input().remove_image_by_index(list(range(9, len(prediction))))"
],
"metadata": {
"collapsed": false
},
"id": "a5ddbbfba41aa7f",
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -286,15 +286,15 @@
},
{
"cell_type": "code",
"outputs": [],
"source": [
"shuffled_prediction.get_output().to_list()[0:9]"
],
"metadata": {
"collapsed": false
},
"id": "7081595d7100fb42",
"execution_count": null
"execution_count": null,
"outputs": []
}
],
"metadata": {
Expand Down
28 changes: 14 additions & 14 deletions docs/tutorials/machine_learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -60,8 +60,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -87,8 +87,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -158,8 +158,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -199,8 +199,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -240,8 +240,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -281,8 +281,8 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down
8 changes: 4 additions & 4 deletions src/safeds/data/labeled/containers/_tabular_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def _into_dataloader_with_classes(self, batch_size: int, num_of_classes: int) ->
if num_of_classes <= 2:
return DataLoader(
dataset=_create_dataset(
torch.Tensor(self.features._data_frame.to_numpy()).to(_get_device()),
torch.Tensor(self.target._series.to_numpy()).to(_get_device()).unsqueeze(dim=-1),
self.features._data_frame.to_torch().to(_get_device()),
self.target._series.to_torch().to(_get_device()).unsqueeze(dim=-1),
),
batch_size=batch_size,
shuffle=True,
Expand All @@ -206,9 +206,9 @@ def _into_dataloader_with_classes(self, batch_size: int, num_of_classes: int) ->
else:
return DataLoader(
dataset=_create_dataset(
torch.Tensor(self.features._data_frame.to_numpy()).to(_get_device()),
self.features._data_frame.to_torch().to(_get_device()),
torch.nn.functional.one_hot(
torch.LongTensor(self.target._series.to_numpy()).to(_get_device()),
self.target._series.to_torch().to(_get_device()),
num_classes=num_of_classes,
),
),
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/transformation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
__name__,
{
"Discretizer": "._discretizer:Discretizer",
"InvertibleTableTransformer": "._table_transformer:InvertibleTableTransformer",
"InvertibleTableTransformer": "._invertible_table_transformer:InvertibleTableTransformer",
"LabelEncoder": "._label_encoder:LabelEncoder",
"OneHotEncoder": "._one_hot_encoder:OneHotEncoder",
"RangeScaler": "._range_scaler:RangeScaler",
Expand Down
2 changes: 2 additions & 0 deletions src/safeds/data/tabular/transformation/_discretizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ def __hash__(self) -> int:

@property
def is_fitted(self) -> bool:
"""Whether the transformer is fitted."""
return self._wrapped_transformer is not None

@property
def bin_count(self) -> int:
"""The number of bins to be created."""
return self._bin_count

# ------------------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 098a07f

Please sign in to comment.