I recently switched form tensorflow to skflow. In tensorflow we would add our lambda*tf.nn.l2_loss(weights) to our loss. Now I have the following code in skflow:
def deep_psi(X, y):
layers = skflow.ops.dnn(X, [5, 10, 20, 10, 5], keep_prob=0.5)
preds, loss = skflow.models.logistic_regression(layers, y)
return preds, loss
def exp_decay(global_step):
return tf.train.exponential_decay(learning_rate=0.01,
global_step=global_step,
decay_steps=1000,
decay_rate=0.005)
deep_cd = skflow.TensorFlowEstimator(model_fn=deep_psi,
n_classes=2,
steps=10000,
batch_size=10,
learning_rate=exp_decay,
verbose=True,)
How and where do I add a regularizer here? Illia hints something here but I couldn't figure it out.
You can still add additional components to loss, you just need to retrieve weights from dnn / logistic_regression and add them to the loss:
def regularize_loss(loss, weights, lambda):
for weight in weights:
loss = loss + lambda * tf.nn.l2_loss(weight)
return loss
def deep_psi(X, y):
layers = skflow.ops.dnn(X, [5, 10, 20, 10, 5], keep_prob=0.5)
preds, loss = skflow.models.logistic_regression(layers, y)
weights = []
for layer in range(5): # n layers you passed to dnn
weights.append(tf.get_variable("dnn/layer%d/linear/Matrix" % layer))
# biases are also available at dnn/layer%d/linear/Bias
weights.append(tf.get_variable('logistic_regression/weights'))
return preds, regularize_loss(loss, weights, lambda)
```
Note, the path to variables can be found here.
Also, we want to add regularizer support to all layers with variables (like dnn
, conv2d
or fully_connected
) so may be next week's night build of Tensorflow should have something like this dnn(.., regularize=tf.contrib.layers.l2_regularizer(lambda))
. I'll update this answer when this happens.