定数と変数

Introduction to TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

TensorFlow とは?

  • グラフベースの数値計算用オープンソースライブラリ
    • Google Brain Team により開発
  • 高・低レベル API
    • 加算、乗算、微分
    • 機械学習モデル
  • TensorFlow 2.0 の主な変更点
    • 既定で eager execution
    • Keras と Estimators によるモデル構築
Introduction to TensorFlow in Python

テンソルとは?

  • ベクトルと行列の一般化
  • 数値の集まり
  • 特定の形状を持つ
Introduction to TensorFlow in Python

テンソルとは?

画像はパンのスライス、9分割したスライス、そしてパン一斤を示す。 出典: Public Domain Vectors

Introduction to TensorFlow in Python

TensorFlow でテンソルを定義する

import tensorflow as tf

# 0 次元テンソル
d0 = tf.ones((1,))
# 1 次元テンソル
d1 = tf.ones((2,))
# 2 次元テンソル
d2 = tf.ones((2, 2))
# 3 次元テンソル
d3 = tf.ones((2, 2, 2))
Introduction to TensorFlow in Python

TensorFlow でテンソルを定義する

# 3 次元テンソルを表示
print(d3.numpy())
[[[1. 1.]
  [1. 1.]]

 [[1. 1.]
  [1. 1.]]]
Introduction to TensorFlow in Python

TensorFlow で定数を定義する

  • 定数は最も単純なテンソルの種類
    • 学習対象ではない(非trainable)
    • 次元は任意
from tensorflow import constant

# 2x3 の定数を定義
a = constant(3, shape=[2, 3])
# 2x2 の定数を定義
b = constant([1, 2, 3, 4], shape=[2, 2])
Introduction to TensorFlow in Python

定数定義のための補助関数の利用

操作 例
tf.constant() constant([1, 2, 3])
tf.zeros() zeros([2, 2])
tf.zeros_like() zeros_like(input_tensor)
tf.ones() ones([2, 2])
tf.ones_like() ones_like(input_tensor)
tf.fill() fill([3, 3], 7)
Introduction to TensorFlow in Python

変数の定義と初期化

import tensorflow as tf

# 変数を定義
a0 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.float32)
a1 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.int16)
# 定数を定義
b = tf.constant(2, tf.float32)
# 積を計算
c0 = tf.multiply(a0, b)
c1 = a0*b
Introduction to TensorFlow in Python

練習しましょう!

Introduction to TensorFlow in Python

Preparing Video For Download...