mirror of
https://github.com/wassname/pytorch-for-numpy-users.git
synced 2026-07-14 01:10:29 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# PyTorch for Numpy users
|
||||
|
||||
[PyTorch](https://github.com/pytorch/pytorch.git) version of [_Torch for Numpy users_](https://github.com/torch/torch7/wiki/Torch-for-Numpy-users).
|
||||
|
||||
|
||||
## Types
|
||||
|
||||
| Numpy | PyTorch |
|
||||
|:-------------|:---------------------|
|
||||
| `np.uint8` | `torch.ByteTensor` |
|
||||
| `np.int32` | `torch.IntTensor` |
|
||||
| `np.int64` | `torch.LongTensor` |
|
||||
| `np.float32` | `torch.FloatTensor` |
|
||||
| `np.float64` | `torch.DoubleTensor` |
|
||||
|
||||
## Constructors
|
||||
|
||||
| Numpy | PyTorch |
|
||||
|:-------------------|:---------------------------------------|
|
||||
| `np.empty((2, 3))` | `torch.Tensor(2, 3)` |
|
||||
| `np.empty_like(x)` | `x.new(x.size()).type(x.type())` |
|
||||
| `np.eye` | `torch.eye` |
|
||||
| `np.identity` | `torch.eye` |
|
||||
| `np.ones` | `torch.ones` |
|
||||
| `np.ones_like` | `torch.ones(x.size()).type(x.type())` |
|
||||
| `np.zeros` | `torch.zeros` |
|
||||
| `np.zeros_like` | `torch.zeros(x.size()).type(x.type())` |
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
types:
|
||||
- numpy: np.uint8
|
||||
pytorch: torch.ByteTensor
|
||||
- numpy: np.int32
|
||||
pytorch: torch.IntTensor
|
||||
- numpy: np.int64
|
||||
pytorch: torch.LongTensor
|
||||
- numpy: np.float32
|
||||
pytorch: torch.FloatTensor
|
||||
- numpy: np.float64
|
||||
pytorch: torch.DoubleTensor
|
||||
|
||||
constructors:
|
||||
- numpy: np.empty((2, 3))
|
||||
pytorch: torch.Tensor(2, 3)
|
||||
- numpy: np.empty_like(x)
|
||||
pytorch: x.new(x.size()).type(x.type())
|
||||
- numpy: np.eye
|
||||
pytorch: torch.eye
|
||||
- numpy: np.identity
|
||||
pytorch: torch.eye
|
||||
- numpy: np.ones
|
||||
pytorch: torch.ones
|
||||
- numpy: np.ones_like
|
||||
pytorch: torch.ones(x.size()).type(x.type())
|
||||
- numpy: np.zeros
|
||||
pytorch: torch.zeros
|
||||
- numpy: np.zeros_like
|
||||
pytorch: torch.zeros(x.size()).type(x.type())
|
||||
|
||||
# - numpy: x.astype(np.int32)
|
||||
# pytorch: x.type(torch.IntTensor)
|
||||
#
|
||||
# - numpy: y = x.copy()
|
||||
# pytorch: y = x.clone()
|
||||
#
|
||||
# - numpy: x.shape
|
||||
# pytorch: x.size()
|
||||
#
|
||||
# - numpy: x.size
|
||||
# pytorch: x.nelement()
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import collections
|
||||
import os.path as osp
|
||||
|
||||
import tabulate
|
||||
import yaml
|
||||
|
||||
|
||||
TEMPLATE = '''\
|
||||
# PyTorch for Numpy users
|
||||
|
||||
[PyTorch](https://github.com/pytorch/pytorch.git) version of [_Torch for Numpy users_](https://github.com/torch/torch7/wiki/Torch-for-Numpy-users).
|
||||
|
||||
{contents}
|
||||
'''
|
||||
|
||||
|
||||
here = osp.dirname(osp.abspath(__file__))
|
||||
|
||||
|
||||
def get_contents():
|
||||
# keep order in yaml file
|
||||
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
||||
lambda loader, node: \
|
||||
collections.OrderedDict(loader.construct_pairs(node)))
|
||||
|
||||
yaml_file = osp.join(here, 'conversions.yaml')
|
||||
data = yaml.load(open(yaml_file))
|
||||
contents = ''
|
||||
for section, data in data.items():
|
||||
headers = ['Numpy', 'PyTorch']
|
||||
rows = []
|
||||
for d in data:
|
||||
rows.append([
|
||||
'`' + d['numpy'] + '`',
|
||||
'`' + d['pytorch'] + '`',
|
||||
])
|
||||
contents += '''
|
||||
## {title}
|
||||
|
||||
{table}\n'''.format(
|
||||
title=section.capitalize(),
|
||||
table=tabulate.tabulate(rows, headers=headers, tablefmt='pipe'),
|
||||
)
|
||||
return contents
|
||||
|
||||
|
||||
print(TEMPLATE.format(contents=get_contents()))
|
||||
Reference in New Issue
Block a user