Skip to main content

A decorator for seamless numpy calculations from torch.tensor and pd.DataFrame.

Project description

CI CI

Installation

$ pip install numpy_fn

Usage

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Time-stamp: "2024-04-07 21:56:35 (ywatanabe)"

from numpy_fn import numpy_fn

import numpy as np
import pandas as pd
import scipy
import torch


@numpy_fn
def numpy_softmax(*args, **kwargs):
    return scipy.special.softmax(*args, **kwargs)


def custom_print(x):
    print(type(x), x)


# Test the decorator with different input types
x = [1, 2, 3]
x_list = x
x_array = np.array(x)
x_df = pd.DataFrame({"col1": x})
x_tensor = torch.tensor(x).float()
if torch.cuda.is_available():
    x_tensor_cuda = torch.tensor(x).float().cuda()

custom_print(numpy_softmax(x_list, axis=-1))
# <class 'numpy.ndarray'> [0.09003057 0.24472847 0.66524096]

custom_print(numpy_softmax(x_array, axis=-1))
# <class 'numpy.ndarray'> [0.09003057 0.24472847 0.66524096]

custom_print(numpy_softmax(x_df, axis=-1))
# <class 'numpy.ndarray'> [0.09003057 0.24472847 0.66524096]

custom_print(numpy_softmax(x_tensor, axis=-1))
# /home/ywatanabe/proj/numpy_fn/src/numpy_fn/_numpy_fn.py:57: UserWarning: Converted from  <class 'numpy.ndarray'> to <class 'torch.Tensor'> (cpu)
#   warnings.warn(
# <class 'torch.Tensor'> tensor([0.0900, 0.2447, 0.6652])

if torch.cuda.is_available():
    custom_print(numpy_softmax(x_tensor_cuda, axis=-1))
# /home/ywatanabe/proj/numpy_fn/src/numpy_fn/_numpy_fn.py:57: UserWarning: Converted from  <class 'numpy.ndarray'> to <class 'torch.Tensor'> (cuda:0)
#   warnings.warn(
# <class 'torch.Tensor'> tensor([0.0900, 0.2447, 0.6652], device='cuda:0')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numpy_fn-1.0.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

numpy_fn-1.0.0-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page