请支持我的博客http://shuaizhang.tech/
原文地址
什么是线性模型
相信大多数人,刚开始接触机器学习的时候,就会接触到线性模型。来看一个简单的例子:通过人的年龄、受教育年数、工作年限等信息,可以预测出一个人的基本收入水平,预测方法就是对前面的限定特征赋予不同的权值,最后计算出工资;此外,线性模型也可以用于分类,例如逻辑回归就是一种典型的线性分类器。
相对于其他的复杂模型来说,线性模型主要有以下几个优点:
- 训练速度快
- 大量特征集的时候工作的很好
- 方便解释和debug,参数调节比较方便
tf.learn关于线性模型的一些API
- FeatureColumn
- sparse_column 用于解决类别特征的稀疏问题,对于类别型的特征,一般使用的One hot方法,会导致矩阵稀疏的问题。
eye_color = tf.contrib.layers.sparse_column_with_keys(
column_name="eye_color", keys=["blue", "brown", "green"])
education = tf.contrib.layers.sparse_column_with_hash_bucket(\
"education", hash_bucket_size=1000)#不知道所有的可能值的时候用这个接口
- Feature Crosses 可以用来合并不同的特征
sport = tf.contrib.layers.sparse_column_with_hash_bucket(\
"sport", hash_bucket_size=1000)
city = tf.contrib.layers.sparse_column_with_hash_bucket(\
"city", hash_bucket_size=1000)
sport_x_city = tf.contrib.layers.crossed_column(
[sport, city], hash_bucket_size=int(1e4))
- Continuous columns 用于连续的变量特征
age = tf.contrib.layers.real_valued_column(“age”)
- Bucketization 将连续的变量变成类别标签
age_buckets = tf.contrib.layers.bucketized_column(age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])
tf.contrib.learn.LinearClassifier和LinearRegressor
这两个一个用于分类,一个用于回归,使用步骤如下
- 创建对象实例,在构造函数中传入featureColumns
- 用fit训练模型
- 用evaluate评估
下面是一段示例代码:
e = tf.contrib.learn.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.fit(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, steps=1)
# Print the stats for the evaluation.
for key in sorted(results):
print "%s: %s" % (key, results[key])
Wide and deep learning
最近刚看了这篇论文,打算专门写一章来详细讲解,这个训练模型的出现是为了结合memorization和generalization。下面推荐几篇文章: