Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/io/tutorials/orc.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

Apache ORC リーダー

概要

Apache ORC は、一般的な列型ストレージ形式です。tensorflow-io パッケージは、Apache ORC ファイルを読み取るデフォルトの実装を提供します。

MNIST モデルをビルドする

必要なパッケージをインストールし、ランタイムを再起動する

!pip install tensorflow-io
import tensorflow as tf import tensorflow_io as tfio

ORC でサンプルデータセットファイルをダウンロードする

ここで使用するデータセットは、UCI の Iris データセットです。データセットには、それぞれ 50 インスタンスの 3 つのクラスが含まれ、各クラスはアヤメの植物のタイプを参照します。これには、(1)がく片の長さ、(2)がく片の幅、(3)花びらの長さ、(4)花びらの幅の、4つの属性があり、最後の列にはクラスラベルが含まれています。

!curl -OL https://github.com/tensorflow/io/raw/master/tests/test_orc/iris.orc !ls -l iris.orc

ファイルからデータセットを作成する

dataset = tfio.IODataset.from_orc("iris.orc", capacity=15).batch(1)

データセットを調べます。

for item in dataset.take(1): print(item)

Iris データセットに基づく ORC データセットを使用した tf.keras モデルトレーニングのエンドツーエンドの例を見ていきましょう。

データの前処理をする

どの列が特徴で、どの列がラベルであるかを構成します。

feature_cols = ["sepal_length", "sepal_width", "petal_length", "petal_width"] label_cols = ["species"] # select feature columns feature_dataset = tfio.IODataset.from_orc("iris.orc", columns=feature_cols) # select label columns label_dataset = tfio.IODataset.from_orc("iris.orc", columns=label_cols)

モデルトレーニングのために種を浮動小数点数にマッピングする util 関数:

vocab_init = tf.lookup.KeyValueTensorInitializer( keys=tf.constant(["virginica", "versicolor", "setosa"]), values=tf.constant([0, 1, 2], dtype=tf.int64)) vocab_table = tf.lookup.StaticVocabularyTable( vocab_init, num_oov_buckets=4)
label_dataset = label_dataset.map(vocab_table.lookup) dataset = tf.data.Dataset.zip((feature_dataset, label_dataset)) dataset = dataset.batch(1) def pack_features_vector(features, labels): """Pack the features into a single array.""" features = tf.stack(list(features), axis=1) return features, labels dataset = dataset.map(pack_features_vector)

モデルをビルド、コンパイル、およびトレーニングする

これで、モデルを作成してトレーニングする準備が整いました。処理したデータセットからアヤメ植物のクラスを予測するために、3 つのレイヤーの keras モデルを構築します。

model = tf.keras.Sequential( [ tf.keras.layers.Dense( 10, activation=tf.nn.relu, input_shape=(4,) ), tf.keras.layers.Dense(10, activation=tf.nn.relu), tf.keras.layers.Dense(3), ] ) model.compile(optimizer="adam", loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=["accuracy"]) model.fit(dataset, epochs=5)