简体   繁体   中英

Model subclassing ignores the weights of the Keras layers that appended to a Python list

I am trying to create a model subclassing with a variable number of layers and hidden layers' size.

Since the number and size of the hidden layers are not fixed, I appended the instantiated Keras layers into a list according to constructor parameters. But I do not see why when I use the list self.W to keep the Keras layers, the model ignores the weights of them.

class MLP(keras.Model):

  def __init__(self, first_size, num_hidden_layers, hidden_activation, num_classes, **kwargs):

    super(MLP, self).__init__()

    self.W = [Dense(units=first_size//(2**i), activation=hidden_activation) for i in range(num_hidden_layers)]

    # Regression task
    if num_classes == 0:
      self.W.append(Dense(units=1, activation='linear'))

    # Classification task
    else:
      self.W.append(Dense(units=num_classes, activation='softmax'))


  def call(self, inputs):

    x = inputs
    for w in self.W:
      x = w(x)

    return x
model = MLP(first_size=128, num_hidden_layers=4, hidden_activation='relu', num_classes=10)

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])
model.fit(x_train, y_train, batch_size=64, epochs=20, validation_data=(x_val, y_val))
model.summary()

Model: "mlp_23"
_________________________________________________________________
Layer (type) Output Shape Param # ================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0 _________________________________________________________________

I think you can do this easily like this. For example I used iris dataset from sklearn.

from keras.models import Model
from keras.layers import Input, Dense
import sklearn.datasets
iris_dataset = sklearn.datasets.load_iris()
x_train = iris_dataset["data"]
y_train = iris_dataset["target"]

inputs = Input(shape=x_train[0].shape)
x = inputs
num_hidden_layers=4
num_classes=10
hidden_activation='relu'
first_size=128

for i in range(num_hidden_layers):
    x=Dense(units=first_size//(2**i), activation=hidden_activation)(x)    
outputs=Dense(units=num_classes, activation='softmax')(x)

model = Model(inputs=inputs,outputs=outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])
model.summary()

output

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 4)                 0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               640       
_________________________________________________________________
dense_2 (Dense)              (None, 64)                8256      
_________________________________________________________________
dense_3 (Dense)              (None, 32)                2080      
_________________________________________________________________
dense_4 (Dense)              (None, 16)                528       
_________________________________________________________________
dense_5 (Dense)              (None, 10)                170       
=================================================================
Total params: 11,674
Trainable params: 11,674
Non-trainable params: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM