0

I am following this Tensorflow tutorial using his code and trying to reach the result shown at 8:00, but I must use Docker on Windows with Jupyter for some reason, instead of his IDLE. The Docker machine is running on "Linux 2.6 / 3.x / 4.x (64-bit)".

His code:

from __future__ import print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

%matplotlib notebook

def add_layer(inputs, in_size, out_size, activation_function=None):
    # add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# Make up some real data
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediciton and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                     reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

# important step
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()

for i in range(1000):
    # training
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # to visualize the result and improvement
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        prediction_value = sess.run(prediction, feed_dict={xs: x_data})
        # plot the prediction
        lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
        plt.pause(0.1)

From: https://github.com/erroneousboat/tensorflow-python3-jupyter, I pulled Tensorflow with Python 3.4 and Jupyter 1.0.0 and upgrade Matplotlib to 1.5.1

For Jupyter, I've already added %matplotlib notebook on the top.

When I run this code, I think plt.pause(0.1) at the bottom threw me a deprecation warning and a NotImplementedError:

/usr/local/lib/python3.4/site-packages/matplotlib/backend_bases.py:2437: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str, mplDeprecation)
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-a34f2ae50337> in <module>()
     59         # plot the prediction
     60         lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
---> 61         plt.pause(0.1)

/usr/local/lib/python3.4/site-packages/matplotlib/pyplot.py in pause(interval)
    297                 canvas.draw()
    298             show(block=False)
--> 299             canvas.start_event_loop(interval)
    300             return
    301 

/usr/local/lib/python3.4/site-packages/matplotlib/backends/backend_nbagg.py in start_event_loop(self, timeout)
    192 
    193     def start_event_loop(self, timeout):
--> 194         FigureCanvasBase.start_event_loop_default(self, timeout)
    195 
    196     def stop_event_loop(self):

/usr/local/lib/python3.4/site-packages/matplotlib/backend_bases.py in start_event_loop_default(self, timeout)
   2443         self._looping = True
   2444         while self._looping and counter * timestep < timeout:
-> 2445             self.flush_events()
   2446             time.sleep(timestep)
   2447             counter += 1

/usr/local/lib/python3.4/site-packages/matplotlib/backend_bases.py in flush_events(self)
   2388         backends with GUIs.
   2389         """
-> 2390         raise NotImplementedError
   2391 
   2392     def start_event_loop(self, timeout):

NotImplementedError:

Then, the whole program stopped at the first erred line.

I tested it and confirmed the problem is with pause.

import matplotlib.pyplot as plt
import time
%matplotlib notebook

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

plt.ion()
plt.show()

lines = ax.plot([1,2,3],[2,3,4], 'r-', lw=3)
plt.pause(1)
ax.lines[0].remove()

The error occurred when the code is run, and the line is still there. Without plt.pause(1), the line is gone indeed.

I tried to replace pause with time.sleep(1), but the figure wouldn't show up until the last result of lines is finished.

Any idea how to solve this? Thank you in advance.

Oliver Li
  • 11
  • 1
  • 4

1 Answers1

1

Got it. Thanks to those who tried.

%matplotlib notebook

from __future__ import print_function
import tensorflow as tf
import numpy as np

import matplotlib.pyplot as plt



def add_layer(inputs, in_size, out_size, activation_function=None):
    # add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# Make up some real data
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediciton and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                     reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

# important step
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()

for i in range(1000):
    # training
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # to visualize the result and improvement
        try:
            plt.pause(0.5)
        except Exception:
            pass

        try:


            ax.lines.remove(lines[0])
            plt.show()
        except Exception as e:
            pass
            #print (str(e))

        prediction_value = sess.run(prediction, feed_dict={xs: x_data})
        lines = ax.plot(x_data, prediction_value, 'r-', lw=10)
Oliver Li
  • 11
  • 1
  • 4