# Adding regularization in TensorFlow.jl

**URL:** <https://discourse.julialang.org/t/adding-regularization-in-tensorflow-jl/3006>\
**Category:** Machine Learning\
**Created:** [April 2, 2017, 6:16am UTC](https://discourse.julialang.org/t/adding-regularization-in-tensorflow-jl/3006 "2017-04-02T06:16:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [April 2, 2017, 6:16am UTC](https://discourse.julialang.org/t/adding-regularization-in-tensorflow-jl/3006/1 "2017-04-02T06:16:48Z")

</div>

Hello All,

I am starting to play with TensorFlow.jl. I am thinking about how to add l2 regularization into all weights of all layers. Adding that during the definition stage seems to me to be a nuisance. I would like something automated, though I cannot come with a right filter to autimatically select all weight variables in the graph.

So far, my solution is  
‘’’  
l2reg=mapreduce(w-\>nn.l2\_loss(get\_tensor\_by\_name(w.name)),+,filter(op-\>contains(op.name,“weight”),map(v-\>v.var\_node,g.collections[:TrainableVariables])))  
‘’’  
where I use the assumption that each weight to be regularized have weight in the name.  
Does anyone have better solution?  
Thanks for any suggestions.  
Tomas
