The code setup for the autoencoder can directly be converted to a regularized autoencoder by replacing the cost definition with the following lines:
Lambda=0.01
cost = tf$reduce_mean(tf$pow(x - y_pred, 2))
Regularize_weights = tf$nn$l2_loss(weights)
cost = tf$reduce_mean(cost + lambda * Regularize_weights)