Created
September 28, 2022 19:42
-
-
Save alisterburt/95c3cdc7e1f12b0d90cc55153862ef95 to your computer and use it in GitHub Desktop.
assignment into repeated indices in pytorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "681fa071-4f0d-4a81-888e-dbee02597ec7", | |
| "metadata": {}, | |
| "source": [ | |
| "# figure out tensor index operations" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "4322b571-0baa-46a4-8cc6-1d859eedbfd7", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import torch" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "a7fdefee-194a-4bee-a666-c5858507a935", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(tensor([0, 1, 2, 3, 4]), torch.Size([5]))" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tensor = torch.arange(5)\n", | |
| "tensor, tensor.shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "3f335cde-d43c-4eb4-b92c-79b08b5f4306", | |
| "metadata": { | |
| "tags": [] | |
| }, | |
| "source": [ | |
| "problem being solved...\n", | |
| "\n", | |
| "multiple addition into same index with fancy indexing doesn't work as expected" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "106e71df-b458-4002-9f01-c93982802599", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tensor([3, 1, 2, 3, 4])" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tensor[[0, 0]] += torch.Tensor([5, 3]).long()\n", | |
| "tensor" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "79a8ea6a-46c8-42de-b371-ee2e9dd8a5ac", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tensor([8, 1, 2, 3, 4])" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tensor = torch.arange(5)\n", | |
| "tensor.index_add_(dim=0, index=torch.Tensor([0, 0]).int(), source=torch.Tensor([5, 3]).long())" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "3a276bdc-871f-42e6-a685-0c2227d3f848", | |
| "metadata": {}, | |
| "source": [ | |
| "the above doesn't support multidimensional indices\n", | |
| "\n", | |
| "c.f. https://discuss.pytorch.org/t/index-adding-over-multiple-dimensions/124709/6\n", | |
| "\n", | |
| "use `index_put` instead" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "bdb52925-90e0-4ce8-834f-a2468bfe0ebd", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tensor([8, 1, 2, 3, 4])" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "tensor = torch.arange(5)\n", | |
| "tensor.index_put_((torch.Tensor([0, 0]).long(), ), torch.Tensor([5, 3]).long(), accumulate=True)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.10.5" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment