2

In the following simple example, I have a function foo() do something. There is a variable a inside the function. What I would like to do is that when the program catch the KeyboardInterrupt, the local functional variable a can be printed.

Clearly, the following way is not working. Is there anyway can do this?

def foo():
    a = 1
    for _ in xrange(10000):
        a += 1
    return a

try:
    a = foo()
except KeyboardInterrupt:
    print a

Updates (answer some comments below regarding why I want to Control-C):

Actually, my intention is to stop my Keras RNN training process earlier. Like the following code:

from keras.models import Sequential

def train():
    model = Sequential()
    ....
    model.build()
    ....
    for iteration in range(1, 200):
        ....
        model.fit(X_train, y_train, ...)
        ....
    ....


if __name__ == '__main__':
    try:
        model = train()
    except KeyboardInterrupt:
        model.save_weights("weights.hdf5")

If I stop the training process early, I would like to save the current model weights.

4
  • Can you use a debugger? Commented Apr 20, 2016 at 13:35
  • @Francesco what do you mean by debugger? Commented Apr 20, 2016 at 13:38
  • Well, from the docs: "Raised when the user hits the interrupt key (normally Control-C or Delete)". so do do you really want this or when the user press any key instead? Commented Apr 20, 2016 at 13:40
  • regarding why I want to Control-C, please see my question updates. Commented Apr 20, 2016 at 13:50

3 Answers 3

2

You just need to create your model outside the train function, and then pass model as an argument to train. That way you have a valid reference to model in case you interrupt train with Ctrl-C before it returns.

Eg,

from keras.models import Sequential

def train(model):
    model.build()
    ....
    for iteration in range(1, 200):
        ....
        model.fit(X_train, y_train, ...)
        ....
    ....


if __name__ == '__main__':
    try:
        model = Sequential()
        train(model)
    except KeyboardInterrupt:
        model.save_weights("weights.hdf5")
Sign up to request clarification or add additional context in comments.

Comments

2

In your example, a is local to foo(). Either you need to make it global OR you want to add your exception catch inside foo(). Here's an example of the latter

def foo():
    try:
        a = 1
        for _ in xrange(10000):
            a += 1
        return a
    except KeyboardInterrupt:
        print a

Comments

0

Since foo() never completes a is never assigned. You'll need to break up your function so the caller knows it's last value at any given time.

def foo(a):
   return a + 1

try:
    for _ in xrange(10000):
        a = foo(a)
except KeyboardInterrupt:
    print a

Not sure there's much of a point of doing it that way, but for illustration. You could also use a global variable but I don't like recommending that for most situations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.