MCPcopy Create free account
hub / github.com/pytorch/pytorch / rename

Method rename

torch/_tensor.py:1299–1339  ·  view source on GitHub ↗

Renames dimension names of :attr:`self`. There are two main usages: ``self.rename(**rename_map)`` returns a view on tensor that has dims renamed as specified in the mapping :attr:`rename_map`. ``self.rename(*names)`` returns a view on tensor, renaming all d

(self, *names, **rename_map)

Source from the content-addressed store, hash-verified

1297 return update_names(self, names, rename_map, inplace=True)
1298
1299 def rename(self, *names, **rename_map):
1300 """Renames dimension names of :attr:`self`.
1301
1302 There are two main usages:
1303
1304 ``self.rename(**rename_map)`` returns a view on tensor that has dims
1305 renamed as specified in the mapping :attr:`rename_map`.
1306
1307 ``self.rename(*names)`` returns a view on tensor, renaming all
1308 dimensions positionally using :attr:`names`.
1309 Use ``self.rename(None)`` to drop names on a tensor.
1310
1311 One cannot specify both positional args :attr:`names` and keyword args
1312 :attr:`rename_map`.
1313
1314 Examples::
1315
1316 >>> imgs = torch.rand(2, 3, 5, 7, names=('N', 'C', 'H', 'W'))
1317 >>> renamed_imgs = imgs.rename(N='batch', C='channels')
1318 >>> renamed_imgs.names
1319 ('batch', 'channels', 'H', 'W')
1320
1321 >>> renamed_imgs = imgs.rename(None)
1322 >>> renamed_imgs.names
1323 (None, None, None, None)
1324
1325 >>> renamed_imgs = imgs.rename('batch', 'channel', 'height', 'width')
1326 >>> renamed_imgs.names
1327 ('batch', 'channel', 'height', 'width')
1328
1329 .. warning::
1330 The named tensor API is experimental and subject to change.
1331
1332 """
1333 if has_torch_function_unary(self):
1334 return handle_torch_function(
1335 Tensor.rename, (self,), self, *names, **rename_map
1336 )
1337
1338 # See Note [rename_ / rename API]
1339 return update_names(self, names, rename_map, inplace=False)
1340
1341 def to_sparse_coo(self):
1342 """Convert a tensor to :ref:`coordinate format <sparse-coo-docs>`.

Callers 15

_tensor_strFunction · 0.45
_update_namesMethod · 0.45
exportFunction · 0.45
test_equalMethod · 0.45
check_reprMethod · 0.45
test_renameMethod · 0.45
test_rename_globberMethod · 0.45
test_flattenMethod · 0.45
test_masked_selectMethod · 0.45

Calls 2

handle_torch_functionFunction · 0.90
update_namesFunction · 0.90

Tested by 11

test_equalMethod · 0.36
check_reprMethod · 0.36
test_renameMethod · 0.36
test_rename_globberMethod · 0.36
test_flattenMethod · 0.36
test_masked_selectMethod · 0.36
test_masked_fillMethod · 0.36
handle_log_fileFunction · 0.36