LoginSignup
1
0

More than 3 years have passed since last update.

初心者がTensorFlowを使ってみる〜tf.constant()の勉強〜(2日目)

Last updated at Posted at 2021-02-20

今日勉強していくこと

とりあえず、変数(定数)の定義とか、それすらわかっていないので、呼び出しとかそういうのを学んでいくよ

PHPでいうこんな感じのやつ。(めっちゃ基礎)

$a = 1;
$b = 2;
$all = $a+$b;
var_dump($all);

$hello = "hello world";
var_dump($hello);

TensorFlowについて少し理解が進んだこと

TensorFlowっていうのは、処理を順番通り進めるのとは違って、TensorFlowが自動的に並列処理の計画をたてて、それ通りに実行をしてくれる。
つまり普通に書いた方がシンプルだけど、並列処理で動かした方がいいような負荷が高い処理は、これを使った方がメリットがある。
というようなことらしい。

python開始とtensorflowのimport

昨日インストールしたpythonをターミナルで起動するよ
% python

tensorflowの呼び出し
>>> import tensorflow as tf

tf.constantで定数定義

tf.constant(値)
これで定義していくらしいのでやってみる。

>>> a = tf.constant(100)
2021-02-20 10:32:32.316004: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-20 10:32:32.316412: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

>>> b = tf.constant(300)

なんかめっちゃ出てるけど1日目にみたスルーしていいやつっぽいので無視してbを作ったよ。
初回だけ警告が出るみたいだね。

計算して結果を出してみる

>>> result = a+b
>>> print(result)
tf.Tensor(400, shape=(), dtype=int32)

なんか色々一緒についてきてるけど400はでた!
1つ目が結果、2つ目はわからないから飛ばして、3つ目のdtypeは結果のデータタイプを出してくれているみたい。

printの動きを見ていく。

tf.Tensor(400, shape=(), dtype=int32)

これの確認のために色々見ていく。

>>> print(tf.constant(1))
tf.Tensor(1, shape=(), dtype=int32)
>>> print(tf.constant(1.111))
tf.Tensor(1.111, shape=(), dtype=float32)
>>> print(tf.constant("hello"))
tf.Tensor(b'hello', shape=(), dtype=string)
>>> print(tf.constant("1"))
tf.Tensor(b'1', shape=(), dtype=string)

細かいところは置いといてなんかわかってきた気がする

あれ、shapeってなんなん

配列とか入れると出てきてるっぽいので、今度は配列を試してみる。
そもそも配列を扱うのに特化したやつなんだよね確か・・?

>>> print(tf.constant([1,2,3]))
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
>>> print(tf.constant([1,2,3,4,5,6,7,8,9]))
tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)
>>> print(tf.constant(["a","b","c"]))
tf.Tensor([b'a' b'b' b'c'], shape=(3,), dtype=string)
>>> print(tf.constant(["a","b","c","d"]))
tf.Tensor([b'a' b'b' b'c' b'd'], shape=(4,), dtype=string)

でたshape!!
配列の要素の数が出てくるのか
(これくらいならググるだけでもいいけど、自分で色々やった方が覚えるよね)

ここまでわかったところで、マニュアルを読んでいく

tf.constant(
    value, dtype=None, shape=None, name='Const'
)

こんな感じで引数を指定できるらしい。

shapeの動きを色々見てみる

shapeの1つめの数値を変えてみる

>>> print(tf.constant(7, shape=[2, 2]))
tf.Tensor(
[[7 7]
 [7 7]], shape=(2, 2), dtype=int32)

>>> print(tf.constant(7, shape=[3, 2]))
tf.Tensor(
[[7 7]
 [7 7]
 [7 7]], shape=(3, 2), dtype=int32)

>>> print(tf.constant(7, shape=[10, 2]))
tf.Tensor(
[[7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]
 [7 7]], shape=(10, 2), dtype=int32)

shapeの2つめの数値を変えてみる

>>> print(tf.constant(7, shape=[1, 2]))
tf.Tensor([[7 7]], shape=(1, 2), dtype=int32)

>>> print(tf.constant(7, shape=[1, 3]))
tf.Tensor([[7 7 7]], shape=(1, 3), dtype=int32)

>>> print(tf.constant(7, shape=[1, 10]))
tf.Tensor([[7 7 7 7 7 7 7 7 7 7]], shape=(1, 10), dtype=int32)

shapeの1つ目は1つの配列の中の数を指定してる(valueをその数分用意)
2つ目は↑の配列の数を指定指定してる
っぽい。

dtypeの動きを色々見てみる

うまくいく例

>>> print(tf.constant(7,dtype="int32"))
tf.Tensor(7, shape=(), dtype=int32)
>>> print(tf.constant(7,dtype="int64"))
tf.Tensor(7, shape=(), dtype=int64)
>>> print(tf.constant("テストだよ",dtype="string"))
tf.Tensor(b'\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88\xe3\x81\xa0\xe3\x82\x88', shape=(), dtype=string)

うまくいかない例

>>> print(tf.constant(7,dtype=int32))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'int32' is not defined
>>> print(tf.constant(7,dtype="int1"))
TypeError: Cannot convert value 'int1' to a TensorFlow DType.
>>> print(tf.constant(7,dtype="string"))
TypeError: Cannot convert 7 to EagerTensor of dtype string

dtypeのまとめ

valueの値を変換するというものではなく、valueの値を指定するためのものっぽい。
なので、value値に合わせたdtypeを指定しないとエラーになる。

指定するときは"もしくは'で囲う必要がある。

存在しないdtypeは指定できない。

さいごに

>>> print(tf.constant(7,name="test"))
tf.Tensor(7, shape=(), dtype=int32)

>>> print(tf.constant(7,name=aaa))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aaa' is not defined
>>> 

name色々試しましたが設定されてるのかどうかもよくわからず・・
そもそも定数化してさらに別の名前をつける意味が理解できないので、
今の自分のフェーズできっと使わないと判断してスルーしました。

時間はかかったけど、ここまでは大体把握できたかなという感じ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0