Tflearn: Support execution in Jupyter notebook

Created on 5 Apr 2016  Â·  3Comments  Â·  Source: tflearn/tflearn

Trying to run the contents of examples/basics/linear_regression.py (or anything, really) within a Jupyter notebook gives the following error:

---------------------------------
Run id: M51N6E
Log directory: /tmp/tflearn_logs/
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-1-99453409cb2b> in <module>()
     15                                 metric='R2', learning_rate=0.01)
     16 m = tflearn.DNN(regression)
---> 17 m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)
     18 
     19 print("\nRegression result:")

/Users/brettnaul/miniconda3/envs/deep/lib/python3.5/site-packages/tflearn/models/dnn.py in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, run_id)
    159                          snapshot_epoch=snapshot_epoch,
    160                          shuffle_all=shuffle,
--> 161                          run_id=run_id)
    162 
    163     def predict(self, X):

/Users/brettnaul/miniconda3/envs/deep/lib/python3.5/site-packages/tflearn/helpers/trainer.py in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, run_id)
    188             validation_split(val_feed_dicts, feed_dicts)
    189 
--> 190             termlogger = callbacks.TermLogger(self.training_step)
    191             modelsaver = callbacks.ModelSaver(self.save,
    192                                               self.training_step,

/Users/brettnaul/miniconda3/envs/deep/lib/python3.5/site-packages/tflearn/callbacks.py in __init__(self, training_step)
     51         self.global_data_size = 0
     52         self.global_val_data_size = 0
---> 53         curses.setupterm()
     54         sys.stdout.write(curses.tigetstr('civis').decode())
     55 

/Users/brettnaul/miniconda3/envs/deep/lib/python3.5/site-packages/ipykernel/iostream.py in fileno(self)
    304 
    305     def fileno(self):
--> 306         raise UnsupportedOperation("IOStream has no fileno.")
    307 
    308     def write(self, string):

UnsupportedOperation: IOStream has no fileno.

I think this could be as simple as adding a flag to skip the TermLogger step, but maybe there's a solution that would look nice in the notebook and fall back to the curses version in the terminal?

help wanted

All 3 comments

Notebooks are now working, but there is another issue, because jupyter notebooks doesn't seem to be able to handle VT100 characters (such as \033[A), display isn't good and need to be fix.

As you proposed, maybe it is better to directly write a new callback that will used a different style, only for notebooks. I can investigate that later, unless someone want to help with this.

Thanks for the fix @aymericdamien! I am also getting the following error when I try to run the linear regression example in Jupyter. It works as expected on the command line.

import tflearn

X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1]
Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3]

input_layer = tflearn.input_data(shape=[None])
linear_layer = tflearn.single_unit(input_layer)
regression_layer = tflearn.regression(linear_layer, optimizer="sgd", loss="mean_square",
                                      metric="R2", learning_rate=.01)

m = tflearn.DNN(regression_layer)
m.fit(X, Y, n_epoch=1000, show_metric=False, snapshot_epoch=False)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-0ed73d93e10c> in <module>()
     13 # Train the network
     14 m = tflearn.DNN(regression_layer)
---> 15 m.fit(X, Y, n_epoch=1000, show_metric=False, snapshot_epoch=False)
     16 
     17 # Results

/usr/local/lib/python2.7/site-packages/tflearn/models/dnn.pyc in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, run_id)
    146         # TODO: check memory impact for large data and multiple optimizers
    147         feed_dict = feed_dict_builder(X_inputs, Y_targets, self.inputs,
--> 148                                       self.targets)
    149         feed_dicts = [feed_dict for i in self.train_ops]
    150         val_feed_dicts = None

/usr/local/lib/python2.7/site-packages/tflearn/utils.pyc in feed_dict_builder(X, Y, net_inputs, net_targets)
    240             elif len(net_inputs) > 1:
    241                 if np.ndim(X) < 2:
--> 242                     raise ValueError("Multiple inputs but only one data "
    243                                      "feeded. Please verify number of inputs "
    244                                      "and data provided match.")

ValueError: Multiple inputs but only one data feeded. Please verify number of inputs and data provided match.

You got this error if you are running more than one time this code, because every time, data are added to the default graph, so it has a lot of input data layers, but you only feed a single X, thus the error.

To correct it, if you want to run this code multiple times, you need to specify a graph:

import tflearn
import tensorflow as tf
​
X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1]
Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3]
​
graph_to_use = tf.Graph()
with graph_to_use.as_default():
  input_layer = tflearn.input_data(shape=[None])
  linear_layer = tflearn.single_unit(input_layer)
  regression_layer = tflearn.regression(linear_layer, optimizer="sgd", loss="mean_square",
                                                              metric="R2", learning_rate=.01)
  m = tflearn.DNN(regression_layer)
  m.fit(X, Y, n_epoch=1000, show_metric=False, snapshot_epoch=False)
Was this page helpful?
0 / 5 - 0 ratings