ここでの「分類」は,データから,そのラベル(クラス名など)を求めるもの. 分類のために,教師データを用いて事前学習を行う.
TensorFlow データセットは,事前学習に役立つ. このページでは,TensorFlow データセットの中の Iris データセット,Titanic データセットを紹介する. 利用条件は利用者で確認すること.
参考Webページ:
このページの内容は,Google Colab でも実行できる.
そのために,次の URL で,Google Colab のノートブックを準備している.
次のリンクをクリックすると,Google Colab のノートブックが開く. そして,Google アカウントでログインすると,Google Colab のノートブック内のコードを実行することができる.Google Colab のノートブックは書き換えて使うこともできる.このとき,書き換え後のものを,各自の Google ドライブ内に保存することもできる.
https://colab.research.google.com/drive/10u8owk1y9l-OocyenRZuDb0sKxxicVwK?usp=sharing
Google Colab を使うか,パソコンを使う.それぞれの場合の前準備を説明する.
https://colab.research.google.com
Google Colab はオンラインの Python 開発環境. 使用するには Google アカウントが必要
TensorFlow を使う場合は,必要となる NVIDIA CUDA ツールキット,NVIDIA cuDNN のバージョン確認
TensorFlow は,そのバージョンによって,必要となるNVIDIA CUDA ツールキット,NVIDIA cuDNN のバージョンが違う(最新の NVIDIA CUDA ツールキット,NVIDIA cuDNN で動くというわけでない). そのことは,https://www.tensorflow.org/install/gpu で確認できる.
そこで, まずは,使用したい TensorFlow のバージョンを確認し,それにより, NVIDIA CUDA ツールキット,NVIDIA cuDNN を確認する.
NVIDIA CUDA ツールキットのバージョン:
指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
NVIDIA cuDNN のバージョン:
その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
NVIDIA CUDA は,NVIDIA社が提供している GPU 用のプラットフォームである.
インストール手順の説明
関連 Web ページ
インストール手順の説明
端末で,次のコマンドを実行.
sudo apt -y install python3-dev python3-pip python3-setuptools python3-venv sudo pip3 uninstall ptyprocess sniffio terminado tornado jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder sudo apt -y install jupyter jupyter-qtconsole spyder3 sudo apt -y install python3-ptyprocess python3-sniffio python3-terminado python3-tornado sudo pip3 install -U jupyterlab nteract_on_jupyter sudo pip3 uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets tensorflow-hub keras sudo pip3 uninstall six wheel astunparse tensorflow-estimator numpy keras-preprocessing absl-py wrapt gast flatbuffers grpcio opt-einsum protobuf termcolor typing-extensions google-pasta h5py tensorboard-plugin-wit markdown werkzeug requests-oauthlib rsa cachetools google-auth google-auth-oauthlib tensorboard tensorflow sudo apt -y install python3-six python3-wheel python3-numpy python3-grpcio python3-protobuf python3-termcolor python3-typing-extensions python3-h5py python3-markdown python3-werkzeug python3-requests-oauthlib python3-rsa python3-cachetools python3-google-auth sudo apt -y install python3-numpy python3-pil python3-pydot python3-matplotlib python3-keras python3-keras-applications python3-keras-preprocessing sudo pip3 install -U tensorflow tf-models-official tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer opencv-python sudo pip3 install git+https://github.com/tensorflow/docs sudo pip3 install git+https://github.com/tensorflow/examples.git
詳細は: 別ページで説明している.
Ubuntu では,システムの Python を使うことができる(その場合,Python のインストールは行わない)
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
tf.enable_v2_behavior()
from tensorflow.keras import backend as K
K.clear_session()
iris, iris_info = tfds.load('iris', with_info = True, shuffle_files=True, as_supervised=True)
print(iris_info) print(iris_info.features["label"].num_classes) print(iris_info.features["label"].names)
ロード時に「as_supervised=False」としたときは,「features, label = data['features'], data['label']」
ds_train = iris['train']
it = ds_train.cache().shuffle(1000).batch(128).prefetch(tf.data.experimental.AUTOTUNE)
for data in it.take(1):
features, label = data[0], data[1]
print(features)
print(label)
train = tfds.as_dataframe(iris['train'], iris_info) print(train)
行数は len(<データフレーム>), 属性数は len(<データフレーム>.columns)
print(len(train)) print(len(train.columns))
train = tfds.as_dataframe(iris['train'].take(10), iris_info) print(train)
行数は len(<データフレーム>), 属性数は len(<データフレーム>.columns)
print(len(train)) print(len(train.columns))
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
tf.enable_v2_behavior()
from tensorflow.keras import backend as K
K.clear_session()
titanic, titanic_info = tfds.load('titanic', with_info = True, shuffle_files=True, as_supervised=True)
print(titanic_info)
ロード時に「as_supervised=False」としたときは,「features, survived = data['features'], data['survived']」
ds_train = titanic['train']
it = ds_train.cache().shuffle(1000).batch(128).prefetch(tf.data.experimental.AUTOTUNE)
for data in it.take(1):
features, survived = data[0], data[1]
print(features)
print(survived)
train = tfds.as_dataframe(titanic['train'], titanic_info) print(train)
行数は len(<データフレーム>), 属性数は len(<データフレーム>.columns)
print(len(train)) print(len(train.columns))
train = tfds.as_dataframe(titanic['train'].take(10), titanic_info) print(train)
行数は len(<データフレーム>), 属性数は len(<データフレーム>.columns)
print(len(train)) print(len(train.columns))