def build_estimator(model_dir, nbuckets, hidden_units): """ Build an estimator starting from INPUT COLUMNS. These include feature transformations and synthetic features. The model is a wide-and-deep model. """ # input columns (dayofweek, hourofday, latdiff, londiff, euclidean, plon, plat, dlon, dlat, pcount) = INPUT_COLUMNS # bucketize the lats & lons latbuckets = np.linspace(38.0, 42.0, nbuckets).tolist() lonbuckets = np.linspace(-76.0, -72.0, nbuckets).tolist() b_plat = layers.bucketized_column(plat, latbuckets) b_dlat = layers.bucketized_column(dlat, latbuckets) b_plon = layers.bucketized_column(plon, lonbuckets) b_dlon = layers.bucketized_column(dlon, lonbuckets) # feature cross ploc = layers.crossed_column([b_plat, b_plon], nbuckets*nbuckets) dloc = layers.crossed_column([b_dlat, b_dlon], nbuckets*nbuckets) pd_pair = layers.crossed_column([ploc, dloc], nbuckets ** 4 ) day_hr = layers.crossed_column([dayofweek, hourofday], 24*7) # Wide columns and deep columns. wide_columns = [ # feature crosses dloc, ploc, pd_pair, day_hr, # sparse columns dayofweek, hourofday, # anything with a linear relationship pcount ] deep_columns = [ # embedding_column to "group" together ... layers.embedding_column(pd_pair, 10), layers.embedding_column(day_hr, 10), # real_valued_column plat, plon, dlat, dlon, latdiff, londiff, euclidean ] return tf.contrib.learn.DNNLinearCombinedRegressor( model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=hidden_units or [128, 32, 4])
def wide_and_deep(output_dir, nbuckets=5, hidden_units='64,16,4', learning_rate=0.01): real, sparse = get_features() hidden_units = hidden_units.split(',') hidden_units = list(map(int, hidden_units)) print(".........................", hidden_units) # bucketise/discretise lat and lon to nbuckets latbuckets = np.linspace(20.0, 50.0, nbuckets).tolist() # USA lonbuckets = np.linspace(-120.0, -70.0, nbuckets).tolist() # USA disc = {} disc.update({ 'd_{}'.format(key): tflayers.bucketized_column(real[key], latbuckets) \ for key in ['dep_lat', 'arr_lat'] }) disc.update({ 'd_{}'.format(key): tflayers.bucketized_column(real[key], lonbuckets) \ for key in ['dep_lon', 'arr_lon'] }) # cross columns for new features sparse['dep_loc'] = tflayers.crossed_column( [disc['d_dep_lat'], disc['d_dep_lon']], nbuckets * nbuckets) sparse['arr_loc'] = tflayers.crossed_column( [disc['d_arr_lat'], disc['d_arr_lon']], nbuckets * nbuckets) sparse['dep_arr'] = tflayers.crossed_column( [sparse['dep_loc'], sparse['arr_loc']], nbuckets**4) sparse['ori_dest'] = tflayers.crossed_column( [sparse['origin'], sparse['dest']], hash_bucket_size=1000) # checkpoint # create embeddings of all the sparse columns embed = { colname : create_embed(col) \ for colname, col in sparse.items() } real.update(embed) estimator = tflearn.DNNLinearCombinedClassifier( model_dir=output_dir, linear_feature_columns=sparse.values(), dnn_feature_columns=real.values(), dnn_hidden_units=hidden_units) estimator.params["head"]._thresholds = [0.7] return estimator
def build_estimator(model_dir, embedding_size=8, hidden_units=None): (gender, race, education, marital_status, relationship, workclass, occupation, native_country, age, education_num, capital_gain, capital_loss, hours_per_week) = INPUT_COLUMNS """Build an estimator.""" # Sparse base columns. # Reused Transformations. age_buckets = layers.bucketized_column( age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65]) # Wide columns and deep columns. wide_columns = [ layers.crossed_column([education, occupation], hash_bucket_size=int(1e4)), layers.crossed_column([age_buckets, race, occupation], hash_bucket_size=int(1e6)), layers.crossed_column([native_country, occupation], hash_bucket_size=int(1e4)), gender, native_country, education, occupation, workclass, marital_status, relationship, age_buckets, ] deep_columns = [ layers.embedding_column(workclass, dimension=embedding_size), layers.embedding_column(education, dimension=embedding_size), layers.embedding_column(marital_status, dimension=embedding_size), layers.embedding_column(gender, dimension=embedding_size), layers.embedding_column(relationship, dimension=embedding_size), layers.embedding_column(race, dimension=embedding_size), layers.embedding_column(native_country, dimension=embedding_size), layers.embedding_column(occupation, dimension=embedding_size), age, education_num, capital_gain, capital_loss, hours_per_week, ] return tf.contrib.learn.DNNLinearCombinedClassifier( model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=hidden_units or [100, 70, 50, 25])
def build_estimator(model_dir, model_type): """build an estimator""" # base sparse feature process gender = layers.sparse_column_with_keys(column_name='gender', keys=['female', 'male']) education = layers.sparse_column_with_hash_bucket(column_name='education', hash_bucket_size=1000) relationship = layers.sparse_column_with_hash_bucket(column_name='relationship', hash_bucket_size=100) workclass = layers.sparse_column_with_hash_bucket(column_name='workclass', hash_bucket_size=100) occupation = layers.sparse_column_with_hash_bucket(column_name='occupation', hash_bucket_size=1000) native_country = layers.sparse_column_with_hash_bucket(column_name='native_country', hash_bucket_size=1000) # base continuous feature age = layers.real_valued_column(column_name='age') education_num = layers.real_valued_column(column_name='education_num') capital_gain = layers.real_valued_column(column_name='capital_gain') capital_loss = layers.real_valued_column(column_name='capital_loss') hours_per_week = layers.real_valued_column(column_name='hours_per_week') # transformation.bucketization 将连续变量转化为类别标签。从而提高我们的准确性 age_bucket = layers.bucketized_column(source_column=age, boundaries=[18, 25, 30, 35, 40, 45,50, 55, 60, 65]) # wide columns and deep columns # 深度模型使用到的特征和广度模型使用到的特征 # 广度模型特征只只用到了分类标签 wide_columns = [gender, native_country, education, relationship, workclass, occupation, age_bucket, layers.crossed_column(columns=[education, occupation], hash_bucket_size=int(1e4)), layers.crossed_column(columns=[age_bucket, education, occupation], hash_bucket_size=int(1e6)), layers.crossed_column(columns=[native_country, occupation], hash_bucket_size=int(1e4))] deep_columns = [layers.embedding_column(workclass, dimension=8), layers.embedding_column(education, dimension=8), layers.embedding_column(gender, dimension=8), layers.embedding_column(relationship, dimension=8), layers.embedding_column(native_country, dimension=8), layers.embedding_column(occupation, dimension=8), age, education_num, capital_gain, capital_loss, hours_per_week] if model_type == "wide": m=learn.LinearClassifier(feature_columns=wide_columns, model_dir=model_dir) elif model_type == "deep": m=learn.DNNClassifier(feature_columns=deep_columns, model_dir=model_dir, hidden_units=[100, 50]) else: m=learn.DNNLinearCombinedClassifier(model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=[256, 128, 64], dnn_activation_fn=tf.nn.relu) return m
def wide_and_deep_model(output_dir, nbuckets=5, hidden_units='64,32', learning_rate=0.01): real, sparse = get_features() # the lat/lon columns can be discretized to yield "air traffic corridors" latbuckets = np.linspace(20.0, 50.0, nbuckets).tolist() # USA lonbuckets = np.linspace(-120.0, -70.0, nbuckets).tolist() # USA disc = {} disc.update({ 'd_{}'.format(key) : tflayers.bucketized_column(real[key], latbuckets) \ for key in ['dep_lat', 'arr_lat'] }) disc.update({ 'd_{}'.format(key) : tflayers.bucketized_column(real[key], lonbuckets) \ for key in ['dep_lon', 'arr_lon'] }) # cross columns that make sense in combination sparse['dep_loc'] = tflayers.crossed_column([disc['d_dep_lat'], disc['d_dep_lon']],\ nbuckets*nbuckets) sparse['arr_loc'] = tflayers.crossed_column([disc['d_arr_lat'], disc['d_arr_lon']],\ nbuckets*nbuckets) sparse['dep_arr'] = tflayers.crossed_column([sparse['dep_loc'], sparse['arr_loc']],\ nbuckets ** 4) sparse['ori_dest'] = tflayers.crossed_column([sparse['origin'], sparse['dest']], \ hash_bucket_size=1000) # create embeddings of all the sparse columns embed = { colname : create_embed(col) \ for colname, col in sparse.items() } real.update(embed) estimator = \ tflearn.DNNLinearCombinedClassifier(model_dir=output_dir, linear_feature_columns=sparse.values(), dnn_feature_columns=real.values(), dnn_hidden_units=parse_hidden_units(hidden_units)) #linear_optimizer=tf.train.FtrlOptimizer(learning_rate=learning_rate), #dnn_optimizer=tf.train.AdagradOptimizer(learning_rate=learning_rate*0.25)) estimator.params["head"]._thresholds = [0.7] # FIXME: hack return estimator
cluster_frequency_buckets = bucketized_column(cluster_frequency, boundaries=[0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]) country_code = sparse_column_with_hash_bucket("country_code", hash_bucket_size=500) country_rank = real_valued_column("country_rank") edit_distance = sparse_column_with_keys(column_name="edit_distance", keys=["0", "1", "2"]) has_pcode = sparse_column_with_keys(column_name="has_pcode", keys=["True", "False"]) has_mpoly = sparse_column_with_keys(column_name="has_mpoly", keys=["True", "False"]) is_country = sparse_column_with_keys(column_name="is_country", keys=["True", "False"]) is_lowest_admin_level = sparse_column_with_keys(column_name="is_lowest_admin_level", keys=["True", "False"]) is_highest_population = sparse_column_with_keys(column_name="is_highest_population", keys=["True", "False"]) matches_topic = sparse_column_with_keys(column_name="matches_topic", keys=["True", "False"]) median_distance = real_valued_column("median_distance") median_distance_buckets = bucketized_column(median_distance, boundaries=[10,50,100,200,300]) population = real_valued_column("population") population_buckets = bucketized_column(population, boundaries=[0, 1, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000]) popularity = real_valued_column("popularity") admin_level_x_median_distance = crossed_column([admin_level, median_distance_buckets], hash_bucket_size=int(1e4)) admin_level_x_cluster_frequency = crossed_column([admin_level, cluster_frequency_buckets], hash_bucket_size=int(1e4)) admin_level_x_country_code = crossed_column([admin_level, country_code], hash_bucket_size=int(1e4)) #number_of_other_points_in_country = sparse_column_with_hash_bucket("number_of_other_points_in_country", hash_bucket_size=1000) #number_of_other_points_in_admin1 = sparse_column_with_hash_bucket("number_of_other_points_in_admin1", hash_bucket_size=1000) #number_of_other_points_in_admin2 = sparse_column_with_hash_bucket("number_of_other_points_in_admin2", hash_bucket_size=1000) #feature_columns = [admin_level, cluster_frequency_buckets, country_code, country_rank, edit_distance, is_lowest_admin_level, has_mpoly, has_pcode, matches_topic, median_distance, median_distance_buckets, population_buckets, popularity, admin_level_x_cluster_frequency, admin_level_x_country_code, admin_level_x_median_distance] #print "feature_columns:", feature_columns wide_columns = [admin_level, cluster_frequency_buckets, country_code, country_rank, edit_distance, is_country, is_highest_population, is_lowest_admin_level, has_mpoly, has_pcode, matches_topic, median_distance, median_distance_buckets, population_buckets, popularity, admin_level_x_cluster_frequency, admin_level_x_country_code, admin_level_x_median_distance] deep_columns = [ embedding_column(admin_level, dimension=8), cluster_frequency, cluster_frequency_buckets, embedding_column(country_code, dimension=8), country_rank,
def build_estimator(model_dir, embedding_size=8, hidden_units=None): """Build a wide and deep model for predicting income category. Wide and deep models use deep neural nets to learn high level abstractions about complex features or interactions between such features. These models then combined the outputs from the DNN with a linear regression performed on simpler features. This provides a balance between power and speed that is effective on many structured data problems. You can read more about wide and deep models here: https://research.googleblog.com/2016/06/wide-deep-learning-better-together-with.html To define model we can use the prebuilt DNNCombinedLinearClassifier class, and need only define the data transformations particular to our dataset, and then assign these (potentially) transformed features to either the DNN, or linear regression portion of the model. Args: model_dir: str, the model directory used by the Classifier for checkpoints summaries and exports. embedding_size: int, the number of dimensions used to represent categorical features when providing them as inputs to the DNN. hidden_units: [int], the layer sizes of the DNN (input layer first) Returns: A DNNCombinedLinearClassifier """ (actividad, anio, bueno, dia, lugar, mes, pais) = INPUT_COLUMNS """Build an estimator.""" # Reused Transformations. # Continuous columns can be converted to categorical via bucketization mes_bucket = layers.bucketized_column( mes, boundaries=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # Wide columns and deep columns. wide_columns = [ # Interactions between different categorical features can also # be added as new virtual features. layers.crossed_column([actividad, lugar], hash_bucket_size=int(1e4)), layers.crossed_column([actividad, mes_bucket], hash_bucket_size=int(1e4)), layers.crossed_column([actividad, dia], hash_bucket_size=int(1e4)), layers.crossed_column([actividad, pais], hash_bucket_size=int(1e4)), actividad, dia, lugar, mes_bucket, pais, ] deep_columns = [ layers.embedding_column(actividad, dimension=embedding_size), layers.embedding_column(lugar, dimension=embedding_size), layers.embedding_column(dia, dimension=embedding_size), layers.embedding_column(pais, dimension=embedding_size), anio, mes, bueno, ] return tf.contrib.learn.DNNLinearCombinedClassifier( model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=hidden_units or [100, 70, 50, 25])
def build_estimator(model_dir, nbuckets, hidden_units): """ Build an estimator starting from INPUT COLUMNS. These include feature transformations and synthetic features. The model is a wide-and-deep model. """ # input columns (dayofweek, hourofday, latdiff, londiff, euclidean, plon, plat, dlon, dlat, pcount) = INPUT_COLUMNS # bucketize the lats & lons latbuckets = np.linspace(38.0, 42.0, nbuckets).tolist() lonbuckets = np.linspace(-76.0, -72.0, nbuckets).tolist() b_plat = layers.bucketized_column(plat, latbuckets) b_dlat = layers.bucketized_column(dlat, latbuckets) b_plon = layers.bucketized_column(plon, lonbuckets) b_dlon = layers.bucketized_column(dlon, lonbuckets) # feature cross ploc = layers.crossed_column([b_plat, b_plon], nbuckets * nbuckets) dloc = layers.crossed_column([b_dlat, b_dlon], nbuckets * nbuckets) pd_pair = layers.crossed_column([ploc, dloc], nbuckets**4) day_hr = layers.crossed_column([dayofweek, hourofday], 24 * 7) # Wide columns and deep columns. wide_columns = [ # feature crosses dloc, ploc, pd_pair, day_hr, # sparse columns dayofweek, hourofday, # anything with a linear relationship pcount ] deep_columns = [ # embedding_column to "group" together ... layers.embedding_column(pd_pair, 10), layers.embedding_column(day_hr, 10), # real_valued_column plat, plon, dlat, dlon, latdiff, londiff, euclidean ] return tf.contrib.learn.DNNLinearCombinedRegressor( model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=hidden_units or [128, 32, 4])
def build_estimator(model_dir, embedding_size=8, hidden_units=None): """Build a wide and deep model for predicting income category. Wide and deep models use deep neural nets to learn high level abstractions about complex features or interactions between such features. These models then combined the outputs from the DNN with a linear regression performed on simpler features. This provides a balance between power and speed that is effective on many structured data problems. You can read more about wide and deep models here: https://research.googleblog.com/2016/06/wide-deep-learning-better-together-with.html To define model we can use the prebuilt DNNCombinedLinearClassifier class, and need only define the data transformations particular to our dataset, and then assign these (potentially) transformed features to either the DNN, or linear regression portion of the model. Args: model_dir: str, the model directory used by the Classifier for checkpoints summaries and exports. embedding_size: int, the number of dimensions used to represent categorical features when providing them as inputs to the DNN. hidden_units: [int], the layer sizes of the DNN (input layer first) Returns: A DNNCombinedLinearClassifier """ (gender, race, education, marital_status, relationship, workclass, occupation, native_country, age, education_num, capital_gain, capital_loss, hours_per_week) = INPUT_COLUMNS """Build an estimator.""" # Reused Transformations. # Continuous columns can be converted to categorical via bucketization age_buckets = layers.bucketized_column( age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65]) # Wide columns and deep columns. wide_columns = [ # Interactions between different categorical features can also # be added as new virtual features. layers.crossed_column( [education, occupation], hash_bucket_size=int(1e4)), layers.crossed_column( [age_buckets, race, occupation], hash_bucket_size=int(1e6)), layers.crossed_column( [native_country, occupation], hash_bucket_size=int(1e4)), gender, native_country, education, occupation, workclass, marital_status, relationship, age_buckets, ] deep_columns = [ layers.embedding_column(workclass, dimension=embedding_size), layers.embedding_column(education, dimension=embedding_size), layers.embedding_column(marital_status, dimension=embedding_size), layers.embedding_column(gender, dimension=embedding_size), layers.embedding_column(relationship, dimension=embedding_size), layers.embedding_column(race, dimension=embedding_size), layers.embedding_column(native_country, dimension=embedding_size), layers.embedding_column(occupation, dimension=embedding_size), age, education_num, capital_gain, capital_loss, hours_per_week, ] return tf.contrib.learn.DNNLinearCombinedClassifier( model_dir=model_dir, linear_feature_columns=wide_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=hidden_units or [100, 70, 50, 25])
def build_feature_cols(): # Sparse base columns. gender = layers.sparse_column_with_keys(column_name="gender", keys=["female", "male"]) race = layers.sparse_column_with_keys(column_name="race", keys=[ "Amer-Indian-Eskimo", "Asian-Pac-Islander", "Black", "Other", "White" ]) education = layers.sparse_column_with_hash_bucket("education", hash_bucket_size=1000) marital_status = layers.sparse_column_with_hash_bucket( "marital_status", hash_bucket_size=100) relationship = layers.sparse_column_with_hash_bucket("relationship", hash_bucket_size=100) workclass = layers.sparse_column_with_hash_bucket("workclass", hash_bucket_size=100) occupation = layers.sparse_column_with_hash_bucket("occupation", hash_bucket_size=1000) native_country = layers.sparse_column_with_hash_bucket( "native_country", hash_bucket_size=1000) # Continuous base columns. age = layers.real_valued_column("age") education_num = layers.real_valued_column("education_num") capital_gain = layers.real_valued_column("capital_gain") capital_loss = layers.real_valued_column("capital_loss") hours_per_week = layers.real_valued_column("hours_per_week") # Transformations. age_buckets = layers.bucketized_column( age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65]) education_occupation = layers.crossed_column([education, occupation], hash_bucket_size=int(1e4)) age_race_occupation = layers.crossed_column( [age_buckets, race, occupation], hash_bucket_size=int(1e6)) country_occupation = layers.crossed_column([native_country, occupation], hash_bucket_size=int(1e4)) # Wide columns and deep columns. wide_columns = [ gender, native_country, education, occupation, workclass, race, marital_status, relationship, age_buckets, education_occupation, age_race_occupation, country_occupation ] deep_columns = [ layers.embedding_column(gender, dimension=8), layers.embedding_column(native_country, dimension=8), layers.embedding_column(education, dimension=8), layers.embedding_column(occupation, dimension=8), layers.embedding_column(workclass, dimension=8), layers.embedding_column(race, dimension=8), layers.embedding_column(marital_status, dimension=8), layers.embedding_column(relationship, dimension=8), # layers.embedding_column(age_buckets, dimension=8), layers.embedding_column(education_occupation, dimension=8), layers.embedding_column(age_race_occupation, dimension=8), layers.embedding_column(country_occupation, dimension=8), age, education_num, capital_gain, capital_loss, hours_per_week, ] return wide_columns, deep_columns