<div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Aug 30, 2018 at 8:31 PM James Lu <<a href="mailto:jamtlu@gmail.com" target="_blank">jamtlu@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Why shouldn't Python be better at implementing Domain Specific Languages?<div dir="ltr"><div style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div>[snip]</div><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">It would be nice if there was a DSL for describing neural networks (Keras).</div><div style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">The current syntax looks like this:</div><div style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><pre><code class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-python m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs">model.add(Dense(units=<span class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs-number">64</span>, activation=<span class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs-string">'relu'</span>, input_dim=<span class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs-number">100</span>))
model.add(Dense(units=<span class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs-number">10</span>, activation=<span class="m_-5738858187058626210gmail-m_-1208103006261820025gmail-hljs-string">'softmax'</span>))</code></pre></div></div></div></blockquote><div><br></div>How about something like this?</div><div class="gmail_quote"><br></div><div class="gmail_quote">with model:<br>    with Dense() as dense:<br>        dense.units = 64<br>        dense.activation = 'relu'<br>        dense.input_dim = 100<br><br>    with Dense() as dense:<br>        dense.units = 10<br>        dense.activation = 'softmax'</div><div class="gmail_quote"><br></div><div class="gmail_quote">The `with` creates a context to which subsequent layers are added when created within the context.</div><div class="gmail_quote"><br></div><div class="gmail_quote">But it does suffer from the fact that the `with` object's (or the underlying stack that would implement</div><div class="gmail_quote">this in the model/layers) scope is not local to the function, so if within the `with` context you call a <br></div><div class="gmail_quote">function that creates a layer, the layer will be added to the caller's context, which would be surprising.</div><div class="gmail_quote"><br></div><div class="gmail_quote">I was working on a similar approach for a python GUI, and `with` seemed like a very nice candidate, <br></div><div class="gmail_quote">but ran into this issue, which didn't seem to have a clean fix.</div><div class="gmail_quote"><br></div><div class="gmail_quote">I also thought about using an decorator that pre-processes the AST for a GUI description, which for <br></div><div class="gmail_quote">your example would look like something like:</div><div class="gmail_quote"><br></div><div class="gmail_quote">with model:<br>    with Dense():<br>        units = 64<br>        activation = 'relu'<br>        input_dim = 100<br><br>    with Dense():<br>        units = 10<br>        activation = 'softmax'</div><div class="gmail_quote"><br></div><div class="gmail_quote">But here the issues are (a) the similarity with local variables may be confusing (b) you'd need to</div><div class="gmail_quote">either make all `with` statements special, or annotate the `with` statements that are going to be <br></div><div class="gmail_quote">processed by the compiler (e.g. by prefixing the object with a dash). It seemed messy <br></div><div class="gmail_quote">enough that I'm still pondering this.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Matt<br></div><div class="gmail_quote"><br></div></div></div></div>