Skip to content

Commit

Permalink
fix: fixed devices with new polars implementation (#756)
Browse files Browse the repository at this point in the history
### Summary of Changes

fix: fixed devices with new polars implementation
refactor: using `_utils._get_random_seed` in `ImageList.from_files` to
shuffle all files

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
Marsmaennchen221 and megalinter-bot authored May 13, 2024
1 parent a7d92ae commit e72339e
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/safeds/data/image/containers/_image_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import TYPE_CHECKING, Literal, overload

from safeds._config import _init_default_device
from safeds._utils import _get_random_seed
from safeds.data.image.containers._image import Image
from safeds.exceptions import OutOfBoundsError, ClosedBound

Expand Down Expand Up @@ -176,6 +177,8 @@ def from_files(

_init_default_device()

random.seed(_get_random_seed())

from safeds.data.image.containers._empty_image_list import _EmptyImageList
from safeds.data.image.containers._multi_size_image_list import _MultiSizeImageList
from safeds.data.image.containers._single_size_image_list import _SingleSizeImageList
Expand Down
8 changes: 4 additions & 4 deletions src/safeds/data/labeled/containers/_image_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import warnings
from typing import TYPE_CHECKING, Generic, TypeVar

from safeds._config import _init_default_device
from safeds._config import _get_device, _init_default_device
from safeds._utils import _structural_hash
from safeds.data.image.containers import ImageList
from safeds.data.image.containers._empty_image_list import _EmptyImageList
Expand Down Expand Up @@ -294,7 +294,7 @@ def __init__(self, table: Table) -> None:
_init_default_device()

self._column_names = table.column_names
self._tensor = torch.Tensor(table._data_frame.to_numpy()).to(torch.get_default_device())
self._tensor = torch.Tensor(table._data_frame.to_torch()).to(_get_device())

if not torch.all(self._tensor.sum(dim=1) == torch.ones(self._tensor.size(dim=0))):
raise ValueError(
Expand Down Expand Up @@ -355,8 +355,8 @@ def __init__(self, column: Column) -> None:
category=UserWarning,
)
self._one_hot_encoder = OneHotEncoder().fit(column_as_table, [self._column_name])
self._tensor = torch.Tensor(self._one_hot_encoder.transform(column_as_table)._data_frame.to_numpy()).to(
torch.get_default_device(),
self._tensor = torch.Tensor(self._one_hot_encoder.transform(column_as_table)._data_frame.to_torch()).to(
_get_device(),
)

def __eq__(self, other: object) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions src/safeds/data/labeled/containers/_time_series_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from typing import TYPE_CHECKING, Any

from safeds._config import _init_default_device
from safeds._config import _get_device, _init_default_device
from safeds._utils import _structural_hash
from safeds.exceptions import ClosedBound, OutOfBoundsError

Expand Down Expand Up @@ -235,7 +235,7 @@ def _into_dataloader_with_window(self, window_size: int, forecast_horizon: int,
label = target_tensor[i + window_size + forecast_horizon]
for col in feature_cols:
data = torch.tensor(col._series.to_numpy(), dtype=torch.float32)
window = torch.cat((window, data[i: i + window_size]), dim=0)
window = torch.cat((window, data[i : i + window_size]), dim=0)
x_s.append(window)
y_s.append(label)
x_s_tensor = torch.stack(x_s)
Expand Down Expand Up @@ -279,7 +279,7 @@ def _into_dataloader_with_window_predict(

_init_default_device()

target_tensor = self.target._series.to_torch()
target_tensor = self.target._series.to_torch().to(_get_device())
x_s = []

size = target_tensor.size(0)
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,7 @@ def _into_dataloader(self, batch_size: int) -> DataLoader:
_init_default_device()

return DataLoader(
dataset=_create_dataset(self._data_frame.to_torch(dtype=pl.Float32)),
dataset=_create_dataset(self._data_frame.to_torch(dtype=pl.Float32).to(_get_device())),
batch_size=batch_size,
generator=torch.Generator(device=_get_device()),
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions tests/safeds/ml/nn/test_cnn_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ class TestImageToTableClassifier:
(
1234,
device_cuda,
["grayscale"] * 7,
["white_square"] * 7,
),
(
4711,
device_cuda,
["white_square"] * 7,
["rgba"] * 7,
),
(
1234,
device_cpu,
["grayscale"] * 7,
["white_square"] * 7,
),
(
4711,
device_cpu,
["white_square"] * 7,
["rgba"] * 7,
),
],
ids=["seed-1234-cuda", "seed-4711-cuda", "seed-1234-cpu", "seed-4711-cpu"],
Expand Down Expand Up @@ -106,22 +106,22 @@ class TestImageToColumnClassifier:
(
1234,
device_cuda,
["grayscale"] * 7,
["white_square"] * 7,
),
(
4711,
device_cuda,
["white_square"] * 7,
["rgba"] * 7,
),
(
1234,
device_cpu,
["grayscale"] * 7,
["white_square"] * 7,
),
(
4711,
device_cpu,
["white_square"] * 7,
["rgba"] * 7,
),
],
ids=["seed-1234-cuda", "seed-4711-cuda", "seed-1234-cpu", "seed-4711-cpu"],
Expand Down

0 comments on commit e72339e

Please sign in to comment.