1

我目前正在学习谷歌的机器学习速成课程,并且正在尝试使用 DNNClassifier 估计器来解决二进制分类问题。我正在尝试向 Adam 优化器添加正则化(L1/L2),因为它尚未被定义为函数中的参数。任何想法如何实现它?下面是我的代码:

steps = 1000
periods = 10
steps_per_period = steps / periods

my_optimiser = tf.train.AdamOptimizer(learning_rate = learning_rate)
my_optimiser = tf.contrib.estimator.clip_gradients_by_norm(my_optimiser, 5.0)
dnn_classifier = tf.estimator.DNNClassifier(
        feature_columns = construct_feature_columns(training_features),
        n_classes = 2,
        hidden_units = hidden_units,
        optimizer = my_optimiser)

training_input_fn = lambda: my_input_fn(
  training_features, 
  training_targets, 
  batch_size = batch_size)
predict_training_input_fn = lambda: my_input_fn(
  training_features, 
  training_targets, 
  num_epochs = 1, 
  shuffle = False)
predict_validation_input_fn = lambda: my_input_fn(
  validation_features, 
  validation_targets, 
  num_epochs = 1, 
  shuffle = False)

training_log_losses = []
validation_log_losses = []

for period in range (0, periods):

    dnn_classifier.train(
            input_fn = training_input_fn,
            steps = steps_per_period
            )
4

0 回答 0