2016-09-28 8 views
0

私はthis Tensorflow tutorialのコードを使って8時の結果に到達しようとしていますが、私はJupyter何らかの理由で、彼のIDLEの代わりに。 Dockerマシンは "Linux 2.6/3.x/4.x(64-bit)"で動作しています。Python 3.4 matplotlib 1.5.1 pyplot.pause私が投げたNotImplementedError(Windows上のDockerで実行中)

彼のコード:

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を引っ張ってJupyterについて

を1.5.1 matplotlibのをアップグレードし、私はすでに追加しました上部に%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であることを確認しました。

コード実行時にエラーが発生し、その行はまだ存在します。 plt.pause(1)がなければ、その行は実際に消えてしまいます。

pausetime.sleep(1)に置き換えようとしましたが、最後の結果がlinesになるまで表示されませんでした。

これを解決する方法はありますか?前もって感謝します。

答えて

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) 
関連する問題