Skip to content

Instantly share code, notes, and snippets.

View altescy's full-sized avatar
👋

Yasuhiro Yamaguchi altescy

👋
View GitHub Profile
@altescy
altescy / rsm.py
Last active October 6, 2020 14:54
scikit-learn API Implementation of Replicated Softmax (RSM)
# -*- coding: utf-8 -*-
import time
import numpy as np
from scipy.special import expit
from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
from sklearn.utils import check_array
from sklearn.utils import check_random_state
from sklearn.utils import gen_even_slices
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@altescy
altescy / subcommand.py
Last active October 15, 2021 13:39
Example of subcommand with argparse
import argparse
import re
from collections import defaultdict
from typing import (Callable, ClassVar, Dict, NamedTuple, Optional, Type,
TypeVar, Union, cast)
Subclass = TypeVar("Subclass", bound="Subcommand")
Registry = Dict[Type["Subcommand"], Dict[str, Type["Subcommand"]]]
import fcntl
import hashlib
import inspect
import io
import logging
import pickle
import string
import time
from contextlib import contextmanager
from functools import wraps
import threading
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor, wait
from typing import (Any, Dict, Generic, Hashable, ItemsView, Iterator,
Optional, TypeVar, Union, overload)
KT = TypeVar("KT", bound=Hashable)
VT = TypeVar("VT")
VT_co = TypeVar("VT_co")
import os
import re
import sys
import unicodedata
from typing import Any, ClassVar, Dict, List, Optional, TextIO, Union
CSI = "\x1b"
REGEX_ANSI_CSI = re.compile(rf"{CSI}\[[0-9]+[a-zA-Z]")
@altescy
altescy / dataclassutil.py
Last active November 12, 2021 03:14
Construct dataclass object from dict recursively
import copy
import dataclasses
import json
import typing
from collections.abc import Sized
from typing import Any, Dict, Optional, Type, TypeVar, Union
T = TypeVar("T")
from typing import Any, Dict
def flatten(data: Any, parent_key: str = "") -> Any:
if isinstance(data, list):
data = {str(i): x for i, x in enumerate(data)}
if not isinstance(data, dict):
return data
from enum import Enum
from typing import Optional, TypeVar, Union
T = TypeVar("T")
CSI = "\x1b["
class AnsiCodes(Enum):
def __str__(self) -> str:
import os
import sys
import threading
import time
from collections import abc
from types import TracebackType
from typing import (Any, AsyncIterable, AsyncIterator, Callable, Dict,
Iterable, Iterator, List, Optional, TextIO, Type, TypeVar,
Union)