Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only isolated atom calculation is difficult for m3gnet? #38

Open
jinkera opened this issue Oct 5, 2022 · 1 comment
Open

Only isolated atom calculation is difficult for m3gnet? #38

jinkera opened this issue Oct 5, 2022 · 1 comment

Comments

@jinkera
Copy link

jinkera commented Oct 5, 2022

Thank you for the great repositry.
Like #37 I get the error in calculating H atom energy. However, after calculating H2 molecule energy, I don't get the error.
If possible, could you tell me the reason why?

from m3gnet.models import M3GNetCalculator, Potential, M3GNet
from ase.io import read
from ase import Atoms

def main():
    potential = Potential(M3GNet.load())
    calc = M3GNetCalculator(potential=potential)

    print("H")
    atoms = Atoms('H', positions=[(0, 0, -0.35)])
    atoms.set_calculator(calc)
    try:
        print(atoms.get_potential_energy())
    except Exception as e:
        print(e)

if __name__ == '__main__':
    main()
H
in user code:

    File "/usr/local/lib/python3.9/site-packages/m3gnet/models/_base.py", line 186, in get_efs_tensor  *
        energies = self.get_energies(graph)
    File "/usr/local/lib/python3.9/site-packages/m3gnet/models/_base.py", line 261, in get_energies  *
        return self.model(graph)
    File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
        raise e.with_traceback(filtered_tb) from None
    File "/tmp/__autograph_generated_fileot8cd98z.py", line 37, in tf__call
        ag__.for_stmt(ag__.converted_call(ag__.ld(range), (ag__.ld(self).n_blocks,), None, fscope), None, loop_body, get_state, set_state, ('g',), {'iterate_names': 'i'})
    File "/tmp/__autograph_generated_fileot8cd98z.py", line 35, in loop_body
        g = ag__.converted_call(ag__.ld(self).graph_layers[ag__.ld(i)], (ag__.ld(g),), None, fscope)
    File "/tmp/__autograph_generated_filetgzexm7i.py", line 16, in tf__call
        out = ag__.converted_call(ag__.ld(self).state_network, (ag__.converted_call(ag__.ld(self).atom_network, (ag__.converted_call(ag__.ld(self).bond_network, (ag__.ld(graph),), None, fscope),), None, fscope),), None, fscope)
    File "/tmp/__autograph_generated_file786n0fz6.py", line 18, in tf__call
        bonds = ag__.converted_call(ag__.ld(self).update_bonds, (ag__.ld(graph),), None, fscope)
    File "/tmp/__autograph_generated_filez4rbuqc7.py", line 40, in tf__update_bonds
        retval_ = ag__.converted_call(ag__.ld(self).update_func, (ag__.ld(concat),), None, fscope) * ag__.converted_call(ag__.ld(self).weight_func, (ag__.ld(graph)[ag__.ld(Index).BOND_WEIGHTS],), None, fscope) + ag__.ld(graph)[ag__.ld(Index).BONDS]
    File "/tmp/__autograph_generated_file4fv31nqr.py", line 20, in tf__call
        retval_ = ag__.converted_call(ag__.ld(self).pipe.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope) * ag__.converted_call(ag__.ld(self).gate.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope)
    File "/tmp/__autograph_generated_filehl5vv6vt.py", line 31, in tf__call
        ag__.for_stmt(ag__.ld(self).layers, None, loop_body, get_state, set_state, ('out',), {'iterate_names': 'layer'})
    File "/tmp/__autograph_generated_filehl5vv6vt.py", line 29, in loop_body
        out = ag__.converted_call(ag__.ld(layer), (ag__.ld(out),), None, fscope)

    ValueError: Exception encountered when calling layer "m3g_net" (type M3GNet).
    
    in user code:
    
        File "/usr/local/lib/python3.9/site-packages/m3gnet/models/_m3gnet.py", line 259, in call  *
            g = self.graph_layers[i](g)
        File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
            raise e.with_traceback(filtered_tb) from None
        File "/tmp/__autograph_generated_filetgzexm7i.py", line 16, in tf__call
            out = ag__.converted_call(ag__.ld(self).state_network, (ag__.converted_call(ag__.ld(self).atom_network, (ag__.converted_call(ag__.ld(self).bond_network, (ag__.ld(graph),), None, fscope),), None, fscope),), None, fscope)
        File "/tmp/__autograph_generated_file786n0fz6.py", line 18, in tf__call
            bonds = ag__.converted_call(ag__.ld(self).update_bonds, (ag__.ld(graph),), None, fscope)
        File "/tmp/__autograph_generated_filez4rbuqc7.py", line 40, in tf__update_bonds
            retval_ = ag__.converted_call(ag__.ld(self).update_func, (ag__.ld(concat),), None, fscope) * ag__.converted_call(ag__.ld(self).weight_func, (ag__.ld(graph)[ag__.ld(Index).BOND_WEIGHTS],), None, fscope) + ag__.ld(graph)[ag__.ld(Index).BONDS]
        File "/tmp/__autograph_generated_file4fv31nqr.py", line 20, in tf__call
            retval_ = ag__.converted_call(ag__.ld(self).pipe.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope) * ag__.converted_call(ag__.ld(self).gate.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope)
        File "/tmp/__autograph_generated_filehl5vv6vt.py", line 31, in tf__call
            ag__.for_stmt(ag__.ld(self).layers, None, loop_body, get_state, set_state, ('out',), {'iterate_names': 'layer'})
        File "/tmp/__autograph_generated_filehl5vv6vt.py", line 29, in loop_body
            out = ag__.converted_call(ag__.ld(layer), (ag__.ld(out),), None, fscope)
    
        ValueError: Exception encountered when calling layer "graph_network_layer" (type GraphNetworkLayer).
        
        in user code:
        
            File "/usr/local/lib/python3.9/site-packages/m3gnet/layers/_gn.py", line 52, in call  *
                out = self.state_network(self.atom_network(self.bond_network(graph)))
            File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
                raise e.with_traceback(filtered_tb) from None
            File "/tmp/__autograph_generated_file786n0fz6.py", line 18, in tf__call
                bonds = ag__.converted_call(ag__.ld(self).update_bonds, (ag__.ld(graph),), None, fscope)
            File "/tmp/__autograph_generated_filez4rbuqc7.py", line 40, in tf__update_bonds
                retval_ = ag__.converted_call(ag__.ld(self).update_func, (ag__.ld(concat),), None, fscope) * ag__.converted_call(ag__.ld(self).weight_func, (ag__.ld(graph)[ag__.ld(Index).BOND_WEIGHTS],), None, fscope) + ag__.ld(graph)[ag__.ld(Index).BONDS]
            File "/tmp/__autograph_generated_file4fv31nqr.py", line 20, in tf__call
                retval_ = ag__.converted_call(ag__.ld(self).pipe.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope) * ag__.converted_call(ag__.ld(self).gate.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope)
            File "/tmp/__autograph_generated_filehl5vv6vt.py", line 31, in tf__call
                ag__.for_stmt(ag__.ld(self).layers, None, loop_body, get_state, set_state, ('out',), {'iterate_names': 'layer'})
            File "/tmp/__autograph_generated_filehl5vv6vt.py", line 29, in loop_body
                out = ag__.converted_call(ag__.ld(layer), (ag__.ld(out),), None, fscope)
        
            ValueError: Exception encountered when calling layer "concat_atoms" (type ConcatAtoms).
            
            in user code:
            
                File "/usr/local/lib/python3.9/site-packages/m3gnet/layers/_bond.py", line 45, in call  *
                    bonds = self.update_bonds(graph)
                File "/usr/local/lib/python3.9/site-packages/m3gnet/layers/_bond.py", line 161, in update_bonds  *
                    return self.update_func(concat) * self.weight_func(graph[Index.BOND_WEIGHTS]) + graph[Index.BONDS]
                File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
                    raise e.with_traceback(filtered_tb) from None
                File "/tmp/__autograph_generated_file4fv31nqr.py", line 20, in tf__call
                    retval_ = ag__.converted_call(ag__.ld(self).pipe.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope) * ag__.converted_call(ag__.ld(self).gate.call, (ag__.ld(inputs),), dict(**ag__.ld(kwargs)), fscope)
                File "/tmp/__autograph_generated_filehl5vv6vt.py", line 31, in tf__call
                    ag__.for_stmt(ag__.ld(self).layers, None, loop_body, get_state, set_state, ('out',), {'iterate_names': 'layer'})
                File "/tmp/__autograph_generated_filehl5vv6vt.py", line 29, in loop_body
                    out = ag__.converted_call(ag__.ld(layer), (ag__.ld(out),), None, fscope)
            
                ValueError: Exception encountered when calling layer "gated_mlp_4" (type GatedMLP).
                
                in user code:
                
                    File "/usr/local/lib/python3.9/site-packages/m3gnet/layers/_core.py", line 229, in call  *
                        return self.pipe.call(inputs, **kwargs) * self.gate.call(inputs, **kwargs)
                    File "/usr/local/lib/python3.9/site-packages/m3gnet/layers/_core.py", line 38, in call  *
                        out = layer(out)
                    File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
                        raise e.with_traceback(filtered_tb) from None
                    File "/usr/local/lib/python3.9/site-packages/keras/layers/core/dense.py", line 141, in build
                        raise ValueError('The last dimension of the inputs to a Dense layer '
                
                    ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (0, None)
                
                
                Call arguments received by layer "gated_mlp_4" (type GatedMLP):
                  • inputs=tf.Tensor(shape=(0, None), dtype=float32)
                  • kwargs={'training': 'None'}
            
            
            Call arguments received by layer "concat_atoms" (type ConcatAtoms):
              • graph=['tf.Tensor(shape=(1, 64), dtype=float32)', 'tf.Tensor(shape=(None, 64), dtype=float32)', 'None', 'tf.Tensor(shape=(None, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'tf.Tensor(shape=(0, 3), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(None, 3), dtype=float32)', 'tf.Tensor(shape=(1, 3, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)']
              • kwargs={'training': 'None'}
        
        
        Call arguments received by layer "graph_network_layer" (type GraphNetworkLayer):
          • graph=['tf.Tensor(shape=(1, 64), dtype=float32)', 'tf.Tensor(shape=(None, 64), dtype=float32)', 'None', 'tf.Tensor(shape=(None, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'tf.Tensor(shape=(0, 3), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(None, 3), dtype=float32)', 'tf.Tensor(shape=(1, 3, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(0,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)']
          • kwargs={'training': 'None'}
    
    
    Call arguments received by layer "m3g_net" (type M3GNet):
      • graph=['tf.Tensor(shape=(1, 1), dtype=int32)', 'tf.Tensor(shape=(0, 1), dtype=float32)', 'None', 'tf.Tensor(shape=(None, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'tf.Tensor(shape=(0, 3), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(0,), dtype=float32)', 'tf.Tensor(shape=(1, 3, 3), dtype=float32)', 'tf.Tensor(shape=(0, 2), dtype=int32)', 'None', 'None', 'None', 'tf.Tensor(shape=(0,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)', 'tf.Tensor(shape=(1,), dtype=int32)']
      • kwargs={'training': 'None'}

↓no errors occur

from m3gnet.models import M3GNetCalculator, Potential, M3GNet
from ase.io import read
from ase import Atoms

def main():
    potential = Potential(M3GNet.load())
    calc = M3GNetCalculator(potential=potential)
    print("H2")
    atoms = Atoms('H2', positions=[(0, 0, -0.35), (0, 0, 0.35)])
    atoms.set_calculator(calc)
    try:
        print(atoms.get_potential_energy())
    except Exception as e:
        print(e)

    print("H")
    atoms = Atoms('H', positions=[(0, 0, -0.35)])
    atoms.set_calculator(calc)
    try:
        print(atoms.get_potential_energy())
    except Exception as e:
        print(e)

if __name__ == '__main__':
    main()
H2
[-6.506641]
H
[-1.1176894]
@sgbaird
Copy link
Contributor

sgbaird commented Dec 17, 2022

Since M3GNet uses a graph network under the hood, when you pass only one atom, there is only a single node (no edges) in the "graph". That's probably why you're seeing that error. If it's only important to you for testing, then it's probably safe to ignore. Note: I'm not one of the repo owners/writers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants