Skip to content

ClasCat® AI Research

クラスキャット – 生成 AI, AI エージェント, MCP

Menu
  • ホーム
    • ClassCat® AI Research ホーム
    • クラスキャット・ホーム
  • OpenAI API
    • OpenAI Python ライブラリ 1.x : 概要
    • OpenAI ブログ
      • GPT の紹介
      • GPT ストアの紹介
      • ChatGPT Team の紹介
    • OpenAI platform 1.x
      • Get Started : イントロダクション
      • Get Started : クイックスタート (Python)
      • Get Started : クイックスタート (Node.js)
      • Get Started : モデル
      • 機能 : 埋め込み
      • 機能 : 埋め込み (ユースケース)
      • ChatGPT : アクション – イントロダクション
      • ChatGPT : アクション – Getting started
      • ChatGPT : アクション – アクション認証
    • OpenAI ヘルプ : ChatGPT
      • ChatGPTとは何ですか?
      • ChatGPT は真実を語っていますか?
      • GPT の作成
      • GPT FAQ
      • GPT vs アシスタント
      • GPT ビルダー
    • OpenAI ヘルプ : ChatGPT > メモリ
      • FAQ
    • OpenAI ヘルプ : GPT ストア
      • 貴方の GPT をフィーチャーする
    • OpenAI Python ライブラリ 0.27 : 概要
    • OpenAI platform
      • Get Started : イントロダクション
      • Get Started : クイックスタート
      • Get Started : モデル
      • ガイド : GPT モデル
      • ガイド : 画像生成 (DALL·E)
      • ガイド : GPT-3.5 Turbo 対応 微調整
      • ガイド : 微調整 1.イントロダクション
      • ガイド : 微調整 2. データセットの準備 / ケーススタディ
      • ガイド : 埋め込み
      • ガイド : 音声テキスト変換
      • ガイド : モデレーション
      • ChatGPT プラグイン : イントロダクション
    • OpenAI Cookbook
      • 概要
      • API 使用方法 : レート制限の操作
      • API 使用方法 : tiktoken でトークンを数える方法
      • GPT : ChatGPT モデルへの入力をフォーマットする方法
      • GPT : 補完をストリームする方法
      • GPT : 大規模言語モデルを扱う方法
      • 埋め込み : 埋め込みの取得
      • GPT-3 の微調整 : 分類サンプルの微調整
      • DALL-E : DALL·E で 画像を生成して編集する方法
      • DALL·E と Segment Anything で動的マスクを作成する方法
      • Whisper プロンプティング・ガイド
  • Gemini API
    • Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成
    • (2) マルチモーダル入力 / 日本語チャット
    • (3) 埋め込みの使用
    • (4) 高度なユースケース
    • クイックスタート with Node.js
    • クイックスタート with Dart or Flutter (1) 日本語動作確認
    • Gemma
      • 概要 (README)
      • Tutorials : サンプリング
      • Tutorials : KerasNLP による Getting Started
  • Keras 3
    • 新しいマルチバックエンド Keras
    • Keras 3 について
    • Getting Started : エンジニアのための Keras 入門
    • Google Colab 上のインストールと Stable Diffusion デモ
    • コンピュータビジョン – ゼロからの画像分類
    • コンピュータビジョン – 単純な MNIST convnet
    • コンピュータビジョン – EfficientNet を使用した微調整による画像分類
    • コンピュータビジョン – Vision Transformer による画像分類
    • コンピュータビジョン – 最新の MLPモデルによる画像分類
    • コンピュータビジョン – コンパクトな畳込み Transformer
    • Keras Core
      • Keras Core 0.1
        • 新しいマルチバックエンド Keras (README)
        • Keras for TensorFlow, JAX, & PyTorch
        • 開発者ガイド : Getting started with Keras Core
        • 開発者ガイド : 関数型 API
        • 開発者ガイド : シーケンシャル・モデル
        • 開発者ガイド : サブクラス化で新しい層とモデルを作成する
        • 開発者ガイド : 独自のコールバックを書く
      • Keras Core 0.1.1 & 0.1.2 : リリースノート
      • 開発者ガイド
      • Code examples
      • Keras Stable Diffusion
        • 概要
        • 基本的な使い方 (テキスト-to-画像 / 画像-to-画像変換)
        • 混合精度のパフォーマンス
        • インペインティングの簡易アプリケーション
        • (参考) KerasCV – Stable Diffusion を使用した高性能画像生成
  • TensorFlow
    • TF 2 : 初級チュートリアル
    • TF 2 : 上級チュートリアル
    • TF 2 : ガイド
    • TF 1 : チュートリアル
    • TF 1 : ガイド
  • その他
    • 🦜️🔗 LangChain ドキュメント / ユースケース
    • Stable Diffusion WebUI
      • Google Colab で Stable Diffusion WebUI 入門
      • HuggingFace モデル / VAE の導入
      • LoRA の利用
    • Diffusion Models / 拡散モデル
  • クラスキャット
    • 会社案内
    • お問合せ
    • Facebook
    • ClassCat® Blog
Menu

TensorFlow : Tutorials : Eager : Eager execution 基本

Posted on 07/16/2018 by Sales Information

TensorFlow : Tutorials : Eager : Eager execution 基本 (翻訳/解説)

翻訳 : (株)クラスキャット セールスインフォメーション
更新日時 : 04/11/2019
作成日時 : 07/16/2018

* TensorFlow 1.9 でドキュメント構成が変わり新規にチュートリアル・ページも追加されました。
* 本ページは、TensorFlow 本家サイトの Tutorials – Research and Experimentation – Eager execution basics を
翻訳した上で適宜、補足説明したものです:

  • https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb

* サンプルコードの動作確認はしておりますが、適宜、追加改変している場合もあります。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

★ 無料セミナー開催中 ★ クラスキャット主催 人工知能 & ビジネス Web セミナー

人工知能とビジネスをテーマにウェビナー (WEB セミナー) を定期的に開催しています。スケジュールは弊社 公式 Web サイト でご確認頂けます。
  • お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
  • Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。

◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。

株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/
Facebook: https://www.facebook.com/ClassCatJP/

 

序

これは TensorFlow を (eager execution を有効にして) 使用するための初歩的なチュートリアルです。以下をカバーします :

  • 必要なパッケージをインポートする。
  • Tensor を作成して使用する。
  • GPU アクセラレーション。
  • データセット。

 

Import TensorFlow

始めるためには、tensorflow モジュールをインポートして Eager execution を有効にします。Eager execution は TensorFlow により対話的なフロントエンドを可能にします。

import tensorflow as tf

tf.enable_eager_execution()

 

Tensor

Tensor は多次元配列です。NumPy ndarray オブジェクトと同様に、Tensor オブジェクトはデータ型と shape を持ちます。更に、Tensor は (GPU のような) アクセラレータ・メモリ内に常駐できます。TensorFlow は Tensor を消費して生成する演算 (tf.add, tf.matmul, tf.linalg.inv etc.) のリッチなライブラリを提供します。これらの演算は native Python 型を自動的に変換します。例えば :

print(tf.add(1, 2))
print(tf.add([1, 2], [3, 4]))
print(tf.square(5))
print(tf.reduce_sum([1, 2, 3]))
print(tf.encode_base64("hello world"))

# Operator overloading is also supported
print(tf.square(2) + tf.square(3))

Out:

tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([4 6], shape=(2,), dtype=int32)
tf.Tensor(25, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(b'aGVsbG8gd29ybGQ', shape=(), dtype=string)
tf.Tensor(13, shape=(), dtype=int32)

各 Tensor は shape とデータ型を持ちます。

x = tf.matmul([[1]], [[2, 3]])
print(x.shape)
print(x.dtype)

Out:

(1, 2)
<dtype: 'int32'>

NumPy 配列と TensorFlow Tensor の間の最も明白な違いは :

  1. Tensor は (GPU, TPU のような) アクセラレータ・メモリで支援可能です。
  2. Tensors はイミュータブルです。

 

NumPy 互換性

TensorFlow Tensor と NumPy ndarray の間の変換は次のように非常に単純です :

  • TensorFlow 演算は NumPy ndarray を Tensor に自動的に変換します。
  • NumPy 演算は Tensor を NumPy ndarray に自動的に変換します。

Tensor はそれらの上で .numpy() メソッドを呼び出すことで NumPy ndarray に明示的に変換できます。これらの変換は典型的には安上がりです、何故ならば array と Tensor は可能であれば基礎的なメモリ表現を共有するからです。けれども、基礎的な表現は常に可能であるとは限りません、何故ならば Tensor は GPU メモリ内にホストされるかもしれず、その一方で NumPy 配列は常にホストメモリで支援されるからで、そのため変換は GPU からホストメモリへのコピーを呼び起こします。

import numpy as np

ndarray = np.ones([3, 3])

print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.multiply(ndarray, 42)
print(tensor)


print("And NumPy operations convert Tensors to numpy arrays automatically")
print(np.add(tensor, 1))

print("The .numpy() method explicitly converts a Tensor to a numpy array")
print(tensor.numpy())

Out:

TensorFlow operations convert numpy arrays to Tensors automatically
tf.Tensor(
[[ 42.  42.  42.]
 [ 42.  42.  42.]
 [ 42.  42.  42.]], shape=(3, 3), dtype=float64)
And NumPy operations convert Tensors to numpy arrays automatically
[[ 43.  43.  43.]
 [ 43.  43.  43.]
 [ 43.  43.  43.]]
The .numpy() method explicitly converts a Tensor to a numpy array
[[ 42.  42.  42.]
 [ 42.  42.  42.]
 [ 42.  42.  42.]]

 

GPU アクセラレーション

多くの TensorFlow 演算は計算のために GPU を使用することによりアクセラレートできます。どのようなアノテーションもなければ、TensorFlow は演算のために GPU か CPU を使用するかを自動的に決定します (そして必要であれば CPU と GPU メモリ間で tensor をコピーします)。演算により生成される tensor は典型的には (その上で演算が実行される) デバイスのメモリにより支援されます。例えば :

x = tf.random_uniform([3, 3])

print("Is there a GPU available: "),
print(tf.test.is_gpu_available())

print("Is the Tensor on GPU #0:  "),
print(x.device.endswith('GPU:0'))

Out: (CPU 環境の場合)

Is there a GPU available: 
False
Is the Tensor on GPU #0:  
False

 

デバイス名

Tensor.device プロパティは Tensor のコンテンツをホスティングするデバイスの完全修飾文字列名を提供します。この名前は (その上でプログラムが動作する) ホストのネットワーク・アドレスの識別子とホスト内のデバイスのような多くの詳細をエンコードします。これは TensorFlow プログラムの分散実行のために必要ですが、当面はそれをスキップします。tensor がホスト上の N-th GPU 上に置かれる場合には文字列は GPU:<N> で終わります。

 

明示的なデバイスへの配置

TensorFlow での用語「配置 (= placement)」は個々の演算が実行のためにどのようにデバイス上割り当てられるか (置かれるか) を指し示します。上で言及したように、提供される明示的なガイダンスがないときには、TensorFlow は演算を実行するためのデバイスを自動的に決定して必要であれば Tensor をそのデバイスにコピーします。けれども、TensorFlow 演算はtf.device コンテキスト・マネージャを使用して特定のデバイスに明示的に置かれることができます。例えば :

def time_matmul(x):
  %timeit tf.matmul(x, x)

# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
  x = tf.random_uniform([1000, 1000])
  assert x.device.endswith("CPU:0")
  time_matmul(x)

# Force execution on GPU #0 if available
if tf.test.is_gpu_available():
  with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
    x = tf.random_uniform([1000, 1000])
    assert x.device.endswith("GPU:0")
    time_matmul(x)
On CPU:
10.5 ms ± 38.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

 

データセット

このセクションはモデルにデータを供給するためのパイプラインを構築するために tf.data.Dataset API の使用方法を示します。それは次をカバーします :

  • データセットを作成する。
  • eager execution が有効である際にデータセットに渡り iterate する。

モデルの訓練と評価ループに供給する単純で、再利用可能なピースから高性能で、複雑な入力パイプラインを構築するために Dataset API を使用することを推奨します。

TensorFlow グラフに精通しているのであれば、eager execution が有効であるとき Dataset オブジェクトを構築するための API は正確に同じままですが、データセットの要素に渡り iterate するプロセスは少しだけ単純です。tf.data.Dataset オブジェクトに渡り Python iteration を使用できて tf.data.Iterator オブジェクトを明示的に作成する必要がありません。結果的に、TensorFlow ガイド の iterator の議論は eager execution が有効であるときには関係ありません。

 

ソース Dataset を作成する

ソース dataset は、(Dataset.from_tensors, Dataset.from_tensor_slices のような) factory 関数の一つを使用するか、(TextLineDataset や TFRecordDataset のような) ファイルから読むオブジェクトを使用して作成します。更なる情報のためには TensorFlow ガイド の “入力データを読む” を見てください。

ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6])

# Create a CSV file
import tempfile
_, filename = tempfile.mkstemp()

with open(filename, 'w') as f:
  f.write("""Line 1
Line 2
Line 3
  """)

ds_file = tf.data.TextLineDataset(filename)

 

変換を適用する

dataset のレコードに変換を適用するために map, batch, shuffle etc. のような変換関数を使用します。詳細については tf.data.Dataset のための API ドキュメント を見てください。

ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2)

ds_file = ds_file.batch(2)

 

Iterate

eager execution が有効であるとき Dataset オブジェクトは iteration をサポートします。TensorFlow グラフの Dataset の使用に精通している場合、Dataset.make_one_shot_iterator() または get_next() コールへの呼び出しの必要はないことに注意してください。

print('Elements of ds_tensors:')
for x in ds_tensors:
  print(x)

print('\nElements in ds_file:')
for x in ds_file:
  print(x)

Out:

Elements of ds_tensors:
tf.Tensor([1 4], shape=(2,), dtype=int32)
tf.Tensor([ 9 16], shape=(2,), dtype=int32)
tf.Tensor([25 36], shape=(2,), dtype=int32)

Elements in ds_file:
tf.Tensor([b'Line 1' b'Line 2'], shape=(2,), dtype=string)
tf.Tensor([b'Line 3' b'  '], shape=(2,), dtype=string)

 
以上






クラスキャット

最近の投稿

  • LangGraph : 例題 : エージェント型 RAG
  • LangGraph Platform : Get started : クイックスタート
  • LangGraph Platform : 概要
  • LangGraph : Prebuilt エージェント : ユーザインターフェイス
  • LangGraph : Prebuilt エージェント : 配備

タグ

AutoGen (13) ClassCat Press Release (20) ClassCat TF/ONNX Hub (11) DGL 0.5 (14) Eager Execution (7) Edward (17) FLUX.1 (16) Gemini (20) HuggingFace Transformers 4.5 (10) HuggingFace Transformers 4.6 (7) HuggingFace Transformers 4.29 (9) Keras 2 Examples (98) Keras 2 Guide (16) Keras 3 (10) Keras Release Note (17) Kubeflow 1.0 (10) LangChain (45) LangGraph (21) MediaPipe 0.8 (11) Model Context Protocol (16) NNI 1.5 (16) OpenAI Agents SDK (8) OpenAI Cookbook (13) OpenAI platform (10) OpenAI platform 1.x (10) OpenAI ヘルプ (8) TensorFlow 2.0 Advanced Tutorials (33) TensorFlow 2.0 Advanced Tutorials (Alpha) (15) TensorFlow 2.0 Advanced Tutorials (Beta) (16) TensorFlow 2.0 Guide (10) TensorFlow 2.0 Guide (Alpha) (16) TensorFlow 2.0 Guide (Beta) (9) TensorFlow 2.0 Release Note (12) TensorFlow 2.0 Tutorials (20) TensorFlow 2.0 Tutorials (Alpha) (14) TensorFlow 2.0 Tutorials (Beta) (12) TensorFlow 2.4 Guide (24) TensorFlow Deploy (8) TensorFlow Get Started (7) TensorFlow Graphics (7) TensorFlow Probability (9) TensorFlow Programmer's Guide (22) TensorFlow Release Note (18) TensorFlow Tutorials (33) TF-Agents 0.4 (11)
2018年7月
月 火 水 木 金 土 日
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« 6月   8月 »
© 2025 ClasCat® AI Research | Powered by Minimalist Blog WordPress Theme