How to build a simple neural network in 9 lines of Python code

Steve D'Aprano steve+python at pearwood.info
Tue Jun 27 22:28:25 EDT 2017


On Wed, 28 Jun 2017 06:22 am, Marko Rauhamaa wrote:

> You saw the APL example, right? APL's standard runtime/library contains
> most of Numpy functionality because that's what APL has been designed
> for.
> 
> Is that cheating?


Of course not. That demonstrates beautifully (or perhaps "unreadably tersely")
that the APL language primitives are extremely powerful (too powerful?).

Apart from just your usual contrariness *wink* I don't know why you are
objecting to this. It should be obvious that if you allow the use of external
libraries that can contain arbitrary amounts of code, *without* counting that
external code towards your measure of code complexity, you get a bogus
measurement of code complexity.

(That's like manufacturers who can make things artificially cheaply because they
don't have to pay the full costs of their input and processes: they get their
raw materials subsidised, putting part of the cost on tax payers, and don't
have to pay for their pollution, making others pay the cost.)

Suppose I said that I can write a full featured, advanced GUI web browser,
complete with Javascript and a plug-in system, in just *two* lines of Python
code:

import webbrowser
webbrowser.open('http://www.example.com')


What an astonishingly powerful language Python must be! Other languages require
dozens of lines of code just to parse Javascript, let alone run it.

Except... I'm not really measuring the capability of *Python*, as such. Not if I
word it as I have. I haven't really implemented a web browser, I'm just using
an external web browser. If I wanted to change the parameters, lets say by
changing the Javascript keyword "true" to "truish", then my code would expand
from two lines to millions of lines.

My demo of launching an external process to do all the work is of no help at all
to assist a developer in estimating the expressiveness and power of the
language.

If I were more honest, I would say:

"Here is how I can launch a web browser using the Python standard library in
just two lines of code."

which is an honest description of what I did, and can allow people to make fair
comparisons. E.g. comparing Python to AcmeScript, where you write:


program myprogram
begin program
load library webbrowser
new string url copied from 'http://www.example.com'
with webbrowser
  begin
    method = open
    method.call url
  end
end program

(I made that language up, for the record, don't bother googling for it.)

That gives you a fair comparison of the language expressiveness and power.

Chris is right: saying that we can create a simple neural network in 9 lines of
Python plus numpy allows fair comparison to other combinations of language and
library. Implying that it's just Python alone does not.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list