{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "ZjN_IJ8mhJ-4" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2024-01-11T18:47:12.101188Z", "iopub.status.busy": "2024-01-11T18:47:12.100961Z", "iopub.status.idle": "2024-01-11T18:47:12.104718Z", "shell.execute_reply": "2024-01-11T18:47:12.104142Z" }, "id": "sY3Ffd83hK3b" }, "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": "03Pw58e6mTHI" }, "source": [ "# TensorFlow の NumPy API" ] }, { "cell_type": "markdown", "metadata": { "id": "7WpGysDJmZsg" }, "source": [ "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
tf.experimental.numpy
を利用してNumPy API のサブセットを実装します。これにより、TensorFlow により高速化された NumPy コードを実行し、TensorFlow のすべて API にもアクセスできます。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ob1HNwUmYR5b"
},
"source": [
"## セットアップ\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-11T18:47:12.108567Z",
"iopub.status.busy": "2024-01-11T18:47:12.108305Z",
"iopub.status.idle": "2024-01-11T18:47:14.712532Z",
"shell.execute_reply": "2024-01-11T18:47:14.711782Z"
},
"id": "AJR558zjAZQu"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-01-11 18:47:12.854741: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
"2024-01-11 18:47:12.854782: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
"2024-01-11 18:47:12.856314: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using TensorFlow version 2.15.0\n"
]
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import tensorflow.experimental.numpy as tnp\n",
"import timeit\n",
"\n",
"print(\"Using TensorFlow version %s\" % tf.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "M6tacoy0DU6e"
},
"source": [
"### NumPy 動作の有効化\n",
"\n",
"`tnp` を NumPy として使用するには、TensorFlow の NumPy の動作を有効にしてください。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-11T18:47:14.716312Z",
"iopub.status.busy": "2024-01-11T18:47:14.715892Z",
"iopub.status.idle": "2024-01-11T18:47:14.719443Z",
"shell.execute_reply": "2024-01-11T18:47:14.718718Z"
},
"id": "TfCyofpFDQxm"
},
"outputs": [],
"source": [
"tnp.experimental_enable_numpy_behavior()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "et9D5wq0D1H2"
},
"source": [
"この呼び出しによって、TensorFlow での型昇格が可能になり、リテラルからテンソルに変換される場合に、型推論も Numpy の標準により厳格に従うように変更されます。\n",
"\n",
"注意: この呼び出しは、`tf.experimental.numpy` モジュールだけでなく、TensorFlow 全体の動作を変更します。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yh2BwqUzH3C3"
},
"source": [
"## TensorFlow NumPy ND 配列\n",
"\n",
"**ND 配列**と呼ばれる `tf.experimental.numpy.ndarray` は、特定のデバイスに配置されたある `dtype` の多次元の密な配列を表します。`tf.Tensor` のエイリアスです。`ndarray.T`、`ndarray.reshape`、`ndarray.ravel` などの便利なメソッドについては、ND 配列クラスをご覧ください。\n",
"\n",
"まず、ND 配列オブジェクトを作成してから、さまざまなメソッドを呼び出します。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-11T18:47:14.723114Z",
"iopub.status.busy": "2024-01-11T18:47:14.722633Z",
"iopub.status.idle": "2024-01-11T18:47:16.992303Z",
"shell.execute_reply": "2024-01-11T18:47:16.991532Z"
},
"id": "-BHJjxigJ2H1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Created ND array with shape = (5, 3), rank = 2, dtype = np.object
)が含まれます。詳細については、 TensorFlow NumPy API ドキュメントをご覧ください。\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Jb1KXak2YlNN"
},
"source": [
"### NumPy 相互運用性\n",
"\n",
"TensorFlow ND 配列は、NumPy 関数と相互運用できます。これらのオブジェクトは、`__array__`インターフェースを実装します。NumPy はこのインターフェースを使用して、関数の引数を処理する前に`np.ndarray`値に変換します。\n",
"\n",
"同様に、TensorFlow NumPy 関数は、`np.ndarray` などのさまざまなタイプの入力を受け入れることができます。これらの入力は、ndarray.asarray
を呼び出すことにより、ND 配列に変換されます。\n",
"\n",
"ND 配列を`np.ndarray`との間で変換すると、実際のデータコピーがトリガーされる場合があります。詳細については、[バッファコピー](#Buffer-copies)のセクションを参照してください。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2024-01-11T18:47:18.040315Z",
"iopub.status.busy": "2024-01-11T18:47:18.040030Z",
"iopub.status.idle": "2024-01-11T18:47:18.052216Z",
"shell.execute_reply": "2024-01-11T18:47:18.051583Z"
},
"id": "cMOCgzQmeXRU"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sum = 6.0. Class: