Simple but neat python class AppendableList
is introduced
and explained in this post.
First, let me describe the situation when I need it. For example, take a Lasagne tutorial. It is quite long (which pushes researchers from lasagne), but mostly filled with fairly trivial operations that make it hard to grasp everything fast.
Expressiveness ( = ability to write what you want to do without spending time on introducing additional entities) is considered to be a strong side of python.
But in this example it seems to me that newcomer may be lost in the jungles of computing validation quality:
1 |
|
It is better to inspect original code, here I give a minor part of a function.Basically, we compute two measures of quality (loss value, which is called err
here, and accuracy)
on minibatches and average those over minibatches.
Now, how this would be done in python if there was only one metric of quality?
1 |
|
Less code, easier to grasp, the same amount of operations. In particular the last line explicitly says: print averaged validation accuracies.
When I try to follow “pythonic way” to write this for original case with two metrics of quality, I get something like:
1 |
|
This code is not that bad, but required some kung-fu and may look even more scary for a python novice.
Now let’s imagine that during tuple unpacking we can append values to lists without storing those in intermediate variable.
1 |
|
In my opinion, this solution is quite beneficial: it is quite readable and does what it is expected to do. Also this trick interplays well with other python features.
Implementation of AppendableList
1 |
|
Why not split computing metrics in two different methods?
Splitting into separate functions should be avoided, because this will result in doubling the amount of computations. It is crucial in the case of neural networks, since the amount of computations may be huge.
Links for python developers
-
Beyond PEP 8 – Best practices for beautiful intelligible code
-
An intro to itertools
-
Also check an awesome page about formatting in python