python> Exit processing> import sys / sys.exit ()

Operating environment


GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.

I wanted to stop the python code in the middle. It was okay to execute it sequentially using Jupyter, but I investigated how to stop it with script.

Reference http://stackoverflow.com/questions/179369/how-do-i-abort-the-execution-of-a-python-script

The following is a script for Deep Learning called TensorFlow. I wanted to finish after print (input_ph.get_shape ()).

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim

#Create a Queue with filename
filename_queue = tf.train.string_input_producer(["input.csv"])

#CSV parse
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
input1, output = tf.decode_csv(value, record_defaults=[[0.], [0.]])
inputs = tf.pack([input1])
output = tf.pack([output])

batch_size=4 # [4]
inputs_batch, output_batch = tf.train.shuffle_batch([inputs, output], batch_size, capacity=40, min_after_dequeue=batch_size)

input_ph = tf.placeholder("float", [None,1])
output_ph = tf.placeholder("float",[None,1])

#if 1 // debug
print(input_ph.get_shape())
sys.exit()
#endif

##Graph generation of NN
hiddens = slim.stack(input_ph, slim.fully_connected, [1,7,7,7], 
  activation_fn=tf.nn.sigmoid, scope="hidden")
prediction = slim.fully_connected(hiddens, 1, activation_fn=tf.nn.sigmoid, scope="output")
loss = tf.contrib.losses.mean_squared_error(prediction, output_ph)

#train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.01))
train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

def feed_dict(inputs, output):
    return {input_ph: inputs.eval(), output_ph: output.eval()}

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess=sess, coord=coord)

  try:
    sess.run(init_op)
    for i in range(30000): #[10000]
      _, t_loss = sess.run([train_op, loss], feed_dict=feed_dict(inputs_batch, output_batch))
      if (i+1) % 100 == 0:
        print("%d,%f" % (i+1, t_loss))
#        print("%d,%f,#step, loss" % (i+1, t_loss))
  finally:
    coord.request_stop()

  coord.join(threads)

result


$ python linreg2_feeddict.py 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
(?, 1)

Recommended Posts

python> Exit processing> import sys / sys.exit ()
[Python] How to use import sys sys.argv
My sys (python)
python image processing
Python module import
Import python script
Python file processing
Summarize Python import
Python distributed processing Spartan
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
Python from or import
python original module import
First Python image processing
Text processing in Python
Queue processing in Python
Image processing with Python
Import tsv with Python
Python string processing illustration
Various processing of Python
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
UTF8 text processing in python
python3 Measure the processing speed.
How Python module import works
"Apple processing" with OpenCV3 + Python3
Dynamically import scripts in Python
Import vtk with brew python
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Asynchronous processing (threading) in python
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Another way to import
Image processing by python (Pillow)
Post processing of python (NG)
Image Processing Collection in Python
Python # Snap7 Libray Import problem
Using Python mode in Processing
[Python] Iterative processing (for, while)
[Python] Image processing with scikit-image