2

I have a similar problem to Keras replacing input layer, however I need to remove also the next layer, and that will require different input shape.

Here is a simplification of what I'm trying to do:

a = Input(shape=(64,))
b = Dense(32)(a)
c = Dense(16)(b)
d = Dense(8)(c)
model = Model(inputs=a, outputs=d)
print(model.summary())
print('input shape = ' + str(model.input_shape))

model.layers.pop(0)
model.layers.pop(0)
print(model.summary())
print('input shape = ' + str(model.input_shape))

new_input = Input(shape=(32,))
new_output = model(new_input)
new_model = Model(new_input, new_output)
print(new_model.summary())

But the input shape of the model remains the same:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                2080      
_________________________________________________________________
dense_2 (Dense)              (None, 16)                528       
_________________________________________________________________
dense_3 (Dense)              (None, 8)                 136       
=================================================================
Total params: 2,744
Trainable params: 2,744
Non-trainable params: 0
_________________________________________________________________
None
input shape = (None, 64)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 16)                528       
_________________________________________________________________
dense_3 (Dense)              (None, 8)                 136       
=================================================================
Total params: 664
Trainable params: 664
Non-trainable params: 0
_________________________________________________________________
None
input shape = (None, 64)

And that prevents me from creating new model, so the code above fails with:

ValueError: Dimensions must be equal, but are 32 and 64 for 'model_1/dense_1/MatMul' (op: 'MatMul') with input shapes: [?,32], [64,32].

Any ideas how to do that?

Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55
corwin
  • 77
  • 10

1 Answers1

1

It might not be possible to do in the way that you describe. The accepted answer on this post explains it a little.

how-to-change-input-shape-in-sequential-model-in-keras?

Their solution was to rebuild the layer with the correct input shape, then load the pre-trained weights for that specific layer.

Chris Farr
  • 3,580
  • 1
  • 21
  • 24