টিএফএল স্তর দিয়ে কেরাস মডেল তৈরি করা

TensorFlow.org এ দেখুন Google Colab-এ চালান GitHub-এ উৎস দেখুন নোটবুক ডাউনলোড করুন

ওভারভিউ

আপনি একঘেয়েমি এবং অন্যান্য আকৃতির সীমাবদ্ধতার সাথে কেরাস মডেলগুলি তৈরি করতে TFL কেরাস স্তরগুলি ব্যবহার করতে পারেন। এই উদাহরণ টিএফএল স্তরগুলি ব্যবহার করে UCI হার্ট ডেটাসেটের জন্য একটি ক্যালিব্রেটেড জালি মডেল তৈরি এবং প্রশিক্ষণ দেয়।

একটি মডেলটির ক্রমাঙ্ক জাফরি মডেল, প্রত্যেক বৈশিষ্ট্য একটি দ্বারা রুপান্তরিত করা হয় tfl.layers.PWLCalibration বা tfl.layers.CategoricalCalibration স্তর এবং ফলাফল nonlinearly একটি ব্যবহার নিলীন হয় tfl.layers.Lattice

সেটআপ

টিএফ ল্যাটিস প্যাকেজ ইনস্টল করা হচ্ছে:

pip install -q tensorflow-lattice pydot

প্রয়োজনীয় প্যাকেজ আমদানি করা হচ্ছে:

import tensorflow as tf

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 (Heart) dataset.
csv_file = tf.keras.utils.get_file(
    'heart.csv', 'http://storage.googleapis.com/download.tensorflow.org/data/heart.csv')
training_data_df = pd.read_csv(csv_file).sample(
    frac=1.0, random_state=41).reset_index(drop=True)
training_data_df.head()

এই নির্দেশিকায় প্রশিক্ষণের জন্য ব্যবহৃত ডিফল্ট মান সেট করা:

LEARNING_RATE = 0.1
BATCH_SIZE = 128
NUM_EPOCHS = 100

অনুক্রমিক কেরাস মডেল

এই উদাহরণটি একটি অনুক্রমিক কেরাস মডেল তৈরি করে এবং শুধুমাত্র TFL স্তরগুলি ব্যবহার করে।

জাফরি স্তর আশা input[i] মধ্যে হতে [0, lattice_sizes[i] - 1.0] , তাই আমরা জাফরি ক্রমাঙ্কন স্তর এগিয়ে মাপ তাই আমরা সঠিকভাবে ক্রমাঙ্কন স্তর আউটপুট পরিসীমা নির্দিষ্ট করতে পারেন সংজ্ঞায়িত করা প্রয়োজন।

# Lattice layer expects input[i] to be within [0, lattice_sizes[i] - 1.0], so
lattice_sizes = [3, 2, 2, 2, 2, 2, 2]

আমরা একটি ব্যবহার tfl.layers.ParallelCombination গ্রুপ একসঙ্গে ক্রমাঙ্কন স্তর যা অর্ডার অনুক্রমিক মডেল তৈরি করতে সক্ষম হবেন করার জন্য সমান্তরাল কার্যকর করা আছে স্তর।

combined_calibrators = tfl.layers.ParallelCombination()

আমরা প্রতিটি বৈশিষ্ট্যের জন্য একটি ক্রমাঙ্কন স্তর তৈরি করি এবং সমান্তরাল সংমিশ্রণ স্তরে এটি যুক্ত করি। সাংখ্যিক বৈশিষ্ট্যের জন্য আমরা ব্যবহার tfl.layers.PWLCalibration এবং শ্রেণীগত বৈশিষ্ট্যগুলির জন্য আমরা ব্যবহার tfl.layers.CategoricalCalibration

# ############### age ###############
calibrator = tfl.layers.PWLCalibration(
    # Every PWLCalibration layer must have keypoints of piecewise linear
    # function specified. Easiest way to specify them is to uniformly cover
    # entire input range by using numpy.linspace().
    input_keypoints=np.linspace(
        training_data_df['age'].min(), training_data_df['age'].max(), num=5),
    # You need to ensure that input keypoints have same dtype as layer input.
    # You can do it by setting dtype here or by providing keypoints in such
    # format which will be converted to desired tf.dtype by default.
    dtype=tf.float32,
    # Output range must correspond to expected lattice input range.
    output_min=0.0,
    output_max=lattice_sizes[0] - 1.0,
)
combined_calibrators.append(calibrator)

# ############### sex ###############
# For boolean features simply specify CategoricalCalibration layer with 2
# buckets.
calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=2,
    output_min=0.0,
    output_max=lattice_sizes[1] - 1.0,
    # Initializes all outputs to (output_min + output_max) / 2.0.
    kernel_initializer='constant')
combined_calibrators.append(calibrator)

# ############### cp ###############
calibrator = tfl.layers.PWLCalibration(
    # Here instead of specifying dtype of layer we convert keypoints into
    # np.float32.
    input_keypoints=np.linspace(1, 4, num=4, dtype=np.float32),
    output_min=0.0,
    output_max=lattice_sizes[2] - 1.0,
    monotonicity='increasing',
    # You can specify TFL regularizers as a tuple ('regularizer name', l1, l2).
    kernel_regularizer=('hessian', 0.0, 1e-4))
combined_calibrators.append(calibrator)

# ############### trestbps ###############
calibrator = tfl.layers.PWLCalibration(
    # Alternatively, you might want to use quantiles as keypoints instead of
    # uniform keypoints
    input_keypoints=np.quantile(training_data_df['trestbps'],
                                np.linspace(0.0, 1.0, num=5)),
    dtype=tf.float32,
    # Together with quantile keypoints you might want to initialize piecewise
    # linear function to have 'equal_slopes' in order for output of layer
    # after initialization to preserve original distribution.
    kernel_initializer='equal_slopes',
    output_min=0.0,
    output_max=lattice_sizes[3] - 1.0,
    # You might consider clamping extreme inputs of the calibrator to output
    # bounds.
    clamp_min=True,
    clamp_max=True,
    monotonicity='increasing')
combined_calibrators.append(calibrator)

# ############### chol ###############
calibrator = tfl.layers.PWLCalibration(
    # Explicit input keypoint initialization.
    input_keypoints=[126.0, 210.0, 247.0, 286.0, 564.0],
    dtype=tf.float32,
    output_min=0.0,
    output_max=lattice_sizes[4] - 1.0,
    # Monotonicity of calibrator can be decreasing. Note that corresponding
    # lattice dimension must have INCREASING monotonicity regardless of
    # monotonicity direction of calibrator.
    monotonicity='decreasing',
    # Convexity together with decreasing monotonicity result in diminishing
    # return constraint.
    convexity='convex',
    # You can specify list of regularizers. You are not limited to TFL
    # regularizrs. Feel free to use any :)
    kernel_regularizer=[('laplacian', 0.0, 1e-4),
                        tf.keras.regularizers.l1_l2(l1=0.001)])
combined_calibrators.append(calibrator)

# ############### fbs ###############
calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=2,
    output_min=0.0,
    output_max=lattice_sizes[5] - 1.0,
    # For categorical calibration layer monotonicity is specified for pairs
    # of indices of categories. Output for first category in pair will be
    # smaller than output for second category.
    #
    # Don't forget to set monotonicity of corresponding dimension of Lattice
    # layer to '1'.
    monotonicities=[(0, 1)],
    # This initializer is identical to default one('uniform'), but has fixed
    # seed in order to simplify experimentation.
    kernel_initializer=tf.keras.initializers.RandomUniform(
        minval=0.0, maxval=lattice_sizes[5] - 1.0, seed=1))
combined_calibrators.append(calibrator)

# ############### restecg ###############
calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=3,
    output_min=0.0,
    output_max=lattice_sizes[6] - 1.0,
    # Categorical monotonicity can be partial order.
    monotonicities=[(0, 1), (0, 2)],
    # Categorical calibration layer supports standard Keras regularizers.
    kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.001),
    kernel_initializer='constant')
combined_calibrators.append(calibrator)

তারপরে আমরা ক্যালিব্রেটরগুলির আউটপুটগুলিকে ননলাইনারলি ফিউজ করার জন্য একটি জালি স্তর তৈরি করি।

উল্লেখ্য যে প্রয়োজনীয় মাত্রার জন্য আমাদের জালির একঘেয়েতা নির্দিষ্ট করতে হবে। ক্রমাঙ্কনে একঘেয়েতার দিকনির্দেশ সহ কম্পোজিশনের ফলে একঘেয়েতার সঠিক এন্ড-টু-এন্ড দিক হবে। এটি ক্যাটেগরিক্যাল ক্যালিব্রেশন স্তরের আংশিক একঘেয়েমি অন্তর্ভুক্ত করে।

lattice = tfl.layers.Lattice(
    lattice_sizes=lattice_sizes,
    monotonicities=[
        'increasing', 'none', 'increasing', 'increasing', 'increasing',
        'increasing', 'increasing'
    ],
    output_min=0.0,
    output_max=1.0)

তারপরে আমরা সম্মিলিত ক্যালিব্রেটর এবং জালি স্তর ব্যবহার করে একটি অনুক্রমিক মডেল তৈরি করতে পারি।

model = tf.keras.models.Sequential()
model.add(combined_calibrators)
model.add(lattice)

প্রশিক্ষণ অন্যান্য কেরাস মডেলের মতোই কাজ করে।

features = training_data_df[[
    'age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg'
]].values.astype(np.float32)
target = training_data_df[['target']].values.astype(np.float32)

model.compile(
    loss=tf.keras.losses.mean_squared_error,
    optimizer=tf.keras.optimizers.Adagrad(learning_rate=LEARNING_RATE))
model.fit(
    features,
    target,
    batch_size=BATCH_SIZE,
    epochs=NUM_EPOCHS,
    validation_split=0.2,
    shuffle=False,
    verbose=0)

model.evaluate(features, target)
10/10 [==============================] - 0s 1ms/step - loss: 0.1551
0.15506614744663239

কার্যকরী কেরাস মডেল

এই উদাহরণ কেরাস মডেল নির্মাণের জন্য একটি কার্যকরী API ব্যবহার করে।

হিসাবে পূর্বের বিভাগে উল্লেখ করা হয়েছে, জাফরি স্তর আশা input[i] মধ্যে হতে [0, lattice_sizes[i] - 1.0] , তাই আমরা ক্রমাঙ্কন স্তর এগিয়ে জাফরি মাপ নির্ধারণ করতে তাই আমরা সঠিকভাবে আউটপুট পরিসীমা নির্দিষ্ট করতে পারেন প্রয়োজন ক্রমাঙ্কন স্তর.

# We are going to have 2-d embedding as one of lattice inputs.
lattice_sizes = [3, 2, 2, 3, 3, 2, 2]

প্রতিটি বৈশিষ্ট্যের জন্য, আমাদের একটি ক্যালিব্রেশন স্তর অনুসরণ করে একটি ইনপুট স্তর তৈরি করতে হবে। সাংখ্যিক বৈশিষ্ট্যের জন্য আমরা ব্যবহার tfl.layers.PWLCalibration এবং শ্রেণীগত বৈশিষ্ট্যগুলির জন্য আমরা ব্যবহার tfl.layers.CategoricalCalibration

model_inputs = []
lattice_inputs = []
# ############### age ###############
age_input = tf.keras.layers.Input(shape=[1], name='age')
model_inputs.append(age_input)
age_calibrator = tfl.layers.PWLCalibration(
    # Every PWLCalibration layer must have keypoints of piecewise linear
    # function specified. Easiest way to specify them is to uniformly cover
    # entire input range by using numpy.linspace().
    input_keypoints=np.linspace(
        training_data_df['age'].min(), training_data_df['age'].max(), num=5),
    # You need to ensure that input keypoints have same dtype as layer input.
    # You can do it by setting dtype here or by providing keypoints in such
    # format which will be converted to desired tf.dtype by default.
    dtype=tf.float32,
    # Output range must correspond to expected lattice input range.
    output_min=0.0,
    output_max=lattice_sizes[0] - 1.0,
    monotonicity='increasing',
    name='age_calib',
)(
    age_input)
lattice_inputs.append(age_calibrator)

# ############### sex ###############
# For boolean features simply specify CategoricalCalibration layer with 2
# buckets.
sex_input = tf.keras.layers.Input(shape=[1], name='sex')
model_inputs.append(sex_input)
sex_calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=2,
    output_min=0.0,
    output_max=lattice_sizes[1] - 1.0,
    # Initializes all outputs to (output_min + output_max) / 2.0.
    kernel_initializer='constant',
    name='sex_calib',
)(
    sex_input)
lattice_inputs.append(sex_calibrator)

# ############### cp ###############
cp_input = tf.keras.layers.Input(shape=[1], name='cp')
model_inputs.append(cp_input)
cp_calibrator = tfl.layers.PWLCalibration(
    # Here instead of specifying dtype of layer we convert keypoints into
    # np.float32.
    input_keypoints=np.linspace(1, 4, num=4, dtype=np.float32),
    output_min=0.0,
    output_max=lattice_sizes[2] - 1.0,
    monotonicity='increasing',
    # You can specify TFL regularizers as tuple ('regularizer name', l1, l2).
    kernel_regularizer=('hessian', 0.0, 1e-4),
    name='cp_calib',
)(
    cp_input)
lattice_inputs.append(cp_calibrator)

# ############### trestbps ###############
trestbps_input = tf.keras.layers.Input(shape=[1], name='trestbps')
model_inputs.append(trestbps_input)
trestbps_calibrator = tfl.layers.PWLCalibration(
    # Alternatively, you might want to use quantiles as keypoints instead of
    # uniform keypoints
    input_keypoints=np.quantile(training_data_df['trestbps'],
                                np.linspace(0.0, 1.0, num=5)),
    dtype=tf.float32,
    # Together with quantile keypoints you might want to initialize piecewise
    # linear function to have 'equal_slopes' in order for output of layer
    # after initialization to preserve original distribution.
    kernel_initializer='equal_slopes',
    output_min=0.0,
    output_max=lattice_sizes[3] - 1.0,
    # You might consider clamping extreme inputs of the calibrator to output
    # bounds.
    clamp_min=True,
    clamp_max=True,
    monotonicity='increasing',
    name='trestbps_calib',
)(
    trestbps_input)
lattice_inputs.append(trestbps_calibrator)

# ############### chol ###############
chol_input = tf.keras.layers.Input(shape=[1], name='chol')
model_inputs.append(chol_input)
chol_calibrator = tfl.layers.PWLCalibration(
    # Explicit input keypoint initialization.
    input_keypoints=[126.0, 210.0, 247.0, 286.0, 564.0],
    output_min=0.0,
    output_max=lattice_sizes[4] - 1.0,
    # Monotonicity of calibrator can be decreasing. Note that corresponding
    # lattice dimension must have INCREASING monotonicity regardless of
    # monotonicity direction of calibrator.
    monotonicity='decreasing',
    # Convexity together with decreasing monotonicity result in diminishing
    # return constraint.
    convexity='convex',
    # You can specify list of regularizers. You are not limited to TFL
    # regularizrs. Feel free to use any :)
    kernel_regularizer=[('laplacian', 0.0, 1e-4),
                        tf.keras.regularizers.l1_l2(l1=0.001)],
    name='chol_calib',
)(
    chol_input)
lattice_inputs.append(chol_calibrator)

# ############### fbs ###############
fbs_input = tf.keras.layers.Input(shape=[1], name='fbs')
model_inputs.append(fbs_input)
fbs_calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=2,
    output_min=0.0,
    output_max=lattice_sizes[5] - 1.0,
    # For categorical calibration layer monotonicity is specified for pairs
    # of indices of categories. Output for first category in pair will be
    # smaller than output for second category.
    #
    # Don't forget to set monotonicity of corresponding dimension of Lattice
    # layer to '1'.
    monotonicities=[(0, 1)],
    # This initializer is identical to default one ('uniform'), but has fixed
    # seed in order to simplify experimentation.
    kernel_initializer=tf.keras.initializers.RandomUniform(
        minval=0.0, maxval=lattice_sizes[5] - 1.0, seed=1),
    name='fbs_calib',
)(
    fbs_input)
lattice_inputs.append(fbs_calibrator)

# ############### restecg ###############
restecg_input = tf.keras.layers.Input(shape=[1], name='restecg')
model_inputs.append(restecg_input)
restecg_calibrator = tfl.layers.CategoricalCalibration(
    num_buckets=3,
    output_min=0.0,
    output_max=lattice_sizes[6] - 1.0,
    # Categorical monotonicity can be partial order.
    monotonicities=[(0, 1), (0, 2)],
    # Categorical calibration layer supports standard Keras regularizers.
    kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.001),
    kernel_initializer='constant',
    name='restecg_calib',
)(
    restecg_input)
lattice_inputs.append(restecg_calibrator)

তারপরে আমরা ক্যালিব্রেটরগুলির আউটপুটগুলিকে ননলাইনারলি ফিউজ করার জন্য একটি জালি স্তর তৈরি করি।

উল্লেখ্য যে প্রয়োজনীয় মাত্রার জন্য আমাদের জালির একঘেয়েতা নির্দিষ্ট করতে হবে। ক্রমাঙ্কনে একঘেয়েতার দিকনির্দেশ সহ কম্পোজিশনের ফলে একঘেয়েতার সঠিক এন্ড-টু-এন্ড দিক হবে। এই আংশিক monotonicity অন্তর্ভুক্ত tfl.layers.CategoricalCalibration স্তর।

lattice = tfl.layers.Lattice(
    lattice_sizes=lattice_sizes,
    monotonicities=[
        'increasing', 'none', 'increasing', 'increasing', 'increasing',
        'increasing', 'increasing'
    ],
    output_min=0.0,
    output_max=1.0,
    name='lattice',
)(
    lattice_inputs)

মডেলটিতে আরও নমনীয়তা যোগ করতে, আমরা একটি আউটপুট ক্রমাঙ্কন স্তর যুক্ত করি।

model_output = tfl.layers.PWLCalibration(
    input_keypoints=np.linspace(0.0, 1.0, 5),
    name='output_calib',
)(
    lattice)

আমরা এখন ইনপুট এবং আউটপুট ব্যবহার করে একটি মডেল তৈরি করতে পারি।

model = tf.keras.models.Model(
    inputs=model_inputs,
    outputs=model_output)
tf.keras.utils.plot_model(model, rankdir='LR')

png

প্রশিক্ষণ অন্যান্য কেরাস মডেলের মতোই কাজ করে। মনে রাখবেন, আমাদের সেটআপের সাথে, ইনপুট বৈশিষ্ট্যগুলি পৃথক টেনসর হিসাবে পাস করা হয়।

feature_names = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg']
features = np.split(
    training_data_df[feature_names].values.astype(np.float32),
    indices_or_sections=len(feature_names),
    axis=1)
target = training_data_df[['target']].values.astype(np.float32)

model.compile(
    loss=tf.keras.losses.mean_squared_error,
    optimizer=tf.keras.optimizers.Adagrad(LEARNING_RATE))
model.fit(
    features,
    target,
    batch_size=BATCH_SIZE,
    epochs=NUM_EPOCHS,
    validation_split=0.2,
    shuffle=False,
    verbose=0)

model.evaluate(features, target)
10/10 [==============================] - 0s 1ms/step - loss: 0.1590
0.15900751948356628