TensorFlowとPythonを使ってStack Overflow質問データセットのテキストデータをベクトル化する方法
TensorFlowはGoogleが提供する機械学習フレームワークです。オープンソースとして公開されており、Pythonと組み合わせてアルゴリズムやディープラーニングアプリケーションの実装など、幅広い用途に活用されています。研究開発から本番運用まで対応しており、複雑な数学的演算を高速に実行できる最適化技術を備えているのが特徴です。
TensorFlowの主な特徴
TensorFlowは内部でNumPyと多次元配列を使用しています。この多次元配列は「テンソル」と呼ばれ、フレームワークの中核となる概念です。主な特徴は以下の通りです。
- ディープニューラルネットワークの構築と学習をサポート
- 高いスケーラビリティと豊富な定番データセットを同梱
- GPU計算への対応とリソース管理の自動化
- 多数の機械学習ライブラリと充実したドキュメント
これらにより、深層ニューラルネットワークモデルの実行やトレーニングを行い、データセットの特徴を予測するアプリケーションを簡単に作成できます。
インストール方法
「tensorflow」パッケージは、Windows環境であれば以下のコマンド1行でインストールできます。
pip install tensorflow
テンソルとは何か
テンソルはTensorFlowで使われる基本的なデータ構造であり、データフローグラフと呼ばれるフロー図のエッジを接続する役割を担います。実体は多次元配列、あるいはリストそのものです。
なお、この記事のコードはGoogle Colaboratory上で実行しています。Google Colabはブラウザ上でPythonコードを実行できるサービスで、面倒な初期設定が不要なうえ、GPU(グラフィックス処理ユニット)にも無料でアクセス可能です。Jupyter Notebookをベースに構築されています。
コード例:テキストデータのベクトル化
以下は、テキストデータをベクトル化するためのコード例です。
print("The vectorize function is defined")
def int_vectorize_text(text, label):
text = tf.expand_dims(text, -1)
return int_vectorize_layer(text), label
print(" A batch of the dataset is retrieved")
text_batch, label_batch = next(iter(raw_train_ds))
first_question, first_label = text_batch[0], label_batch[0]
print("Question is : ", first_question)
print("Label is : ", first_label)
print("'binary' vectorized question is :",
binary_vectorize_text(first_question, first_label)[0])
print("'int' vectorized question is :",
int_vectorize_text(first_question, first_label)[0])コード出典:https://www.tensorflow.org/tutorials/load_data/text
出力結果
The vectorize function is defined
A batch of the dataset is retrieved
Question is : tf.Tensor(b'"function expected error in blank for dynamically created check box
when it is clicked i want to grab the attribute value.it is working in ie 8,9,10 but not working in ie
11,chrome shows function expected error..<input type=checkbox checked=\'checked\'
id=\'symptomfailurecodeid\' tabindex=\'54\' style=\'cursor:pointer;\' onclick=chkclickevt(this);
failurecodeid=""1"" >...function chkclickevt(obj) { .
alert(obj.attributes(""failurecodeid""));.}"\n', shape=(), dtype=string)
Label is : tf.Tensor(2, shape=(), dtype=int32)
'binary' vectorized question is : tf.Tensor([[1. 1. 1. ... 0. 0. 0.]], shape=(1, 10000), dtype=float32)
'int' vectorized question is : tf.Tensor(
[[ 37 464 65 7 16 12 879 262 181 448 44 10 6 700
3 46 4 2085 2 473 1 6 156 7 478 1 25 20
156 7 478 1 499 37 464 1 1846 1666 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0]], shape=(1, 250), dtype=int64)コードの解説
binaryモードでは、トークンが存在するかどうかを示す配列が返されます。
intモードでは、各トークンが整数に置き換えられます。この方式では単語の出現順序が保持される点が大きな特徴です。
まず、ベクトル化用の関数を定義します。
データセットからサンプルを取り出してベクトル化し、「binary」モードと「int」モードそれぞれの結果をコンソールに表示します。
対象レイヤーに対して「get_vocabulary」メソッドを呼び出すことで、整数インデックスに対応する文字列を逆引きすることも可能です。
-
TensorFlowとPythonを使ってデータセットを視覚化する方法
ここでは、花のデータセットを使用します。このデータセットは、Googleが提供するAPI経由でダウンロードでき、「get_file」メソッドにAPIのURLを引数として渡すことで、データを開発環境に取得できます。 ダウンロードしたデータは「matplotlib」ライブラリを使って視覚化できます。「imshow」メソッドを使用すると、コンソール上に画像を表示することが可能です。 TensorFlowとKerasの連携 関連記事: TensorFlowとは何か?KerasはどのようにTensorFlowと連携してニューラルネットワークを構築するのか? 本記事ではKeras Sequential
-
TensorFlowとPythonを使って花のデータセットを可視化する方法
花のデータセットは、matplotlibライブラリを使用することで簡単に可視化できます。imshowメソッドを使うと、画像をコンソール上に表示でき、データセット全体を反復処理しながら最初の数枚の画像だけを出力するといったことも可能です。使用するデータセットについて本記事では、数千枚の花の画像を含む「flowers(花)」データセットを使用します。このデータセットには5つのサブディレクトリが含まれており、各クラス(花の種類)ごとに1つのサブディレクトリが割り当てられています。なお、以下のコードはGoogle Colaboratoryで実行することを想定しています。Google Colab(Col