Memo

メモ > 技術 > プログラミング言語: Python > TensorFlow(ディープラーニング)

■TensorFlow(ディープラーニング)
TensorFlow(テンソルフロー)は物体認識の分野で圧倒的な性能を示すようになり、 今は画像・音声・自然言語処理など、さまざまな分野で活用されている TensorFlow https://www.tensorflow.org/ Googleがオープンソースで公開している機械学習ライブラリで、機械学習やディープラーニングを実践できる Gmailでの迷惑メール除去にも使われているらしい スパムはいらない ―― TensorFlow を使って Gmail から新たに 1 億以上のスパム メッセージを除去 | Google Cloud Blog https://cloud.google.com/blog/ja/products/gcp/ridding-gmail-of-100-million-more-spam-messages-with-t... ■テンソル ディープラーニングの数学「スカラー・ベクトル・行列・テンソル」とは? | あぱーブログ https://blog.apar.jp/deep-learning/12121/ 「テンソル」「ベクトル」「行列」とは?ディープラーニングの情報整理のカラクリ 連載:図でわかる3分間AIキソ講座|ビジネス+IT https://www.sbbit.jp/article/cont1/63580 ディープラーニングでは、複雑なニューラルネットワーク上で膨大な数の数値がやりとりされる コンピュータではこれらの数値を扱うことができるが、数値の羅列だと人間には理解しづらく、技術の応用や発展も難しい そこで使われるようになったのが「テンソル(Tensor)」という数学的概念 基本的には「たくさんの数値を集めて、1つの情報として表現する」のがテンソル スカラーは単なる数値 スカラーを縦もしくは横に並べたものをベクトルという スカラーを縦横に並べたものを行列という 上記も含めて、スカラーを縦横斜めなど複数に並べたものをまとめて「テンソル」と呼ぶ これはプログラムの配列をイメージすると解りやすい 0次元テンソル … 0次元の配列(スカラー) 1次元テンソル … 1次元の配列(ベクトル) 2次元テンソル … 2次元の配列(行列) 3次元テンソル … 3次元の配列 4次元テンソル … 4次元の配列 X次元テンソル … X次元の配列 例えば2次元の配列なら「行列」という呼び方があるが、3次元以上になると決まった呼び方がない よって「3次元のテンソル」や「3階のテンソル」などと呼ぶ TensorFlowでは、グラフを構成するすべてのものをテンソルとして扱う ■TensorFlowのインストール
$ sudo apt install libatlas-base-dev $ sudo pip3 install tensorflow
Raspberry Pi 4 4B-32GB の環境ではインストールできた Raspberry Pi 3 Model b+ の環境では以下のエラーになったが、「sudo pip3 install tensorflow」を3回目でインストールに成功した
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting tensorflow Downloading https://www.piwheels.org/simple/tensorflow/tensorflow-1.13.1-cp37-none-linux_armv7l.whl (93.2MB) 85% |■■■■■■■■■■■■■■ | 79.8MB 2.3MB/s eta 0:00:06 THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them. tensorflow from https://www.piwheels.org/simple/tensorflow/tensorflow-1.13.1-cp37-none-linux_armv7l.whl#sha256=25f4f... Expected sha256 25f4ff027beec1e568baf8e90a07bad59d354560533d6b37318b9efeb70beeb1 Got 4d215130b13d9606e91a2327383cf98e603e9e53294c2d68c59e8b5224e8a923
以下で動作確認
$ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow as tf >>> sess = tf.Session() >>> hello = tf.constant('Hello.') >>> print(sess.run(hello)) b'Hello.' >>> exit()
Raspberry Pi 4 4B-32GB の環境では「import tensorflow as tf」を実行したときに以下の警告が表示されたが動作した
2021-02-06 14:47:10.086584: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory
Raspberry Pi 3 Model b+ の環境では「import tensorflow as tf」を実行したときに以下の警告が表示されたが動作した Pythonのバージョン次第で表示されないらしいが、いったんこのまま進める
WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.AttrValue is deprecated. Please use tf.compat.v1.AttrValue instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.COMPILER_VERSION is deprecated. Please use tf.version.COMPILER_VERSION instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.CXX11_ABI_FLAG is deprecated. Please use tf.sysconfig.CXX11_ABI_FLAG instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.ConditionalAccumulator is deprecated. Please use tf.compat.v1.ConditionalAccumulator instead.
Raspberry PiにTensorFlowをインストールして基本的な分類サンプル実行まで試した - Qiita https://qiita.com/hidakanoko/items/5292ca79e3ff53867e40 RaspberryPiで機械学習TensorFlowセットアップ - Qiita https://qiita.com/totoshi/items/7eb690926eebafdb49ff Raspbian に公式対応した TensorFlow を入れてみたら警告が出たのでどうにかしてみたって話 - Qiita https://qiita.com/ktooi/items/48a8d18839cae7246c0b ■TensorFlowのインストール(CentOS7) 以下などを試したがインストールできず
# pip3 install tensorflow # pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl # pip3 install tensorflow-gpu==2.4 # pip3 install tensorflow-gpu==1.14.0 # yum -y groupinstall "Base" "Development tools" # pip3 install tensorflow # yum -y install python3-devel # pip3 install tensorflow # pip3 install tensorflow-gpu==1.14.0 # pip3 install tensorflow==2.0.0b1
TensorFlow のインストール https://docs.vmware.com/jp/VMware-vSphere-Bitfusion/3.0/Example-Guide/GUID-1C053853-4D83-4D94-A6F3-D... 以下のエラーメッセージによると、「-std=gnu99」のオプションが問題になっているようだが対応方法が判らず
# pip3 install tensorflow==2.0.0b1 gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -D_WIN32_WINNT=1536 -DGPR_BACKWARDS_COMPATIBILITY_MODE=1 -DHAVE_CONFIG_H=1 -DGRPC_ENABLE_FORK_SUPPORT=1 -DPyMODINIT_FUNC=extern "C" __attribute__((visibility ("default"))) PyObject* -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1 -Isrc/python/grpcio -Iinclude -I. -Ithird_party/abseil-cpp -Ithird_party/address_sorting/include -Ithird_party/cares -Ithird_party/cares/cares -Ithird_party/cares/config_linux -Ithird_party/re2 -Ithird_party/boringssl-with-bazel/src/include -Ithird_party/upb -Isrc/core/ext/upb-generated -Isrc/core/ext/upbdefs-generated -Ithird_party/xxhash -Ithird_party/zlib -I/usr/include/python3.6m -c src/python/grpcio/grpc/_cython/cygrpc.cpp -o python_build/temp.linux-x86_64-3.6/src/python/grpcio/grpc/_cython/cygrpc.o -std=c++11 -std=gnu99 -fvisibility=hidden -fno-wrapv -fno-exceptions -pthread cc1plus: warning: command line option ‘-std=gnu99’ is valid for C/ObjC but not for C++ [enabled by default] 〜中略〜 Traceback (most recent call last): File "/tmp/pip-build-nppnb9ij/grpcio/src/python/grpcio/commands.py", line 286, in build_extensions build_ext.build_ext.build_extensions(self) File "/usr/lib64/python3.6/distutils/command/build_ext.py", line 448, in build_extensions self._build_extensions_serial() File "/usr/lib64/python3.6/distutils/command/build_ext.py", line 473, in _build_extensions_serial self.build_extension(ext) File "/usr/local/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 202, in build_extension _build_ext.build_extension(self, ext) File "/usr/lib64/python3.6/distutils/command/build_ext.py", line 533, in build_extension depends=ext.depends) File "/usr/lib64/python3.6/distutils/ccompiler.py", line 574, in compile self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) File "/usr/lib64/python3.6/distutils/unixccompiler.py", line 129, in _compile raise CompileError(msg) distutils.errors.CompileError: command 'gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nppnb9ij/grpcio/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-dqgchcji-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-nppnb9ij/grpcio/
-std=c99と-std=gnu99は同じ? - bugfix https://pneumaster.hatenadiary.org/entry/20081224/p1 ■足し算を行う
import tensorflow as tf # 定数を定義 a = tf.constant(100) b = tf.constant(30) # 演算を定義 add_op = a + b # セッションを開始する sess = tf.Session() # 式を評価する res = sess.run(add_op) print(res)
■データフローログ
$ sudo mkdir logs $ sudo chown smbuser. logs $ sudo chmod 0777 logs
プログラムを作成する
import tensorflow as tf # 定数を定義 a = tf.constant(10, name='10') b = tf.constant(20, name='20') c = tf.constant(30, name='30') # 演算を定義 add_op = tf.add(a, b, name='add') mul_op = tf.multiply(add_op, c, name='mul') # セッションを開始する sess = tf.Session() # 式を評価する res = sess.run(mul_op) print(res) # TensorBoardのためにグラフを出力 tf.summary.FileWriter('./logs', sess.graph)
プログラム実行後、tensorboard コマンドでデータフローグラフを表示できる
$ python3 test_mul_tb.py $ tensorboard --logdir=./logs 2021-02-06 15:06:32.558996: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all TensorBoard 2.0.2 at http://localhost:6006/ (Press CTRL+C to quit)
Raspberry Pi にGUIでアクセスし、ブラウザを起動して以下にアクセスすると「TensorBoard」が表示された http://localhost:6006/ Raspberry Pi 3 Model b+ の環境では以下のエラーになって動作しなかった
$ tensorboard --logdir=./logs WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.AttrValue is deprecated. Please use tf.compat.v1.AttrValue instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.COMPILER_VERSION is deprecated. Please use tf.version.COMPILER_VERSION instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.CXX11_ABI_FLAG is deprecated. Please use tf.sysconfig.CXX11_ABI_FLAG instead. WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py:98: The name tf.ConditionalAccumulator is deprecated. Please use tf.compat.v1.ConditionalAccumulator instead. Traceback (most recent call last): File "/usr/local/bin/tensorboard", line 6, in <module> from tensorboard.main import run_main File "/usr/local/lib/python3.7/dist-packages/tensorboard/main.py", line 44, in <module> from tensorboard import default File "/usr/local/lib/python3.7/dist-packages/tensorboard/default.py", line 44, in <module> from tensorboard.plugins.interactive_inference import interactive_inference_plugin File "/usr/local/lib/python3.7/dist-packages/tensorboard/plugins/interactive_inference/interactive_inference_plugin.py", line 27, in <module> from grpc.framework.interfaces.face.face import AbortionError File "/usr/local/lib/python3.7/dist-packages/grpc/__init__.py", line 23, in <module> from grpc._cython import cygrpc as _cygrpc ImportError: /usr/local/lib/python3.7/dist-packages/grpc/_cython/cygrpc.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_exchange_8
■TensorFlowでアヤメの分類 TensorFlowでは、ラベルデータを「One-Hotベクトル」という形式で表す必要がある これは「1つだけHight(1)の状態で、他はLow(0)の状態であるようなビット列のことを指す」 iris.csv は「Pandas(データ解析支援)でアヤメの分類」で使用したものと同じ
import tensorflow as tf import pandas as pd from sklearn.model_selection import train_test_split # アヤメデータの読み込み iris_data = pd.read_csv("iris.csv", encoding="utf-8") # アヤメデータをラベルと入力データに分離する y_labels = iris_data.loc[:,"Name"] x_data = iris_data.loc[:,["SepalLength","SepalWidth","PetalLength","PetalWidth"]] # ラベルデータをone-hotベクトルに直す labels = { 'Iris-setosa': [1, 0, 0], 'Iris-versicolor': [0, 1, 0], 'Iris-virginica': [0, 0, 1] } y_nums = list(map(lambda v : labels[v] , y_labels)) # 学習用とテスト用に分離する x_train, x_test, y_train, y_test = train_test_split( x_data, y_nums, train_size=0.8) # アヤメデータの入力値(4次元)と出力値(3次元)を入れる場所を定義 x = tf.placeholder(tf.float32, [None, 4]) y_ = tf.placeholder(tf.float32, [None, 3]) # 重みとバイアスのための変数を定義 w = tf.Variable(tf.zeros([4, 3])) # 重み b = tf.Variable(tf.zeros([3])) # バイアス # ソフトマックス回帰を定義 y = tf.nn.softmax(tf.matmul(x, w) + b) # モデルを訓練する cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) optimizer = tf.train.AdamOptimizer(0.05) train = optimizer.minimize(cross_entropy) # 正解率を求める predict = tf.equal(tf.argmax(y, 1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(predict, tf.float32)) # 変数を初期化 init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # 変数を初期化 init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # 学習を行う train_feed_dict = {x: x_train, y_: y_train} for step in range(300): sess.run(train, feed_dict=train_feed_dict) # テストデータで最終的な正解率を求める acc = sess.run(accuracy, feed_dict={x: x_test, y_: y_test}) print("正解率=", acc)
以下のとおり実行する
$ python3.7 iris.py 2021-02-20 12:43:47.841792: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory WARNING:tensorflow:From iris.py:25: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. WARNING:tensorflow:From iris.py:36: The name tf.log is deprecated. Please use tf.math.log instead. WARNING:tensorflow:From iris.py:37: The name tf.train.AdamOptimizer is deprecated. Please use tf.compat.v1.train.AdamOptimizer instead. WARNING:tensorflow:From iris.py:45: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead. WARNING:tensorflow:From iris.py:46: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead. 正解率= 0.96666664
「E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132」のエラーが表示されているが、実行に問題は無いようなのでいったん無視しておく Pythonおよび機械学習勉強用のRaspberryPiの構築 (RaspberryPi4 & Buster版 (RaspberryPi3も可)) - Qiita https://qiita.com/rhene/items/d370a3b3f05a7248bd79 Raspberry Pi 4 で機械学習系のパッケージを入れる(numpy, scipy, pandas, sklearn, matplotlib, tensorflow, keras) - 工作と競馬2 https://dekuo-03.hatenablog.jp/entry/2020/02/01/000000_1

Advertisement