1

I was trying to run Integrated Gradients as implemented here but unfortunately I get 'Sequential' object has no attribute 'model'.

My model is:

def nn_model():
    learning_rate = 0.001
    model = Sequential()
    model.add(Input(shape=(X1.shape[1],)))
    model.add(Dense(units=100,activation='LeakyReLU'))
    model.add(Dropout(0.01))
    model.add(Dense(units=50,activation='LeakyReLU'))
    model.add(Dropout(0.01))
    model.add(Dense(units=25,activation='LeakyReLU'))
    model.add(Dropout(0.01))
    model.add(Dense(units=1,activation='LeakyReLU'))
    # compile model
    model.compile(loss='mae', optimizer=optimizers.Adam(learning_rate=learning_rate), metrics=['mae'])
    return model

final_nn_model = nn_model()

# fit network
history = final_nn_model.fit(X1, y1, batch_size=10, epochs=500, shuffle=True, verbose=1) 

The line that raises the error is:

if isinstance(model, Sequential):
---> 35     self.model = model.model
     36 elif isinstance(model, Model):
     37     self.model = model

If .model is deprecated, what should I use instead?

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Caterina
  • 775
  • 9
  • 26
  • The code is plain wrong, there has never been a model attribute inside Sequential models, you could just replace that code with self.model = model – Dr. Snoopy Dec 28 '22 at 15:16
  • Yeah I also did that but then it gives me an error here: self.model.output.shape[1]._value, self.model.output._keras_shape[1] and self.model.output[:, c] – Caterina Dec 28 '22 at 15:20
  • I don't, this is not my code. But this is the error that rises if I simply do a dirty self.model = model fix. I don't get why someone would publish a code that doesn't work in a journal :/ – Caterina Dec 28 '22 at 15:35
  • What makes you think this code was published in a journal? – Dr. Snoopy Dec 28 '22 at 15:45
  • 1
    This [code](https://github.com/hiranumn/IntegratedGradients/blob/master/IntegratedGradients.py) uses `keras & tensorflow 1.x` and the old version. You can get all attributes of a `model` object with `model.__dict__`. You can read [this](https://stackoverflow.com/a/39392891/1740577) also. You can check with `hasattr(model, 'model')` and see we get `False`. – I'mahdi Dec 28 '22 at 16:26
  • Thanks Mahdi! Any chance you could write a full answer for this? I can give you my dataset if you need it. – Caterina Dec 28 '22 at 16:51
  • @Caterina, you can correct code with : `self.model = model` and `self.model.output.shape[1]` – I'mahdi Dec 28 '22 at 17:03
  • Now I get: def __array__(self, dtype=None): --> 283 raise TypeError( 284 f"You are passing {self}, an intermediate Keras symbolic " 285 "input/output, to a TF API that does not allow registering custom " 286 "dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, " 287 "or `tf.map_fn`. Keras Functional model construction only supports " 288 "TF API calls that *do* support dispatching, such as `tf.math.add` " 289 "or `tf.reshape`. " – Caterina Dec 28 '22 at 18:46

0 Answers0