akensert / molgraph

Graph neural networks for molecular machine learning. Implemented and compatible with TensorFlow and Keras.
https://molgraph.readthedocs.io/en/latest/
MIT License
48 stars 5 forks source link

model summary returns only multiple #17

Closed thegodone closed 10 months ago

thegodone commented 1 year ago

How to use class models and return the output dimension of each layers ?

I need to pass the input shape of the graphs but I don't see an example to do so ?

from tensorflow.keras.layers import Dense, Input, Flatten, Conv2D, MaxPool2D
from tensorflow.keras.models import Model

class LeNet5(tf.keras.Model):
  def __init__(self):
    super(LeNet5, self).__init__()
    #creating layers in initializer
    self.conv1 = Conv2D(filters=6, kernel_size=(5,5), padding="same", activation="relu")
    self.max_pool2x2 = MaxPool2D(pool_size=(2,2))
    self.conv2 = Conv2D(filters=16, kernel_size=(5,5), padding="same", activation="relu")
    self.conv3 = Conv2D(filters=120, kernel_size=(5,5), padding="same", activation="relu")
    self.flatten = Flatten()
    self.fc2 = Dense(units=84, activation="relu")
    self.fc3=Dense(units=10, activation="softmax")

  def call(self, input_tensor):
    #don't add layers here, need to create the layers in initializer, otherwise you will get the tf.Variable can only be created once error
    x = self.conv1(input_tensor)
    x = self.max_pool2x2(x)
    x = self.conv2(x)
    x = self.max_pool2x2(x)
    x = self.conv3(x)
    x = self.flatten(x)
    x = self.fc2(x)
    x = self.fc3(x)
    return x  
input_layer = Input(shape=(32,32,3,))
x = LeNet5()(input_layer)
model = Model(inputs=input_layer, outputs=x)
print(model.summary(expand_nested=True))

How to get the Input(shape=(32,32,3,)) for a given graphtensor ?

akensert commented 1 year ago

How to get the Input(shape=(32,32,3,)) for a given graphtensor ?

Note entirely sure what you ask for regarding the Input.

keras.layers.Input accepts type specs (instead of shape). So you would do keras.layers.Input(type_spec=graph_tensor.spec); or better: keras.layers.Input(type_spec=graph_tensor.unspecific_spec).

akensert commented 1 year ago

Regardingmodel.summary() returning multiple is normal, when subclassing keras models. For known shapes use the functional or sequential API. If you really need known shapes (in summary()) for subclassed models, you could override the summary method.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 10 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.