In [1]:
import tensorflow as tf
sess = tf.Session()

from keras import backend as K
K.set_session(sess)


Using TensorFlow backend.

In [2]:
# this placeholder will contain our input digits, as flat vectors
img = tf.placeholder(tf.float32, shape=(None, 784))

In [3]:
from keras.layers import Dense

# Keras layers can be called on TensorFlow tensors:
x = Dense(128, activation='relu')(img)  # fully-connected layer with 128 units and ReLU activation
x = Dense(128, activation='relu')(x)
preds = Dense(10, activation='softmax')(x)  # output layer with 10 units and a softmax activation

In [4]:
labels = tf.placeholder(tf.float32, shape=(None, 10))

from keras.objectives import categorical_crossentropy
loss = tf.reduce_mean(categorical_crossentropy(labels, preds))

In [6]:
from tensorflow.examples.tutorials.mnist import input_data
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
with sess.as_default():
    for i in range(100):
        batch = mnist_data.train.next_batch(50)
        train_step.run(feed_dict={img: batch[0],
                                  labels: batch[1]})


----------------------------------------------------------------
TimeoutError                   Traceback (most recent call last)
/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1253             try:
-> 1254                 h.request(req.get_method(), req.selector, req.data, headers)
   1255             except OSError as err: # timeout error

/home/jhoward/anaconda3/lib/python3.5/http/client.py in request(self, method, url, body, headers)
   1105         """Send a complete request to the server."""
-> 1106         self._send_request(method, url, body, headers)
   1107 

/home/jhoward/anaconda3/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers)
   1150             body = _encode(body, 'body')
-> 1151         self.endheaders(body)
   1152 

/home/jhoward/anaconda3/lib/python3.5/http/client.py in endheaders(self, message_body)
   1101             raise CannotSendHeader()
-> 1102         self._send_output(message_body)
   1103 

/home/jhoward/anaconda3/lib/python3.5/http/client.py in _send_output(self, message_body)
    933 
--> 934         self.send(msg)
    935         if message_body is not None:

/home/jhoward/anaconda3/lib/python3.5/http/client.py in send(self, data)
    876             if self.auto_open:
--> 877                 self.connect()
    878             else:

/home/jhoward/anaconda3/lib/python3.5/http/client.py in connect(self)
    848         self.sock = self._create_connection(
--> 849             (self.host,self.port), self.timeout, self.source_address)
    850         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/home/jhoward/anaconda3/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    710     if err is not None:
--> 711         raise err
    712     else:

/home/jhoward/anaconda3/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    701                 sock.bind(source_address)
--> 702             sock.connect(sa)
    703             return sock

TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

URLError                       Traceback (most recent call last)
<ipython-input-6-f06017274e80> in <module>()
      1 from tensorflow.examples.tutorials.mnist import input_data
----> 2 mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)
      3 
      4 train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
      5 with sess.as_default():

/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py in read_data_sets(train_dir, fake_data, one_hot, dtype, reshape, validation_size)
    209 
    210   local_file = base.maybe_download(TRAIN_IMAGES, train_dir,
--> 211                                    SOURCE_URL + TRAIN_IMAGES)
    212   with open(local_file, 'rb') as f:
    213     train_images = extract_images(f)

/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in maybe_download(filename, work_directory, source_url)
    206   filepath = os.path.join(work_directory, filename)
    207   if not gfile.Exists(filepath):
--> 208     temp_file_name, _ = urlretrieve_with_retry(source_url)
    209     gfile.Copy(temp_file_name, filepath)
    210     with gfile.GFile(filepath) as f:

/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in wrapped_fn(*args, **kwargs)
    163       for delay in delays():
    164         try:
--> 165           return fn(*args, **kwargs)
    166         except Exception as e:  # pylint: disable=broad-except)
    167           if is_retriable is None:

/home/jhoward/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in urlretrieve_with_retry(url, filename)
    188 @retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable)
    189 def urlretrieve_with_retry(url, filename=None):
--> 190   return urllib.request.urlretrieve(url, filename)
    191 
    192 

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in urlretrieve(url, filename, reporthook, data)
    186     url_type, path = splittype(url)
    187 
--> 188     with contextlib.closing(urlopen(url, data)) as fp:
    189         headers = fp.info()
    190 

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    161     else:
    162         opener = _opener
--> 163     return opener.open(url, data, timeout)
    164 
    165 def install_opener(opener):

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
    464             req = meth(req)
    465 
--> 466         response = self._open(req, data)
    467 
    468         # post-process response

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in _open(self, req, data)
    482         protocol = req.type
    483         result = self._call_chain(self.handle_open, protocol, protocol +
--> 484                                   '_open', req)
    485         if result:
    486             return result

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    442         for handler in handlers:
    443             func = getattr(handler, meth_name)
--> 444             result = func(*args)
    445             if result is not None:
    446                 return result

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in http_open(self, req)
   1280 
   1281     def http_open(self, req):
-> 1282         return self.do_open(http.client.HTTPConnection, req)
   1283 
   1284     http_request = AbstractHTTPHandler.do_request_

/home/jhoward/anaconda3/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1254                 h.request(req.get_method(), req.selector, req.data, headers)
   1255             except OSError as err: # timeout error
-> 1256                 raise URLError(err)
   1257             r = h.getresponse()
   1258         except:

URLError: <urlopen error [Errno 110] Connection timed out>

In [ ]:
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)

In [ ]: