20210220のTensorFlowに関する記事は3件です。

初心者がTensorFlowを使ってみる〜TensorFlowの基本構文を学んでいく〜(2日目)

今日勉強していくこと

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

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

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

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

python開始とtensorflowのimport

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

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

定数の定義

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

連想配列はちょっとややこしかったので一回ここでおわり。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

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

今日勉強していくこと

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

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

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

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

初心者がTensorFlowを使ってみる〜TensorFlowのインストール〜(1日目)

はじめに

現状の知識

TensorFlow面白いよと言われたけど、機械学習できるの?機械学習って結局なんなん?っていうくらいわかってない。

pythonはスクレイピングで使う程度で、使いこなせてないよ。(PHPは仕事で使ってるのでプログラミング初心者というわけではないけど)

勉強の進め方

Qiitaにメモしつつ、ゆっくり進めていくので、多分果てしない時間がかかる。
あと暇なタイミングで少しずつ進めるので、進みは遅いはずです。詰まったところはしっかり調べていく。

目標

とりあえずなんか作る!そんでなにができるものなのか把握しとく(今は何ができるのかわからない)


勉強スタート


TensorFlowのインストールとエラー対応

とりあえず機械学習できるやつなんでしょ?っていうのはわかったので、まずインストールしてくよ。
[公式]https://www.tensorflow.org/install/pip?hl=ja
これのページに書いてある下記コマンドを実行

インストール

pip install --upgrade tensorflow

インストール確認

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

エラー出ました(Could not import the lzma module.)

 UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  warnings.warn(msg)

原因

Could not import the lzma module.

lzmaがないよっていうことらしい。

対応方法

lzmaを入れて、pythonのアンインストール+再インストールをしなくちゃいけないとのこと。
下記のようにbrewで入るらしいので、一旦入れてみる

brew install xz

このあとpyenvでアンインストールと、再インストールを行うとのこと。

アンインストール
pyenv uninstall 3.7.5
再インストール
pyenv install 3.7.5

またpipで入れたライブラリが消えてしまった。。辛い。

もう一回インストールと確認

インストール
pip install --upgrade tensorflow

確認

確認
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

2021-02-20 00:00:12.799993: 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.
tf.Tensor(-796.5173, shape=(), dtype=float32)

AVX2 FMA使いたかったら再構築しろよ!みたいなことが書いてあるけど、どうやらスルーしておkなやつらしい。
速度がちょっと遅いとかそれくらい。
なのでこれで環境構築終了!!

最後に

なんかpythonでGUI使う時もだけどしょっちゅう再インストールして、pipでパッケージを入れ直してって繰り返してる気がする・・。
site-packageのディレクトリにpipで入れたパッケージファイルまとまってるんだけどこれバックアップして上書きすればいいんだろか?

もう少しやりたかったけど、AWS障害であわあわして疲れちゃったので今日(1日目)は勉強おわり。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む