A decorator for seamless numpy calculations from torch.tensor and pd.DataFrame.
Project description
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
Release history Release notifications | RSS feed
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 details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numpy_fn-1.0.0.tar.gz.
File metadata
- Download URL: numpy_fn-1.0.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abfd4b6b118ccc4087b8fef3c40b85daab094081ee2818b69dd464df7230f44f
|
|
| MD5 |
adee53e9d47ba7a667639d9102b02836
|
|
| BLAKE2b-256 |
a96aa1e646a6dfe39b70570b64565430a43702b13d9ae1a04012f0b001565016
|
File details
Details for the file numpy_fn-1.0.0-py3-none-any.whl.
File metadata
- Download URL: numpy_fn-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ac08fed655529a1214248d4db19fe168333b2d7b27937e46874bfb19bbeaf72
|
|
| MD5 |
d3130657242a08ef1ab4314e436da9e1
|
|
| BLAKE2b-256 |
5fcea7462156327392f82a3e075cd055823e6daa1743a11d0922f5393aa59c25
|