みらいテックラボ

音声・画像認識や機械学習など, 週末プログラマである管理人が興味のある技術の紹介や実際にトライしてみた様子などメモしていく.

TensorFlow はじめの一歩(2)

今回は, 定数/数列/乱数テンソル[1]に関するPython APIについて, 少しまとめておく.
注) TensorFlow : Python API 0.6.0ベース

1. 定数テンソル

API機能
tf.zeros(shape, dtype=tf.float32, name=None)すべての要素が0のTensorを生成
tf.zeros_like(tensor, dtype=None, name=None)tensorと同じtype, shapeで, すべての要素が0のTensorを生成
tf.ones(shape, dtype=tf.float32, name=None)すべての要素が1のTensorを生成
tf.ones_like(tensor, dtype=None, name=None)tensorと同じtype, shapeで, すべての要素が1のTensorを生成
tf.fill(dims, value, name=None)スカラー値でfillしたTensorを生成
tf.constant(value, dtype=None, shape=None, name=Const)定数のTensorを生成

サンプルコード:

import tensorflow as tf

sess = tf.InteractiveSession()

################
# tf.zeros
################
tf_zeros = tf.zeros([3, 4], "int32")
print "tf.zeros"
print sess.run(tf_zeros)

# output:

# tf.zeros
# [[0 0 0 0]
#  [0 0 0 0]
#  [0 0 0 0]]

################
# tf.zeros_like
################
a = tf.constant([[1, 2, 3], [4, 5, 6]])
tf_zeros_like = tf.zeros_like(a)
print "a"
print sess.run(a)
print "tf.zeros_like"
print sess.run(tf_zeros_like)

# output:

# a
# [[1 2 3]
#  [4 5 6]]
# tf.zeros_like
# [[0 0 0]
#  [0 0 0]]

################
# tf.ones
################
tf_ones = tf.ones([2, 3], "int32")
print "tf.ones"
print sess.run(tf_ones)

# output:

# tf.ones
# [[1 1 1]
#  [1 1 1]]

################
# tf.ones_like
################
a = tf.constant([[1, 2, 3], [4, 5, 6]])
tf_ones_like = tf.ones_like(a)
print "a"
print sess.run(a)
print "tf.ones_like"
print sess.run(tf_ones_like)

# output:

# a
# [[1 2 3]
#  [4 5 6]]
# tf.ones_like
# [[1 1 1]
#  [1 1 1]]

################
# tf.fill
################
tf_fill = tf.fill((2, 3), 9)
print "tf.fill"
print sess.run(tf_fill)

# output:

# tf.fill
# [[9 9 9]
#  [9 9 9]]

################
# tf.constant
################
# 1-D Tensor
tf_const1 = tf.constant([1, 2, 3, 4, 5, 6, 7])
# 2-D Tensor
tf_const2 = tf.constant(-1.0, shape=[2, 3])
print "tf.constant"
print "1-D"
print sess.run(tf_const1)
print "2-D"
print sess.run(tf_const2)

# output:

# tf.constant
# 1-D
# [1 2 3 4 5 6 7]
# 2-D
# [[-1. -1. -1.]
#  [-1. -1. -1.]]

sess.close()

2. 数列

API機能
tf.linspace(start, stop, num, name=None)ある範囲を要素数になるように区切った値を生成
tf.range(start, limit=None, delta=1, name=range)一定間隔の整数列を生成

サンプルコード:

import tensorflow as tf

sess = tf.InteractiveSession()

################
# tf.linspace
################
tf_linspace = tf.linspace(10.0, 12.0, 3, name="linspace")
print "tf.linspace"
print sess.run(tf_linspace)

# output:

# tf.linspace
# [ 10.  11.  12.]

################
# tf.range
################
# 'start' is 3
# 'limit' is 18
# 'delta' is 3
tf_range1 = tf.range(3, 18, 3)
# 'limit' is 5
tf_range2 = tf.range(5)
print "tf.range"
print "start = 3, limit = 18, delta = 3"
print sess.run(tf_range1)
print "limit = 5"
print sess.run(tf_range2)

# output:

# tf.range
# start = 3, limit = 18, delta = 3
# [ 3  6  9 12 15]
# limit = 5
# [0 1 2 3 4]

sess.close()

3. 乱数テンソル

API機能
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)正規分布から乱数を出力
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)切断正規分布から乱数を出力
tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)一様分布から乱数を出力
tf.random_shuffle(value, seed=None, name=None)Tensorをランダムにシャッフル
tf.set_random_seed(seed)乱数のシード値を設定

サンプルコード:

import tensorflow as tf

sess = tf.InteractiveSession()

######################
# tf.random_normal
######################
tf_normal = tf.random_normal([2, 3], mean=-1, stddev=4)
print "tf.random_normal"
print sess.run(tf_normal)

# output(example):

# tf.random_normal
# [[-4.77949524  1.09623837 -1.79200745]
#  [ 0.96336484 -5.47785091 -1.14655089]

######################
# tf.truncated_normal
######################
tf_truncated = tf.truncated_normal([2, 3])
print "tf.truncated_normal"
print sess.run(tf_truncated)

# output(example):

# tf.truncated_normal
# [[ 0.73809606  1.65349591  0.15235977]
#  [ 0.42829624 -0.83115262 -0.44399944]]

######################
# tf.random_uniform
######################
tf_uniform = tf.random_uniform([2, 3], minval=0, maxval=5)
print "tf.random_uniform"
print sess.run(tf_uniform)

# output(example):

# tf.random_uniform
# [[ 1.65245831  4.85745525  1.04827166]
#  [ 2.29264975  0.33302426  1.61023617]]

######################
# tf.random_shuffle
######################
c = tf.constant([[1, 2], [3, 4], [5, 6]])
tf_shuff = tf.random_shuffle(c)
print "c"
print sess.run(c)
print "tf.random_shuffle"
print sess.run(tf_shuff)

# output(example):

# c
# [[1 2]
#  [3 4]
#  [5 6]]
# tf.random_shuffle
# [[5 6]
#  [3 4]
#  [1 2]]

######################
# tf.set_random_seed
######################
tf.set_random_seed(1234)
a = tf.random_uniform([1])
b = tf.random_normal([1])

print "tf.set_random_seed"
print "Session 1"
with tf.Session() as sess1:
    print sess1.run(a)
    print sess1.run(a)
    print sess1.run(b)
    print sess1.run(b)

print "Session 2"
with tf.Session() as sess2:
    print sess2.run(a)
    print sess2.run(a)
    print sess2.run(b)
    print sess2.run(b)

# output(example):

# tf.set_random_seed
# Session 1
# [ 0.81463408]
# [ 0.84950328]
# [-0.28481847]
# [ 0.23557714]
# Session 2
# [ 0.81463408]
# [ 0.84950328]
# [-0.28481847]
# [ 0.23557714]

sess.close()

----
参照URL:
[1] Constants, Sequences, and Random Values | TensorFlow




イラストで学ぶ ディープラーニング (KS情報科学専門書)

イラストで学ぶ ディープラーニング (KS情報科学専門書)


ITエンジニアのための機械学習理論入門

ITエンジニアのための機械学習理論入門


深層学習 (機械学習プロフェッショナルシリーズ)

深層学習 (機械学習プロフェッショナルシリーズ)


初めてのディープラーニング --オープンソース

初めてのディープラーニング --オープンソース"Caffe"による演習付き