如何使用AdaNet提高DNN模型的准确性?

时间:2019-10-13 06:45:13

标签: python tensorflow tensorflow-estimator adanet

我正在使用TensorFlow 2.0设置DNN模型,并使用AdaNet(版本0.8)进行NAS。如何使用AdaNet提高DNN模型的准确性?

生产AdaNet的模型(由两个不同的DNN子网组合而成)的指标要比单个DNN模型的指标差。我已经尝试过调整包括max_iteration_steps和AutoEnsembleEstimator训练步骤在内的参数,但似乎不起作用。


from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import tensorflow as tf

import adanet

# load data
(x_train, y_train), (x_test, y_test) = (
    tf.keras.datasets.boston_housing.load_data())

# input_fn
def input_fn(partition):
    def _input_fn():
        feat_tensor_dict = {}
        if partition == 'train':
            x = x_train.copy()
            y = y_train.copy()
        else:
            x = x_test.copy()
            y = y_test.copy()
        for i in range(0,np.size(x,1)):
            feat_nam = ('feat'+str(i))
            feat_tensor_dict[feat_nam] = tf.convert_to_tensor(x[:,i], dtype=tf.float32)
        label_tensor = tf.convert_to_tensor(y, dtype=tf.float32)
        return (feat_tensor_dict,label_tensor)
    return _input_fn

feat_nam_lst = ['feat'+str(i) for i in range(0,np.size(x_train,1))]

feature_columns = []
for item in feat_nam_lst:
    feature_columns.append(tf.feature_column.numeric_column(item))


head = tf.estimator.RegressionHead

# Build subnetwork
dnn_estimator_1 = tf.estimator.DNNRegressor(
                feature_columns = feature_columns,
                hidden_units=[100, 500, 100])

dnn_estimator_2 = tf.estimator.DNNRegressor(
                feature_columns = feature_columns,
                hidden_units=[200, 300, 100])

# Build AdaNet
estimator = adanet.AutoEnsembleEstimator(
    head=head,
    candidate_pool=lambda config: {
        "dnn1": dnn_estimator_1,
        "dnn2":dnn_estimator_2 },
    max_iteration_steps=1000)

estimator.train(input_fn=input_fn(partition = 'train'), steps=10)
metrics = estimator.evaluate(input_fn=input_fn(partition = 'test'),steps = 1000)

best_ensemble_index_0 = 1,global_step = 10,迭代= 0,label / mean = 23.078432,损失= 65.15532,预测/平均值= 22.63752


dnn_estimator_1.train(input_fn=input_fn(partition = 'train'), steps=1000)

dnn_estimator_1.evaluate(input_fn=input_fn(partition = 'test'), steps=1000)

{'average_loss':37.712597,  '标签/均值':23.078432,  '损失':37.712288,  '预测/平均值':23.500063,  'global_step':1000}

AdaNet的输出指标应该更好。但是事实恰恰相反。

1 个答案:

答案 0 :(得分:0)

看来您只训练了AdaNet 10步,而您训练了单独的DNN 1000步。

尝试将max_iteration_steps设置为1000,并进行2000步训练。