tf.estimator API为TensorFlow中的线性模型提供了一套丰富的工具(除其他外)。 本文档提供了这些工具的概述。 它解释说:
阅读本概述以确定tf.estimator线性模型工具是否对您有用。 然后执行线性模型教程来尝试一下。 本概述使用了本教程中的代码示例,但本教程更详细地介绍了代码。
为了理解这个概述,它将有助于熟悉一些基本的机器学习概念,以及tf.estimator。
[TOC]
一个线性模型使用一个单一的加权和来进行预测。 例如,如果您的年龄,受教育年限和人口的每周工作时间都有数据,您可以学习每个数字的权重,以便他们的加权总和估算出一个人的工资。 您还可以使用线性模型进行分类。
一些线性模型将加权和转换为更方便的形式。 For example, logistic regression plugs the weighted sum into the logistic function to turn the output into a value between 0 and 1. 但是对于每个输入功能,您仍然只有一个权重。
当最近的研究证明更复杂的神经网络具有多层功能时,为什么要使用如此简单的模型?
线性模型:
您可以在TensorFlow中从头开始构建线性模型,而无需特殊API的帮助。 但是tf.estimator提供了一些工具,可以更容易地构建有效的大规模线性模型。
设计线性模型的大部分工作包括将原始数据转换为合适的输入特征。 Tensorflow使用FeatureColumn
抽象来启用这些转换。
一个FeatureColumn
表示数据中的一个特征。 A FeatureColumn
may represent a quantity like 'height', or it may represent a category like 'eye_color' where the value is drawn from a set of discrete possibilities like {'blue', 'brown', 'green'}.
对于像'height'和类别特征 t>这样的连续特征类似'eye_color'的情况,数据中的单个值可能会变成数字序列输入到模型中。 The FeatureColumn
abstraction lets you manipulate the feature as a single semantic unit in spite of this fact. 您可以指定转换并选择要包含的要素,而不用处理模型中张量中的特定索引。
线性模型中的分类特征通常被转换为稀疏向量,其中每个可能的值都具有相应的索引或id。 例如,如果只有三种可能的眼睛颜色,则可以将“eye_color”表示为长度为3的矢量:“棕色”将变为[1,0,0],“蓝色”变为[0,1,0]和“绿色'将变成[0,0,1]。 这些向量被称为“稀疏”,因为它们可能非常长,有很多零,当可能值的集合非常大时(例如所有英文单词)。
虽然不需要使用分类列来使用tf.estimator线性模型,但线性模型的优势之一在于它们处理大型稀疏向量的能力。 稀疏特征是tf.estimator线性模型工具的主要用例。
FeatureColumn
handles the conversion of categorical values into vectors automatically, with code like this:
eye_color = tf.feature_column.categorical_column_with_vocabulary_list(
"eye_color", vocabulary_list=["blue", "brown", "green"])
其中eye_color
是源数据中列的名称。
您还可以为不知道所有可能值的分类要素生成FeatureColumn
。 对于这种情况,您可以使用categorical_column_with_hash_bucket()
,它使用散列函数将索引分配给要素值。
education = tf.feature_column.categorical_column_with_hash_bucket(
"education", hash_bucket_size=1000)
由于线性模型为独立要素分配独立权重,因此无法了解特征值的特定组合的相对重要性。 如果你有一个功能'favorite_sport'和一个功能'home_city',并且你试图预测一个人是否喜欢穿红色,那么你的线性模型将无法从圣路易斯学习棒球迷特别喜欢穿红。
你可以通过创建一个新功能'favorite_sport_x_home_city'来解决这个限制。 这个特征对于给定的人的价值仅仅是两个源特征值的连接:例如,'baseball_x_stlouis'。 这种组合功能被称为特性交叉。
使用crossed_column()
方法可以轻松设置特征十字:
sport_x_city = tf.feature_column.crossed_column(
["sport", "city"], hash_bucket_size=int(1e4))
您可以像这样指定一个连续的功能:
age = tf.feature_column.numeric_column("age")
虽然作为单个实数,连续特征通常可以直接输入到模型中,但Tensorflow也为这种列提供了有用的转换。
Bucketization将连续列转变为分类列。 此转换可让您在特征十字中使用连续特征,或了解特定值范围具有特殊重要性的情况。
分行化将可能值的范围划分为称为桶的子范围:
age_buckets = tf.feature_column.bucketized_column(
age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])
值所在的存储桶将成为该值的分类标签。
FeatureColumn
s provide a specification for the input data for your model, indicating how to represent and transform the data. 但他们不提供数据本身。 您通过输入功能提供数据。
输入函数必须返回张量字典。 每个键对应于FeatureColumn
的名称。 每个键的值都是一个包含所有数据实例的该特征值的张量。 请参阅使用tf.estimator构建输入函数以更全面地了解输入函数,并且线性模型教程代码中的input_fn
的输入功能。
输入函数被传递给train()
和evaluate()
调用,以开始训练和测试,如下一节所述。
Tensorflow估计器类为回归和分类模型提供了统一的培训和评估工具。 他们负责培训和评估循环的细节,并允许用户专注于模型输入和架构。
To build a linear estimator, you can use either the tf.estimator.LinearClassifier
estimator or the tf.estimator.LinearRegressor
estimator, for classification and regression respectively.
与所有张量流量估计器一样,要运行估算器,您只需:
FeatureColumn
的列表传递给构造函数。train()
方法来训练它。evaluate()
方法来看看它是如何工作的。例如:
e = tf.estimator.LinearClassifier(
feature_columns=[
native_country, education, occupation, workclass, marital_status,
race, age_buckets, education_x_occupation,
age_buckets_x_race_x_occupation],
model_dir=YOUR_MODEL_DIRECTORY)
e.train(input_fn=input_fn_train, steps=200)
# Evaluate for one step (one pass through the test data).
results = e.evaluate(input_fn=input_fn_test)
# Print the stats for the evaluation.
for key in sorted(results):
print("%s: %s" % (key, results[key]))
tf.estimator API还提供了一个估算器类,可让您联合训练线性模型和深度神经网络。 这种新颖的方法结合了线性模型“记忆”关键特征和神经网络泛化能力的能力。 使用tf.estimator.DNNLinearCombinedClassifier
创建这种“宽而深”的模型:
e = tf.estimator.DNNLinearCombinedClassifier(
model_dir=YOUR_MODEL_DIR,
linear_feature_columns=wide_columns,
dnn_feature_columns=deep_columns,
dnn_hidden_units=[100, 50])
有关更多信息,请参阅广泛和深度学习教程。