Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/lattice/tutorials/canned_estimators.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.

TF Lattice の缶詰 Estimator

警告: 新しいコードには Estimators は推奨されません。Estimators は v1.Session スタイルのコードを実行しますが、これは正しく記述するのはより難しく、特に TF 2 コードと組み合わせると予期しない動作をする可能性があります。Estimators は、互換性保証の対象となりますが、セキュリティの脆弱性以外の修正は行われません。詳細については、移行ガイドを参照してください。

概要

缶詰 Estimator は、典型的な使用事例向けの TFL モデルを素早く簡単にトレーニングする方法です。このガイドでは、TFL 缶詰 Estimator の作成に必要な手順を説明します。

セットアップ

TF Lattice パッケージをインストールします。

#@test {"skip": true} !pip install tensorflow-lattice

必要なパッケージをインポートします。

import tensorflow as tf import copy import logging import numpy as np import pandas as pd import sys import tensorflow_lattice as tfl from tensorflow import feature_column as fc logging.disable(sys.maxsize)

UCI Statlog(心臓)データセットをダウンロードします。

csv_file = tf.keras.utils.get_file( 'heart.csv', 'http://storage.googleapis.com/download.tensorflow.org/data/heart.csv') df = pd.read_csv(csv_file) target = df.pop('target') train_size = int(len(df) * 0.8) train_x = df[:train_size] train_y = target[:train_size] test_x = df[train_size:] test_y = target[train_size:] df.head()

このガイドのトレーニングに使用されるデフォルト値を設定します。

LEARNING_RATE = 0.01 BATCH_SIZE = 128 NUM_EPOCHS = 500 PREFITTING_NUM_EPOCHS = 10

特徴量カラム

ほかの TF Estimator と同様に、データを Estimator に渡す必要があります。通常、input_fn によって行われ、FeatureColumns を使用して解析されます。

# Feature columns. # - age # - sex # - cp chest pain type (4 values) # - trestbps resting blood pressure # - chol serum cholestoral in mg/dl # - fbs fasting blood sugar > 120 mg/dl # - restecg resting electrocardiographic results (values 0,1,2) # - thalach maximum heart rate achieved # - exang exercise induced angina # - oldpeak ST depression induced by exercise relative to rest # - slope the slope of the peak exercise ST segment # - ca number of major vessels (0-3) colored by flourosopy # - thal 3 = normal; 6 = fixed defect; 7 = reversable defect feature_columns = [ fc.numeric_column('age', default_value=-1), fc.categorical_column_with_vocabulary_list('sex', [0, 1]), fc.numeric_column('cp'), fc.numeric_column('trestbps', default_value=-1), fc.numeric_column('chol'), fc.categorical_column_with_vocabulary_list('fbs', [0, 1]), fc.categorical_column_with_vocabulary_list('restecg', [0, 1, 2]), fc.numeric_column('thalach'), fc.categorical_column_with_vocabulary_list('exang', [0, 1]), fc.numeric_column('oldpeak'), fc.categorical_column_with_vocabulary_list('slope', [0, 1, 2]), fc.numeric_column('ca'), fc.categorical_column_with_vocabulary_list( 'thal', ['normal', 'fixed', 'reversible']), ]

TFL 缶詰 Estimator は、どの種類の較正レイヤーを使用するかを決定するために特徴量カラムの種類を使用します。数値特徴量カラムには tfl.layers.PWLCalibration レイヤーを使用し、分類特徴量カラムには tfl.layers.CategoricalCalibration レイヤーを使用します。

分類特徴量カラムは、埋め込み特徴量カラムによってラッピングされていないことに注意してください。これらは Estimator に直接フィードされます。

input_fn を作成する

ほかの Estimator と同様に、トレーニング用と評価用のモデルにデータをフィードするために input_fn を使用することができます。TFL Estimator は特徴量の分位数を自動的に計算し、PWL 較正レイヤーの入力キーポイントとして使用します。これを行うには、feature_analysis_input_fn を渡す必要があります。トレーニング input_fn に似てはいますが、単一のエポックまたはデータのサブサンプルを使用します。

train_input_fn = tf.compat.v1.estimator.inputs.pandas_input_fn( x=train_x, y=train_y, shuffle=False, batch_size=BATCH_SIZE, num_epochs=NUM_EPOCHS, num_threads=1) # feature_analysis_input_fn is used to collect statistics about the input. feature_analysis_input_fn = tf.compat.v1.estimator.inputs.pandas_input_fn( x=train_x, y=train_y, shuffle=False, batch_size=BATCH_SIZE, # Note that we only need one pass over the data. num_epochs=1, num_threads=1) test_input_fn = tf.compat.v1.estimator.inputs.pandas_input_fn( x=test_x, y=test_y, shuffle=False, batch_size=BATCH_SIZE, num_epochs=1, num_threads=1) # Serving input fn is used to create saved models. serving_input_fn = ( tf.estimator.export.build_parsing_serving_input_receiver_fn( feature_spec=fc.make_parse_example_spec(feature_columns)))

特徴量の構成

特徴量の較正と特徴量あたりの構成は tfl.configs.FeatureConfig によって設定します。特徴量の構成には、単調性制約、特徴量あたりの正則化(tfl.configs.RegularizerConfig を参照)、および格子モデルの格子のサイズが含まれます。

入力特徴量の構成が定義されない場合、tfl.config.FeatureConfig のデフォルトの構成が使用されます。

# Feature configs are used to specify how each feature is calibrated and used. feature_configs = [ tfl.configs.FeatureConfig( name='age', lattice_size=3, # By default, input keypoints of pwl are quantiles of the feature. pwl_calibration_num_keypoints=5, monotonicity='increasing', pwl_calibration_clip_max=100, # Per feature regularization. regularizer_configs=[ tfl.configs.RegularizerConfig(name='calib_wrinkle', l2=0.1), ], ), tfl.configs.FeatureConfig( name='cp', pwl_calibration_num_keypoints=4, # Keypoints can be uniformly spaced. pwl_calibration_input_keypoints='uniform', monotonicity='increasing', ), tfl.configs.FeatureConfig( name='chol', # Explicit input keypoint initialization. pwl_calibration_input_keypoints=[126.0, 210.0, 247.0, 286.0, 564.0], monotonicity='increasing', # Calibration can be forced to span the full output range by clamping. pwl_calibration_clamp_min=True, pwl_calibration_clamp_max=True, # Per feature regularization. regularizer_configs=[ tfl.configs.RegularizerConfig(name='calib_hessian', l2=1e-4), ], ), tfl.configs.FeatureConfig( name='fbs', # Partial monotonicity: output(0) <= output(1) monotonicity=[(0, 1)], ), tfl.configs.FeatureConfig( name='trestbps', pwl_calibration_num_keypoints=5, monotonicity='decreasing', ), tfl.configs.FeatureConfig( name='thalach', pwl_calibration_num_keypoints=5, monotonicity='decreasing', ), tfl.configs.FeatureConfig( name='restecg', # Partial monotonicity: output(0) <= output(1), output(0) <= output(2) monotonicity=[(0, 1), (0, 2)], ), tfl.configs.FeatureConfig( name='exang', # Partial monotonicity: output(0) <= output(1) monotonicity=[(0, 1)], ), tfl.configs.FeatureConfig( name='oldpeak', pwl_calibration_num_keypoints=5, monotonicity='increasing', ), tfl.configs.FeatureConfig( name='slope', # Partial monotonicity: output(0) <= output(1), output(1) <= output(2) monotonicity=[(0, 1), (1, 2)], ), tfl.configs.FeatureConfig( name='ca', pwl_calibration_num_keypoints=4, monotonicity='increasing', ), tfl.configs.FeatureConfig( name='thal', # Partial monotonicity: # output(normal) <= output(fixed) # output(normal) <= output(reversible) monotonicity=[('normal', 'fixed'), ('normal', 'reversible')], ), ]

較正済みの線形モデル

TFL 缶詰 Estimator を構築するには、tfl.configs からモデル構成を構築します。較正された線形モデルは、tfl.configs.CalibratedLinearConfig を使用して構築されます。これは、ピースごとに線形で分類上の較正を入力特徴量に適用し、その後で、線形の組み合わせとオプションの出力ピース単位線形較正を適用します。出力較正を使用する場合、または出力境界が指定されている場合、線形レイヤーは加重平均を較正済みの入力に適用します。

この例では、最初の 5 つの特徴量で較正済みの線形モデルを作成します。キャリブレータプロットでモデルグラフを描画するために、tfl.visualization を使用します。

# Model config defines the model structure for the estimator. model_config = tfl.configs.CalibratedLinearConfig( feature_configs=feature_configs, use_bias=True, output_calibration=True, regularizer_configs=[ # Regularizer for the output calibrator. tfl.configs.RegularizerConfig(name='output_calib_hessian', l2=1e-4), ]) # A CannedClassifier is constructed from the given model config. estimator = tfl.estimators.CannedClassifier( feature_columns=feature_columns[:5], model_config=model_config, feature_analysis_input_fn=feature_analysis_input_fn, optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), config=tf.estimator.RunConfig(tf_random_seed=42)) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) print('Calibrated linear test AUC: {}'.format(results['auc'])) saved_model_path = estimator.export_saved_model(estimator.model_dir, serving_input_fn) model_graph = tfl.estimators.get_model_graph(saved_model_path) tfl.visualization.draw_model_graph(model_graph)

較正済みの格子モデル

較正済みの格子モデルは、tfl.configs.CalibratedLatticeConfig を使って構築されます。較正済みの格子モデルは、入力特徴量にピース単位の線形と分類上の較正を適用し、その後で、格子モデルとオプションの出力ピース単位線形較正を適用します。

この例では、最初の 5 つの特徴量で較正済みの格子モデルを作成します。

# This is calibrated lattice model: Inputs are calibrated, then combined # non-linearly using a lattice layer. model_config = tfl.configs.CalibratedLatticeConfig( feature_configs=feature_configs, regularizer_configs=[ # Torsion regularizer applied to the lattice to make it more linear. tfl.configs.RegularizerConfig(name='torsion', l2=1e-4), # Globally defined calibration regularizer is applied to all features. tfl.configs.RegularizerConfig(name='calib_hessian', l2=1e-4), ]) # A CannedClassifier is constructed from the given model config. estimator = tfl.estimators.CannedClassifier( feature_columns=feature_columns[:5], model_config=model_config, feature_analysis_input_fn=feature_analysis_input_fn, optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), config=tf.estimator.RunConfig(tf_random_seed=42)) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) print('Calibrated lattice test AUC: {}'.format(results['auc'])) saved_model_path = estimator.export_saved_model(estimator.model_dir, serving_input_fn) model_graph = tfl.estimators.get_model_graph(saved_model_path) tfl.visualization.draw_model_graph(model_graph)

較正済みの格子アンサンブル

特徴量の数が大きい場合、アンサンブルモデルを使用できます。アンサンブルモデルは、単一の大型の格子を作成する代わりに、特徴量のサブセットにより小さな格子を作成し、その出力を平均化します。アンサンブル格子モデルは、tfl.configs.CalibratedLatticeEnsembleConfig を使って作成します。較正済み格子アンサンブルモデルは、特徴量にピース単位の線形および分類上の較正を適用し、その後で、格子モデルのアンサンブルとオプションの出力ピース単位線形較正を適用します。

ランダムな格子アンサンブル

次のモデル構成は、各格子のランダムな特徴量サブセットを使用します。

# This is random lattice ensemble model with separate calibration: # model output is the average output of separately calibrated lattices. model_config = tfl.configs.CalibratedLatticeEnsembleConfig( feature_configs=feature_configs, num_lattices=5, lattice_rank=3) # A CannedClassifier is constructed from the given model config. estimator = tfl.estimators.CannedClassifier( feature_columns=feature_columns, model_config=model_config, feature_analysis_input_fn=feature_analysis_input_fn, optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), config=tf.estimator.RunConfig(tf_random_seed=42)) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) print('Random ensemble test AUC: {}'.format(results['auc'])) saved_model_path = estimator.export_saved_model(estimator.model_dir, serving_input_fn) model_graph = tfl.estimators.get_model_graph(saved_model_path) tfl.visualization.draw_model_graph(model_graph, calibrator_dpi=15)

RTL レイヤーのランダムな格子アンサンブル

次のモデル構成は、各格子のランダムな特徴量サブセットを使用する tfl.layers.RTL レイヤーを使用します。tfl.layers.RTL は単調性制約のみをサポートしており、すべての特徴量と特徴量ごとの正則化において同じ格子サイズが必要です。tfl.layers.RTL レイヤーを使用すると、別の tfl.layers.Lattice インスタンスを使用する場合よりもさらに大きなアンサンブルにスケーリングすることができます。

# Make sure our feature configs have the same lattice size, no per-feature # regularization, and only monotonicity constraints. rtl_layer_feature_configs = copy.deepcopy(feature_configs) for feature_config in rtl_layer_feature_configs: feature_config.lattice_size = 2 feature_config.unimodality = 'none' feature_config.reflects_trust_in = None feature_config.dominates = None feature_config.regularizer_configs = None # This is RTL layer ensemble model with separate calibration: # model output is the average output of separately calibrated lattices. model_config = tfl.configs.CalibratedLatticeEnsembleConfig( lattices='rtl_layer', feature_configs=rtl_layer_feature_configs, num_lattices=5, lattice_rank=3) # A CannedClassifier is constructed from the given model config. estimator = tfl.estimators.CannedClassifier( feature_columns=feature_columns, model_config=model_config, feature_analysis_input_fn=feature_analysis_input_fn, optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), config=tf.estimator.RunConfig(tf_random_seed=42)) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) print('Random ensemble test AUC: {}'.format(results['auc'])) saved_model_path = estimator.export_saved_model(estimator.model_dir, serving_input_fn) model_graph = tfl.estimators.get_model_graph(saved_model_path) tfl.visualization.draw_model_graph(model_graph, calibrator_dpi=15)

Crystals 格子アンサンブル

TFL は、Crystals と呼ばれるヒューリスティックな特徴量配置アルゴリズムも提供しています。Crystals アルゴリズムはまず、ペアでの特徴量の相互作用を推定する事前適合モデルをトレーニングします。次に、同じ格子内により多くの非線形相互作用を持つ特徴量が存在するように、最終的なアンサンブルを配置します。

Crystals モデルでは、前述のとおり、事前適合モデルのトレーニングに使用される prefitting_input_fn も提供する必要があります。事前適合モデルは完全にトレーニングされている必要はないため、数エポックのみで十分だといえます。

prefitting_input_fn = tf.compat.v1.estimator.inputs.pandas_input_fn( x=train_x, y=train_y, shuffle=False, batch_size=BATCH_SIZE, num_epochs=PREFITTING_NUM_EPOCHS, num_threads=1)

その他とで、モデル構成で lattice='crystals' を設定すると、Crystal モデルを作成することができます。

# This is Crystals ensemble model with separate calibration: model output is # the average output of separately calibrated lattices. model_config = tfl.configs.CalibratedLatticeEnsembleConfig( feature_configs=feature_configs, lattices='crystals', num_lattices=5, lattice_rank=3) # A CannedClassifier is constructed from the given model config. estimator = tfl.estimators.CannedClassifier( feature_columns=feature_columns, model_config=model_config, feature_analysis_input_fn=feature_analysis_input_fn, # prefitting_input_fn is required to train the prefitting model. prefitting_input_fn=prefitting_input_fn, optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), prefitting_optimizer=tf.keras.optimizers.legacy.Adam(LEARNING_RATE), config=tf.estimator.RunConfig(tf_random_seed=42)) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) print('Crystals ensemble test AUC: {}'.format(results['auc'])) saved_model_path = estimator.export_saved_model(estimator.model_dir, serving_input_fn) model_graph = tfl.estimators.get_model_graph(saved_model_path) tfl.visualization.draw_model_graph(model_graph, calibrator_dpi=15)

tfl.visualization モジュールを使用して、より詳細に特徴量キャリブレータを描画することができます。

_ = tfl.visualization.plot_feature_calibrator(model_graph, "age") _ = tfl.visualization.plot_feature_calibrator(model_graph, "restecg")