google-deepmind / graph_nets

Build Graph Nets in Tensorflow
https://arxiv.org/abs/1806.01261
Apache License 2.0
5.36k stars 782 forks source link

Cannot interpret feed_dict as Tensor #59

Closed jmcs100 closed 5 years ago

jmcs100 commented 5 years ago

Hi! I'm trying to run a model that predicts node attributes based on global and edge inputs. I've been largely following the shortest_path.ipynb demo to write my code, and my code at the moment looks as follows (happy to include more if need be!):

# train_input, train_target, test_input etc. are all lists containing nxgraphs

train_input_ph, train_target_ph = create_placeholders(train_input, train_target)
test_input_ph, test_target_ph = create_placeholders(test_input, test_target)

output_train_graphs = graph_net_module(train_input_ph)
output_test_graphs = graph_net_module(test_input_ph)

loss_train = create_loss_ops(train_target_ph, output_train_graphs)
loss_test = create_loss_ops(test_target_ph, output_test_graphs)
....

train_input_ph, train_target_ph, output_train_graphs, output_test_graphs = make_all_runnable_in_session(train_input_ph, train_target_ph, output_train_graphs, output_test_graphs)

In the running training section of code, I then have:

for iteration in range(last_iteration, num_training_iterations):
    last_iteration = iteration
    train_values = sess.run({
        "step": step_op,
        "target": train_target_ph,
        "train_loss": loss_train,
        "outputs": output_train_graphs
    },
    feed_dict={train_input_ph: gn.utils_np.networkxs_to_graphs_tuple(train_input),
               train_target_ph: gn.utils_np.networkxs_to_graphs_tuple(train_target)}
    )

However, when I try to run the second set of code, I get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1091             subfeed_t = self.graph.as_graph_element(
-> 1092                 subfeed, allow_tensor=True, allow_operation=False)
   1093           except Exception as e:

/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
   3477     with self._lock:
-> 3478       return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
   3479 

/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
   3566       raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__,
-> 3567                                                            types_str))
   3568 

TypeError: Can not convert a Operation into a Tensor.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-1103-fddee2f34548> in <module>
     16     },
     17     feed_dict={train_input_ph: gn.utils_np.networkxs_to_graphs_tuple(train_input),
---> 18                train_target_ph: gn.utils_np.networkxs_to_graphs_tuple(train_target)}
     19     )
     20     the_time = time.time()

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1093           except Exception as e:
   1094             raise TypeError(
-> 1095                 'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
   1096 
   1097           if isinstance(subfeed_val, ops.Tensor):

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a Operation into a Tensor.

I notice this is a similar thing to #24 but when I tried the solution there of reducing make_all_runnable_in_session to only act on output_train_graphs and output_test_graphs, I get the following error instead:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1091             subfeed_t = self.graph.as_graph_element(
-> 1092                 subfeed, allow_tensor=True, allow_operation=False)
   1093           except Exception as e:

/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
   3477     with self._lock:
-> 3478       return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
   3479 

/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
   3566       raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__,
-> 3567                                                            types_str))
   3568 

TypeError: Can not convert a NoneType into a Tensor.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-1106-fddee2f34548> in <module>
     16     },
     17     feed_dict={train_input_ph: gn.utils_np.networkxs_to_graphs_tuple(train_input),
---> 18                train_target_ph: gn.utils_np.networkxs_to_graphs_tuple(train_target)}
     19     )
     20     the_time = time.time()

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1093           except Exception as e:
   1094             raise TypeError(
-> 1095                 'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
   1096 
   1097           if isinstance(subfeed_val, ops.Tensor):

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a NoneType into a Tensor.

What can I do with the feed_dict and session variables to make this all work?

alvarosg commented 5 years ago

The problem is that your graphs have Nones fields in them, so you cannot directly create the feed_dict like that.

Instead you should use the helper function gn.utils_tf.get_feed_dict:

feed_dict = gn.utils_tf.get_feed_dict(
    train_input_ph, 
    gn.utils_np.networkxs_to_graphs_tuple(train_input))
feed_dict.update(gn.utils_tf.get_feed_dict(
    train_target_ph, 
    gn.utils_np.networkxs_to_graphs_tuple(train_target)))

Hope this works!

vbapst commented 5 years ago

Hi,

I think you have Nones in your input. Can you try:

from graph_nets import utils_tf 

feed_dict=utils_tf.get_feed_dict(train_input_ph: gn.utils_np.networkxs_to_graphs_tuple(train_input))
feed_dict.update(utils_tf.get_feed_dict(train_target_ph:gn.utils_np.networkxs_to_graphs_tuple(train_target))
jmcs100 commented 5 years ago

Hi, thanks very much for getting back to me! So I'm now running:

for iteration in range(last_iteration, num_training_iterations):
    last_iteration = iteration
    feed_dict = utils_tf.get_feed_dict(
        train_input_ph, 
        utils_np.networkxs_to_graphs_tuple(train_input))
    feed_dict.update(utils_tf.get_feed_dict(
        train_target_ph, 
        utils_np.networkxs_to_graphs_tuple(train_target)
    ))
    train_values = sess.run({
        "step": step_op,
        "target": train_target_ph,
        "train_loss": loss_train,
        "outputs": output_train_graphs
    },
    feed_dict=feed_dict
    )

And the error is now:

---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

InternalError: Unable to get element as bytes.

During handling of the above exception, another exception occurred:

InternalError                             Traceback (most recent call last)
<ipython-input-1140-2017fe71ea6b> in <module>
     24         "outputs": output_train_graphs
     25     },
---> 26     feed_dict=feed_dict
     27     )
     28     the_time = time.time()

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1150     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1151       results = self._do_run(handle, final_targets, final_fetches,
-> 1152                              feed_dict_tensor, options, run_metadata)
   1153     else:
   1154       results = []

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1326     if handle is None:
   1327       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328                            run_metadata)
   1329     else:
   1330       return self._do_call(_prun_fn, handle, feeds, fetches)

/miniconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1346           pass
   1347       message = error_interpolation.interpolate(message, self._graph)
-> 1348       raise type(e)(node_def, op, message)
   1349 
   1350   def _extend_graph(self):

InternalError: Unable to get element as bytes.

Googling this seems to suggest there can be all kinds of reasons for this error, so I thought I'd bring it back here just in case.

vbapst commented 5 years ago

Can you try to see which output causes this? For instance I think you wont be able to run train_target_ph but should use utils_tf.make_runnable_in_session(train_target_ph)

alvarosg commented 5 years ago

I think you may need to use the graphs placeholders to call get_feed_dict before you pass them through by make_runnable_in_session.

But you seem to be losing the reference to those when you do: train_input_ph, train_target_ph, output_train_graphs, output_test_graphs = make_all_runnable_in_session(train_input_ph, train_target_ph, output_train_graphs, output_test_graphs)

Just try:

runnable_train_input_ph, runnable_train_target_ph, runnable_output_train_graphs, runnable_output_test_graphs = make_all_runnable_in_session(train_input_ph, train_target_ph, output_train_graphs, output_test_graphs)

And use the runnable_* ones only for feeding them directly in the first argument of sess.run, but not for anything else (not even to create the feed_dict).

jmcs100 commented 5 years ago

Thanks so much! The model is now training, really appreciate the help