# Adding a few more layers to pre\_trained\_model

**URL:** <https://discuss.ai.google.dev/t/adding-a-few-more-layers-to-pre-trained-model/32620>\
**Category:** Keras\
**Tags:** model-code, model-training, help\_request\
**Created:** [March 11, 2024, 12:22am UTC](https://discuss.ai.google.dev/t/adding-a-few-more-layers-to-pre-trained-model/32620 "2024-03-11T00:22:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seungjun\_Lee](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/seungjun_lee/32/18164_2.png) [@Seungjun\_Lee](https://discuss.ai.google.dev/u/Seungjun_Lee)\
**Post date:** [March 11, 2024, 12:22am UTC](https://discuss.ai.google.dev/t/adding-a-few-more-layers-to-pre-trained-model/32620/1 "2024-03-11T00:22:58Z")

</div>

Can someone explain me why first version of code works but not second?

I first loaded a InceptionV3 pertained model as follow:

```auto
pre_trained_model = InceptionV3(input_shape = (150, 150, 3),
                                  include_top = False,
                                  weights = None)

pre_trained_model.load_weights(local_weights_file)

for layer in pre_trained_model.layers:
  layer.trainable = False

```

Then tried to create a model adding a few more layers to it.

First version:

```auto
def create_model1():
  last_desired_layer = pre_trained_model.get_layer('mixed7')
  last_output = last_desired_layer.output
  x = layers.Flatten()(last_output)
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(1024, activation='relu')(x)
  x = tf.keras.layers.Dense(1, activation='sigmoid')(x)

  model = Model(inputs=pre_trained_model.input, outputs=x)

  model.compile(optimizer=RMSprop(learning_rate=0.0001),
                loss='binary_crossentropy',
                metrics=['accuracy'])
  
  return model

```

Second version:

```auto
def create_model2():
  inputs1 = keras.Input(shape=(150, 150, 3))
  dummy_output = pre_trained_model(inputs1)
  last_desired_layer = pre_trained_model.get_layer('mixed7')
  last_output = last_desired_layer.output
  x = layers.Flatten()(last_output)
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(1024, activation='relu')(x)
  x = tf.keras.layers.Dense(1, activation='sigmoid')(x)

  model = Model(inputs=inputs1, outputs=x)

  model.compile(optimizer=RMSprop(learning_rate=0.0001),
                loss='binary_crossentropy',
                metrics=['accuracy'])

  return model

```

First version works fine, but second version gives error saying:  
`ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 150, 150, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'") at layer "conv2d". The following previous layers were accessed without issue: []`

I double checked the pre\_trained\_model’s input shape is (None, 150, 150, 3). But why this error is happening?

---

<div class="post-metadata">

**Author:** ![aniruthraj](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/aniruthraj/32/660_2.png) [@aniruthraj](https://discuss.ai.google.dev/u/aniruthraj)\
**Post date:** [October 23, 2024, 5:41pm UTC](https://discuss.ai.google.dev/t/adding-a-few-more-layers-to-pre-trained-model/32620/3 "2024-10-23T17:41:15Z")

</div>

Hi @Seungjun_Lee,

Sorry for the delay in response.  
I could see you are directly using the inputs as in the pretrained model to your new model but in model2 creates a new input tensor `inputs1` which still uses the original model’s last layer `last_desired_layer`, causing a graph disconnection error. To fix this, I suggest to use the output of the new input (dummy\_output) as the last\_desired\_layer as below.

```auto
def create_model2():
  inputs1 = keras.Input(shape=(150, 150, 3))
  dummy_output = pre_trained_model(inputs1)
  last_desired_layer = dummy_output 

```

Please let us know if this works as expected.Thank You.
