paulgavrikov / visualkeras

Visualkeras is a Python package to help visualize Keras (either standalone or included in TensorFlow) neural network architectures. It allows easy styling to fit most needs. This module supports layered style architecture generation which is great for CNNs (Convolutional Neural Networks), and a graph style architecture, which works great for most models including plain feed-forward networks.
MIT License
564 stars 67 forks source link

Error when trying to visualise Functional API model:- AttributeError: 'Functional' object has no attribute '_layers' #41

Closed fraseralex96 closed 11 months ago

fraseralex96 commented 1 year ago

Hi Team,

I receive the following error when I try and run my CNN model built using the Keras Functional API:

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [18], in <cell line: 4>() 1 import visualkeras 2 from PIL import ImageFont ----> 4 visualkeras.graph_view(early_model)

File ~/.local/lib/python3.9/site-packages/visualkeras/graph.py:58, in graph_view(model, to_file, color_map, node_size, background_fill, padding, layer_spacing, node_spacing, connector_fill, connector_width, ellipsize_after, inout_as_tensor, show_neurons) 54 layer_y = list() 56 # Attach helper layers ---> 58 id_to_num_mapping, adj_matrix = model_to_adj_matrix(model) 59 model_layers = model_to_hierarchy_lists(model, id_to_num_mapping, adj_matrix) 61 # add fake output layers

File ~/.local/lib/python3.9/site-packages/visualkeras/layer_utils.py:39, in model_to_adj_matrix(model) 37 if not model.built: 38 model.build() ---> 39 layers = model._layers 41 adj_matrix = np.zeros((len(layers), len(layers))) 42 id_to_num_mapping = dict()

AttributeError: 'Functional' object has no attribute '_layers'`

Below you will find the function used to build my model:

`# CNN MODEL

def build_early_integration_CNN(X_train, learning_rate, momentum, seed):

# set layer weights initialiser
initializer = keras.initializers.GlorotUniform(seed=seed)

# drug-cell line data input
x_input = layers.Input(shape=(X_train.shape[1],1))
# 1st convolution layer
x = layers.Conv1D(filters=16, kernel_size=4, kernel_initializer=initializer, activation='relu')(x_input) 
x = layers.BatchNormalization()(x)
x = layers.MaxPooling1D()(x)
#x = layers.Dropout(0.1)(x)
# 2nd convolution layer
x = layers.Conv1D(filters=32, kernel_size=4, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling1D()(x)
#x = layers.Dropout(0.1)(x)
# 3rd convolution layer
x = layers.Conv1D(filters=64, kernel_size=4, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling1D()(x)
#x = layers.Dropout(0.1)(x)
# 4th convolution layer
x = layers.Conv1D(filters=64, kernel_size=4, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling1D()(x)
#x = layers.Dropout(0.1)(x)
x = layers.Flatten()(x)

# Concatenate phosphoproteomics and one-hot drug data + dense layers & activation layer
x = layers.Dense(256, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(128, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(64, kernel_initializer=initializer, activation='relu')(x)
x = layers.BatchNormalization()(x)
output = layers.Dense(1, kernel_initializer=initializer)(x) # actiation layer is 1 neuron for single regression prediction value

model = keras.Model(x_input, output)

model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=learning_rate, 
                                                    momentum=momentum), 
                                                    loss='mse', metrics=['mae'])
return model`

And the code used to call the function:

`#EARLY INTEGRATION

learning_rate = 1e-2 # initial learning rate, decays via learning rate scheduler momentum = 0.5 initializer_seed = 42

early_model = build_early_integration_CNN(X_train, learning_rate, momentum, initializer_seed)`

I would really appreciate your support as I am using these models for my MSc project and would love to include visualisations in my report!

Kind regards, Alex

github-actions[bot] commented 12 months ago

Stale issue message