0

我正在使用他的代码遵循这个 Tensorflow 教程,并试图达到 8:00 显示的结果,但出于某种原因,我必须在 Windows 上使用带有 Jupyter 的 Docker,而不是他的 IDLE。Docker 机器在“Linux 2.6 / 3.x / 4.x (64-bit)”上运行。

他的代码:

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)

来自: https ://github.com/erroneousboat/tensorflow-python3-jupyter ,我使用 Python 3.4 和 Jupyter 1.0.0 拉取了 Tensorflow,并将 Matplotlib 升级到 1.5.1

对于 Jupyter,我已经%matplotlib notebook在顶部添加了。

当我运行这段代码时,我想plt.pause(0.1)在底部给我一个弃用警告和一个 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:

然后,整个程序停在了第一个错误的行。

我对其进行了测试并确认问题出在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()

运行代码时出现错误,并且该行仍然存在。没有plt.pause(1),这条线确实消失了。

我试图用 替换pausetime.sleep(1)但直到最后一个结果完成后,该数字才会显示lines

知道如何解决这个问题吗?先感谢您。

4

1 回答 1

1

知道了。感谢那些尝试过的人。

%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)
于 2016-09-28T13:30:27.373 回答