{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Tce3stUlHN0L" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2022-12-14T21:53:14.838083Z", "iopub.status.busy": "2022-12-14T21:53:14.837855Z", "iopub.status.idle": "2022-12-14T21:53:14.842046Z", "shell.execute_reply": "2022-12-14T21:53:14.841465Z" }, "id": "tuOe1ymfHZPu" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "VZ-KA8k5kybx" }, "source": [ "# 텐서 슬라이싱 소개" ] }, { "cell_type": "markdown", "metadata": { "id": "MfBg1C5NB3X0" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
TensorFlow.org에서 보기Google Colab에서 실행GitHub에서 소스 보기노트북 다운로드
" ] }, { "cell_type": "markdown", "metadata": { "id": "AixIdVeRk3CO" }, "source": [ "객체 감지 및 NLP와 같은 ML 애플리케이션에서 작업할 때 텐서의 하위 섹션(슬라이스)으로 작업해야 하는 경우가 있습니다. 예를 들어, 모델 아키텍처에 라우팅이 포함된 경우 한 계층이 다음 계층으로 라우팅되는 교육 예제를 제어할 수 있습니다. 이 경우 텐서 슬라이싱 작업을 사용하여 텐서를 분할하고 올바른 순서로 다시 결합할 수 있습니다.\n", "\n", "NLP 애플리케이션에서 텐서 슬라이싱을 사용하여 훈련하는 동안 워드 마스킹을 수행할 수 있습니다. 예를 들어 각 문장에서 마스킹할 단어 인덱스를 선택하고 해당 단어를 레이블로 선택한 다음 선택한 단어를 마스크 토큰으로 교체하여 문장 목록에서 훈련 데이터를 생성할 수 있습니다.\n", "\n", "이 가이드에서는 TensorFlow API를 사용하여 다음을 수행하는 방법을 배웁니다.\n", "\n", "- 텐서에서 조각 추출\n", "- 텐서의 특정 인덱스에 데이터 삽입\n", "\n", "이 가이드는 텐서 인덱싱에 익숙하다고 가정합니다. 이 가이드를 시작하기 전에 [Tensor](https://www.tensorflow.org/guide/tensor#indexing) 및 [TensorFlow NumPy](https://www.tensorflow.org/guide/tf_numpy#indexing) 가이드의 인덱싱 섹션을 읽으십시오." ] }, { "cell_type": "markdown", "metadata": { "id": "FcWhWYn7eXkF" }, "source": [ "## 설정\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:14.845737Z", "iopub.status.busy": "2022-12-14T21:53:14.845181Z", "iopub.status.idle": "2022-12-14T21:53:16.734595Z", "shell.execute_reply": "2022-12-14T21:53:16.733898Z" }, "id": "m6uvewqi0jso" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-12-14 21:53:15.775691: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory\n", "2022-12-14 21:53:15.775783: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory\n", "2022-12-14 21:53:15.775793: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n" ] } ], "source": [ "import tensorflow as tf\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "id": "K-muS4ej5zoN" }, "source": [ "## 텐서 조각 추출\n", "\n", "`tf.slice` 사용하여 NumPy와 같은 텐서 슬라이싱을 수행합니다.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:16.739040Z", "iopub.status.busy": "2022-12-14T21:53:16.738273Z", "iopub.status.idle": "2022-12-14T21:53:20.119154Z", "shell.execute_reply": "2022-12-14T21:53:20.118440Z" }, "id": "wZep0cjs0Oai" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([1 2 3], shape=(3,), dtype=int32)\n" ] } ], "source": [ "t1 = tf.constant([0, 1, 2, 3, 4, 5, 6, 7])\n", "\n", "print(tf.slice(t1,\n", " begin=[1],\n", " size=[3]))" ] }, { "cell_type": "markdown", "metadata": { "id": "Vh3xI3j0DRJ2" }, "source": [ "또는 더 Pythonic 구문을 사용할 수 있습니다. 텐서 슬라이스는 시작-정지 범위에 걸쳐 균등하게 간격을 두고 있습니다." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.122542Z", "iopub.status.busy": "2022-12-14T21:53:20.122294Z", "iopub.status.idle": "2022-12-14T21:53:20.128042Z", "shell.execute_reply": "2022-12-14T21:53:20.127448Z" }, "id": "P1MtEyKuWuDD" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([1 2 3], shape=(3,), dtype=int32)\n" ] } ], "source": [ "print(t1[1:4])" ] }, { "cell_type": "markdown", "metadata": { "id": "cjq1o8D2wKKs" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.131477Z", "iopub.status.busy": "2022-12-14T21:53:20.131018Z", "iopub.status.idle": "2022-12-14T21:53:20.135980Z", "shell.execute_reply": "2022-12-14T21:53:20.135431Z" }, "id": "UunuLTIuwDA-" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([5 6 7], shape=(3,), dtype=int32)\n" ] } ], "source": [ "print(t1[-3:])" ] }, { "cell_type": "markdown", "metadata": { "id": "EHvRB-XTwRTd" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "SW1zFFTnUpCQ" }, "source": [ "2차원 텐서의 경우 다음과 같이 사용할 수 있습니다." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.139098Z", "iopub.status.busy": "2022-12-14T21:53:20.138636Z", "iopub.status.idle": "2022-12-14T21:53:20.144536Z", "shell.execute_reply": "2022-12-14T21:53:20.143941Z" }, "id": "kThZhmpAVAQw" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[ 1 2]\n", " [ 6 7]\n", " [11 12]], shape=(3, 2), dtype=int32)\n" ] } ], "source": [ "t2 = tf.constant([[0, 1, 2, 3, 4],\n", " [5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14],\n", " [15, 16, 17, 18, 19]])\n", "\n", "print(t2[:-1, 1:3])" ] }, { "cell_type": "markdown", "metadata": { "id": "xA5Xt4OdVUui" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "iJPggqsH15fI" }, "source": [ "고차원 텐서에서도 `tf.slice` 를 사용할 수 있습니다." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.147681Z", "iopub.status.busy": "2022-12-14T21:53:20.147254Z", "iopub.status.idle": "2022-12-14T21:53:20.152155Z", "shell.execute_reply": "2022-12-14T21:53:20.151585Z" }, "id": "Re5eX1OXnKOZ" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([[[25 27]]], shape=(1, 1, 2), dtype=int32)\n" ] } ], "source": [ "t3 = tf.constant([[[1, 3, 5, 7],\n", " [9, 11, 13, 15]],\n", " [[17, 19, 21, 23],\n", " [25, 27, 29, 31]]\n", " ])\n", "\n", "print(tf.slice(t3,\n", " begin=[1, 1, 0],\n", " size=[1, 1, 2]))" ] }, { "cell_type": "markdown", "metadata": { "id": "x-O5FNV9qOJK" }, "source": [ "`tf.strided_slice` '하여 텐서 조각을 추출할 수도 있습니다." ] }, { "cell_type": "markdown", "metadata": { "id": "b9FhvrOnJsJb" }, "source": [ "`tf.gather` 를 사용하여 텐서의 단일 축에서 특정 인덱스를 추출합니다." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.155305Z", "iopub.status.busy": "2022-12-14T21:53:20.154842Z", "iopub.status.idle": "2022-12-14T21:53:20.167764Z", "shell.execute_reply": "2022-12-14T21:53:20.167208Z" }, "id": "TwviZrrIj2h7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([0 3 6], shape=(3,), dtype=int32)\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(tf.gather(t1,\n", " indices=[0, 3, 6]))\n", "\n", "# This is similar to doing\n", "\n", "t1[::3]" ] }, { "cell_type": "markdown", "metadata": { "id": "oKyjGi2zyzEC" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "obrjeKy1WfTN" }, "source": [ "`tf.gather` 는 인덱스 간격이 균일할 필요가 없습니다." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.171078Z", "iopub.status.busy": "2022-12-14T21:53:20.170493Z", "iopub.status.idle": "2022-12-14T21:53:20.176491Z", "shell.execute_reply": "2022-12-14T21:53:20.175909Z" }, "id": "LjJcwcZ0druw" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([b'c' b'a' b't' b's'], shape=(4,), dtype=string)\n" ] } ], "source": [ "alphabet = tf.constant(list('abcdefghijklmnopqrstuvwxyz'))\n", "\n", "print(tf.gather(alphabet,\n", " indices=[2, 0, 19, 18]))" ] }, { "cell_type": "markdown", "metadata": { "id": "mSHmUXIyeaJG" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "XsxMx49SOaVu" }, "source": [ "텐서의 여러 축에서 슬라이스를 추출하려면 `tf.gather_nd` 사용하십시오. 이는 행이나 열이 아닌 행렬의 요소를 수집하려는 경우에 유용합니다." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.179310Z", "iopub.status.busy": "2022-12-14T21:53:20.179066Z", "iopub.status.idle": "2022-12-14T21:53:20.184761Z", "shell.execute_reply": "2022-12-14T21:53:20.184099Z" }, "id": "mT52NFWVdiTe" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[2 7]\n", " [3 8]\n", " [0 5]], shape=(3, 2), dtype=int32)\n" ] } ], "source": [ "t4 = tf.constant([[0, 5],\n", " [1, 6],\n", " [2, 7],\n", " [3, 8],\n", " [4, 9]])\n", "\n", "print(tf.gather_nd(t4,\n", " indices=[[2], [3], [0]]))" ] }, { "cell_type": "markdown", "metadata": { "id": "87NN7YQhh2-a" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.187974Z", "iopub.status.busy": "2022-12-14T21:53:20.187285Z", "iopub.status.idle": "2022-12-14T21:53:20.194228Z", "shell.execute_reply": "2022-12-14T21:53:20.193635Z" }, "id": "_z6F2WcPJ9Rh" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([ 0 16], shape=(2,), dtype=int64)\n" ] } ], "source": [ "t5 = np.reshape(np.arange(18), [2, 3, 3])\n", "\n", "print(tf.gather_nd(t5,\n", " indices=[[0, 0, 0], [1, 2, 1]]))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.197216Z", "iopub.status.busy": "2022-12-14T21:53:20.196767Z", "iopub.status.idle": "2022-12-14T21:53:20.201364Z", "shell.execute_reply": "2022-12-14T21:53:20.200793Z" }, "id": "gyIjhm7cV2N0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[[ 0 1 2]\n", " [ 6 7 8]]\n", "\n", " [[ 9 10 11]\n", " [15 16 17]]], shape=(2, 2, 3), dtype=int64)\n" ] } ], "source": [ "# Return a list of two matrices\n", "\n", "print(tf.gather_nd(t5,\n", " indices=[[[0, 0], [0, 2]], [[1, 0], [1, 2]]]))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.204622Z", "iopub.status.busy": "2022-12-14T21:53:20.204128Z", "iopub.status.idle": "2022-12-14T21:53:20.208476Z", "shell.execute_reply": "2022-12-14T21:53:20.207922Z" }, "id": "368D4ciDWB3r" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[ 0 1 2]\n", " [ 6 7 8]\n", " [ 9 10 11]\n", " [15 16 17]], shape=(4, 3), dtype=int64)\n" ] } ], "source": [ "# Return one matrix\n", "\n", "print(tf.gather_nd(t5,\n", " indices=[[0, 0], [0, 2], [1, 0], [1, 2]]))" ] }, { "cell_type": "markdown", "metadata": { "id": "od51VzS2SSPS" }, "source": [ "## 텐서에 데이터 삽입\n", "\n", "`tf.scatter_nd` 를 사용하여 텐서의 특정 슬라이스/인덱스에 데이터를 삽입합니다. 값을 삽입하는 텐서는 0으로 초기화됩니다." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.211420Z", "iopub.status.busy": "2022-12-14T21:53:20.210957Z", "iopub.status.idle": "2022-12-14T21:53:20.216515Z", "shell.execute_reply": "2022-12-14T21:53:20.215965Z" }, "id": "jlALYLWm1KhN" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([ 0 2 0 4 0 6 0 8 0 10], shape=(10,), dtype=int32)\n" ] } ], "source": [ "t6 = tf.constant([10])\n", "indices = tf.constant([[1], [3], [5], [7], [9]])\n", "data = tf.constant([2, 4, 6, 8, 10])\n", "\n", "print(tf.scatter_nd(indices=indices,\n", " updates=data,\n", " shape=t6))" ] }, { "cell_type": "markdown", "metadata": { "id": "CD5vd-kxksW7" }, "source": [ "0으로 초기화된 텐서를 필요로 하는 `tf.scatter_nd` 와 같은 메서드는 희소 텐서 이니셜라이저와 유사합니다. `tf.gather_nd` 및 `tf.scatter_nd` 를 사용하여 희소 텐서 작업의 동작을 모방할 수 있습니다.\n", "\n", "이 두 가지 방법을 함께 사용하여 희소 텐서를 구성하는 예를 고려하십시오." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.219644Z", "iopub.status.busy": "2022-12-14T21:53:20.219071Z", "iopub.status.idle": "2022-12-14T21:53:20.222865Z", "shell.execute_reply": "2022-12-14T21:53:20.222331Z" }, "id": "xyK69QgRmrlW" }, "outputs": [], "source": [ "# Gather values from one tensor by specifying indices\n", "\n", "new_indices = tf.constant([[0, 2], [2, 1], [3, 3]])\n", "t7 = tf.gather_nd(t2, indices=new_indices)" ] }, { "cell_type": "markdown", "metadata": { "id": "_7V_Qfa4qkdn" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.225934Z", "iopub.status.busy": "2022-12-14T21:53:20.225379Z", "iopub.status.idle": "2022-12-14T21:53:20.229393Z", "shell.execute_reply": "2022-12-14T21:53:20.228795Z" }, "id": "QWT1E1Weqjx2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[ 0 0 2 0 0]\n", " [ 0 0 0 0 0]\n", " [ 0 11 0 0 0]\n", " [ 0 0 0 18 0]], shape=(4, 5), dtype=int32)\n" ] } ], "source": [ "# Add these values into a new tensor\n", "\n", "t8 = tf.scatter_nd(indices=new_indices, updates=t7, shape=tf.constant([4, 5]))\n", "\n", "print(t8)" ] }, { "cell_type": "markdown", "metadata": { "id": "NUyYjnvCn_vu" }, "source": [ "이것은 다음과 유사합니다." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.232520Z", "iopub.status.busy": "2022-12-14T21:53:20.232072Z", "iopub.status.idle": "2022-12-14T21:53:20.236852Z", "shell.execute_reply": "2022-12-14T21:53:20.236306Z" }, "id": "LeqFwUgroE4j" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SparseTensor(indices=tf.Tensor(\n", "[[0 2]\n", " [2 1]\n", " [3 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([ 2 11 18], shape=(3,), dtype=int32), dense_shape=tf.Tensor([4 5], shape=(2,), dtype=int64))\n" ] } ], "source": [ "t9 = tf.SparseTensor(indices=[[0, 2], [2, 1], [3, 3]],\n", " values=[2, 11, 18],\n", " dense_shape=[4, 5])\n", "\n", "print(t9)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.239872Z", "iopub.status.busy": "2022-12-14T21:53:20.239282Z", "iopub.status.idle": "2022-12-14T21:53:20.246542Z", "shell.execute_reply": "2022-12-14T21:53:20.245974Z" }, "id": "5MaF6RlJot33" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[ 0 0 2 0 0]\n", " [ 0 0 0 0 0]\n", " [ 0 11 0 0 0]\n", " [ 0 0 0 18 0]], shape=(4, 5), dtype=int32)\n" ] } ], "source": [ "# Convert the sparse tensor into a dense tensor\n", "\n", "t10 = tf.sparse.to_dense(t9)\n", "\n", "print(t10)" ] }, { "cell_type": "markdown", "metadata": { "id": "4sf3F3Xk56Bt" }, "source": [ "기존 값이 있는 텐서에 데이터를 삽입하려면 `tf.tensor_scatter_nd_add` 사용하세요." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.249631Z", "iopub.status.busy": "2022-12-14T21:53:20.249184Z", "iopub.status.idle": "2022-12-14T21:53:20.254900Z", "shell.execute_reply": "2022-12-14T21:53:20.254338Z" }, "id": "mte2ifOb6sQO" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[2 7 6]\n", " [9 5 1]\n", " [4 3 8]], shape=(3, 3), dtype=int32)\n" ] } ], "source": [ "t11 = tf.constant([[2, 7, 0],\n", " [9, 0, 1],\n", " [0, 3, 8]])\n", "\n", "# Convert the tensor into a magic square by inserting numbers at appropriate indices\n", "\n", "t12 = tf.tensor_scatter_nd_add(t11,\n", " indices=[[0, 2], [1, 1], [2, 0]],\n", " updates=[6, 5, 4])\n", "\n", "print(t12)" ] }, { "cell_type": "markdown", "metadata": { "id": "2dQYyROU09G6" }, "source": [ "마찬가지로 `tf.tensor_scatter_nd_sub` 를 사용하여 기존 값이 있는 텐서에서 값을 뺍니다." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.258015Z", "iopub.status.busy": "2022-12-14T21:53:20.257552Z", "iopub.status.idle": "2022-12-14T21:53:20.263200Z", "shell.execute_reply": "2022-12-14T21:53:20.262623Z" }, "id": "ac6_i6uK1EI6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[1 0 0]\n", " [0 1 0]\n", " [0 0 1]], shape=(3, 3), dtype=int32)\n" ] } ], "source": [ "# Convert the tensor into an identity matrix\n", "\n", "t13 = tf.tensor_scatter_nd_sub(t11,\n", " indices=[[0, 0], [0, 1], [1, 0], [1, 1], [1, 2], [2, 1], [2, 2]],\n", " updates=[1, 7, 9, -1, 1, 3, 7])\n", "\n", "print(t13)" ] }, { "cell_type": "markdown", "metadata": { "id": "B_2DuzRRwVc8" }, "source": [ "`tf.tensor_scatter_nd_min` 을 사용하여 한 텐서에서 다른 텐서로 요소별 최소값을 복사합니다." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.266404Z", "iopub.status.busy": "2022-12-14T21:53:20.265896Z", "iopub.status.idle": "2022-12-14T21:53:20.271975Z", "shell.execute_reply": "2022-12-14T21:53:20.271348Z" }, "id": "T_4FrHrHlkHK" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[-2 -7 -6]\n", " [-9 -5 1]\n", " [-4 -3 -8]], shape=(3, 3), dtype=int32)\n" ] } ], "source": [ "t14 = tf.constant([[-2, -7, 0],\n", " [-9, 0, 1],\n", " [0, -3, -8]])\n", "\n", "t15 = tf.tensor_scatter_nd_min(t14,\n", " indices=[[0, 2], [1, 1], [2, 0]],\n", " updates=[-6, -5, -4])\n", "\n", "print(t15)" ] }, { "cell_type": "markdown", "metadata": { "id": "PkaiKyrF0WtX" }, "source": [ "마찬가지로 `tf.tensor_scatter_nd_max` 를 사용하여 한 텐서에서 다른 텐서로 요소별 최대값을 복사합니다." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T21:53:20.275311Z", "iopub.status.busy": "2022-12-14T21:53:20.274819Z", "iopub.status.idle": "2022-12-14T21:53:20.280595Z", "shell.execute_reply": "2022-12-14T21:53:20.279947Z" }, "id": "izJu0nXi0GDq" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[-2 -7 6]\n", " [-9 5 1]\n", " [ 4 -3 -8]], shape=(3, 3), dtype=int32)\n" ] } ], "source": [ "t16 = tf.tensor_scatter_nd_max(t14,\n", " indices=[[0, 2], [1, 1], [2, 0]],\n", " updates=[6, 5, 4])\n", "\n", "print(t16)" ] }, { "cell_type": "markdown", "metadata": { "id": "QAffUOa-85lF" }, "source": [ "## 추가 읽을거리 및 리소스\n", "\n", "이 가이드에서는 TensorFlow에서 사용할 수 있는 텐서 슬라이싱 작업을 사용하여 텐서의 요소를 더 세밀하게 제어하는 방법을 배웠습니다.\n", "\n", "- `tf.experimental.numpy.take_along_axis` 및 `tf.experimental.numpy.take` 와 같이 TensorFlow NumPy에서 사용할 수 있는 슬라이싱 작업을 확인하세요.\n", "\n", "- [Tensor 가이드](https://www.tensorflow.org/guide/tensor) 와 [Variable 가이드](https://www.tensorflow.org/guide/variable) 도 확인하세요." ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "tensor_slicing.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 0 }