6

In the following code snippet,

if evaluation_data: 
    n_data = len(evaluation_data)
    n = len(training_data)
    evaluation_cost, evaluation_accuracy = [], []
    training_cost, training_accuracy = [], []
    for j in list(range(epochs)):
        random.shuffle(training_data)
        mini_batches = training_data[k:k+mini_batch_size]

you can see that I'm not using xrange.Although the code was written to run on pyhton2, I refactored to run it on python3. However, I'm keep getting the follwoing error:

................................Directory/network2.py", line 147, in SGD
    for j in list(range(epochs)):
NameError: name 'xrange' is not defined

In the beginning, I used only range(). Then after learning that range() is not a list in python3 I did list(range()). However, I'm keep getting the error for xrange in both revised cases. Would appreciate if someone can help.

Take_Care_
  • 1,957
  • 1
  • 22
  • 24
Kalia Dona
  • 449
  • 1
  • 5
  • 11

1 Answers1

7

You are running stale bytecode, restart Python.

Python compiles source code to bytecode, and interprets the latter. This means the interpreter doesn't work with the source code once compiled.

However, us humans can't read bytecode very well, so when there is an exception and the interpreter wants us to understand where things went wrong, it would like to show you the source code again. Thus the source code is loaded on demand when there is a traceback to be shown, and lines are taken from the source code based on information recorded with the bytecode.

In your case, you are running bytecode that uses the name xrange. But you have already corrected the source code to use range instead. The bytecode throws an exception, and Python helpfully loads the source code from disk, and shows the already corrected sourcecode.

The solution is to tell Python to re-compile the source code, by restarting. If restarting doesn't help, then Python has determined that the source code is older than the bytecode it has cached. Delete the __pycache__ directory next to your source code to clear the bytecode cache, and remove any .pyc file that might be sitting in the same directory as your source.

Note that you can drop the list() call; you don't have to have a list for the for loop to work; for j in range(epoch): works just fine.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I thought python is not a compiled language.Why it compiles and then interprets? Shouldn't it be interpreting source at the same time?Or I'm having the wrong notion about compiled and interpreted language? – Kalia Dona Oct 19 '17 at 20:55
  • @KaliaDona It can do both. – cs95 Oct 19 '17 at 20:56
  • @cᴏʟᴅsᴘᴇᴇᴅ Thanks. I guess that understanding was my problem. – Kalia Dona Oct 19 '17 at 20:58
  • @KaliaDona: Python has an interpreter loop, but that loop is fed cross-platform portable bytecode. The Python compiler produces that bytecode whenever it imports a file for the first time and there is no bytecode cache yet (a `.pyc` file). It is not a very sophisticated compiler however. – Martijn Pieters Oct 19 '17 at 21:05
  • @KaliaDona check out [this](https://stackoverflow.com/questions/6889747/is-python-interpreted-or-compiled-or-both) question and [this](https://softwareengineering.stackexchange.com/questions/24558/is-python-interpreted-or-compiled) question. – juanpa.arrivillaga Oct 19 '17 at 21:07