NREL / nfp

Keras layers for end-to-end learning with rdkit and pymatgen
Other
57 stars 28 forks source link

"None values not supported." error in example #7

Closed benja8151 closed 2 years ago

benja8151 commented 3 years ago

I've been trying to run the example code, but running cell #10 ("Define the keras model") gives me ValueError: Tried to convert 'input' to a tensor and failed. Error: None values not supported..

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-c064f7512961> in <module>
     25 for _ in range(3):  # Do the message passing
     26     bond_state = nfp.EdgeUpdate()([atom_state, bond_state, connectivity, global_state])
---> 27     atom_state = nfp.NodeUpdate()([atom_state, bond_state, connectivity, global_state])
     28     global_state = nfp.GlobalUpdate(units=8, num_heads=1)(
     29         [atom_state, bond_state, connectivity, global_state]) 

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
    950     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    951       return self._functional_construction_call(inputs, args, kwargs,
--> 952                                                 input_list)
    953 
    954     # Maintains info about the `Layer.call` stack.

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1089         # Check input assumptions set after layer building, e.g. input shape.
   1090         outputs = self._keras_tensor_symbolic_call(
-> 1091             inputs, input_masks, args, kwargs)
   1092 
   1093         if outputs is None:

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
    820       return nest.map_structure(keras_tensor.KerasTensor, output_signature)
    821     else:
--> 822       return self._infer_output_signature(inputs, args, kwargs, input_masks)
    823 
    824   def _infer_output_signature(self, inputs, args, kwargs, input_masks):

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
    861           # TODO(kaftan): do we maybe_build here, or have we already done it?
    862           self._maybe_build(inputs)
--> 863           outputs = call_fn(inputs, *args, **kwargs)
    864 
    865         self._handle_activity_regularization(inputs, outputs)

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\autograph\impl\api.py in wrapper(*args, **kwargs)
    668       except Exception as e:  # pylint:disable=broad-except
    669         if hasattr(e, 'ag_error_metadata'):
--> 670           raise e.ag_error_metadata.to_exception(e)
    671         else:
    672           raise

ValueError: in user code:

    C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages\nfp\layers\graph_layers.py:109 call  *
        messages = tf.where(tf.expand_dims(mask[1], axis=-1),
    C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper  **
        return target(*args, **kwargs)
    C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\ops\array_ops.py:435 expand_dims_v2
        return gen_array_ops.expand_dims(input, axis, name)
    C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\ops\gen_array_ops.py:2278 expand_dims
        "ExpandDims", input=input, dim=axis, name=name)
    C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages\tensorflow\python\framework\op_def_library.py:540 _apply_op_helper
        (input_name, err))

    ValueError: Tried to convert 'input' to a tensor and failed. Error: None values not supported.
benja8151 commented 3 years ago

I was using the latest version of tensorflow (2.4.1). Reverting to version 2.0.0 - same as in the example gives a different error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-c064f7512961> in <module>
     27     atom_state = nfp.NodeUpdate()([atom_state, bond_state, connectivity, global_state])
     28     global_state = nfp.GlobalUpdate(units=8, num_heads=1)(
---> 29         [atom_state, bond_state, connectivity, global_state]) 
     30 
     31 # Since the final prediction is a single, molecule-level property (YSI), we

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    840                     not base_layer_utils.is_in_eager_or_tf_function()):
    841                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 842                     outputs = call_fn(cast_inputs, *args, **kwargs)
    843                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    844                     # circular dependencies.

~\anaconda3\envs\Python 37\lib\site-packages\tensorflow_core\python\autograph\impl\api.py in wrapper(*args, **kwargs)
    235       except Exception as e:  # pylint:disable=broad-except
    236         if hasattr(e, 'ag_error_metadata'):
--> 237           raise e.ag_error_metadata.to_exception(e)
    238         else:
    239           raise

TypeError: in converted code:
    relative to C:\Users\Benjamin\anaconda3\envs\Python 37\lib\site-packages:

    nfp\layers\graph_layers.py:157 call  *
        graph_element_mask = tf.concat([mask[0], mask[1]], axis=1)
    tensorflow_core\python\util\dispatch.py:180 wrapper
        return target(*args, **kwargs)
    tensorflow_core\python\ops\array_ops.py:1431 concat
        return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
    tensorflow_core\python\ops\gen_array_ops.py:1256 concat_v2
        "ConcatV2", values=values, axis=axis, name=name)
    tensorflow_core\python\framework\op_def_library.py:499 _apply_op_helper
        raise TypeError("%s that don't all match." % prefix)

    TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [<NOT CONVERTIBLE TO TENSOR>, bool] that don't all match.
pstjohn commented 3 years ago

Thanks for the issue! This was a change from me that didn't get updated into the example notebook, although i'll look into making that error a bit more intuitive.

For various reasons I ended up changing out the masking was done in the GraphLayers, and now the typical way to stack the message layers is to put in the residual connections manually, i.e.,

for _ in range(3):  # Do the message passing
    new_bond_state = nfp.EdgeUpdate()([atom_state, bond_state, connectivity, global_state])
    bond_state = layers.Add()([bond_state, new_bond_state])

    new_atom_state = nfp.NodeUpdate()([atom_state, bond_state, connectivity, global_state])
    atom_state = layers.Add()([atom_state, new_atom_state])

    new_global_state = nfp.GlobalUpdate(units=8, num_heads=1)(
        [atom_state, bond_state, connectivity, global_state]) 
    global_state = layers.Add()([global_state, new_global_state])

That way the keras_mask gets passed via the Add() layer, and we're not relying on masking from our own custom layers (which doesn't get saved if you use the tf.SavedModel format). Give the new example notebook a try, I'm hoping that solves the problem.

Thanks!

pstjohn commented 3 years ago

OK, I made a couple changes in 0.2.1 where you hopefully wouldn't see that error, even with the original code. But the residual connections are still removed from those GraphLayers to play nicer with the tf.SavedModel format, so the updated example is the better code to follow