Skip to content

Instantly share code, notes, and snippets.

View Geson-anko's full-sized avatar

GesonAnko Geson-anko

View GitHub Profile
@Geson-anko
Geson-anko / reconstructable.py
Created March 25, 2024 04:54
新しいインスタンスを生成可能なクラス。MixInとして使っても良い。
import copy
from typing import Any, Self
class Reconstructable:
_init_args: tuple[Any, ...]
_init_kwds: dict[str, Any]
@classmethod
def reconstructable_init(cls, *args: Any, **kwds: Any) -> Self:
@Geson-anko
Geson-anko / module_with_device.py
Created December 20, 2023 08:18
デバイス情報を取得できる `nn.Module`
import torch
import torch.nn as nn
import torch.backends.mps
class ModuleWithDevice(nn.Module):
def __init__(self, *args, default_device=torch.device("cpu"), **kwargs) -> None:
super().__init__(*args, **kwargs)
# If this module has no parameters, returns this value.
self._default_device = default_device
@Geson-anko
Geson-anko / infer_by_train.py
Created December 16, 2023 08:30
学習と推論を非同期に行う処理のモックアップです。
import copy
import threading
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset
@Geson-anko
Geson-anko / graph.md
Last active November 22, 2023 12:20
マルチスレッド上でオブジェクトを共有する際の模擬実装を行いました。

流れはこんな感じです。

graph TD
    A["Create Shared Object Pool"] --> B["Instantiate Threads and Share."]
    B --> C["Start Interaction Thread"]
    B --> D["Start Training Thread"]
    B --> E["Start Main Thread"]