LSTM model.fit issue Your Layer or Model is in an invalid state

Viewed 692

Hello everyone I hope is all well

I got this annoying error while doing this code on LSTM :

Your Layer or Model is in an invalid state. This can happen if you are interleaving estimator/non-estimator models or interleaving models/layers made in tf.compat.v1.Graph.as_default() with models/layers created outside of it. Converting a model to an estimator

Below the following code below:

def djmodel(Tx, LSTM_cell, densor, reshaper):
    
    n_values = densor.units
    
    # Get the number of the hidden state vector
    n_a = LSTM_cell.units
    
    # Define the input layer and specify the shape
    X = Input(shape=(Tx, n_values)) 
    
    # Define the initial hidden state a0 and initial cell state c0
    # using `Input`
    a0 = Input(shape=(n_a,), name='a0')
    c0 = Input(shape=(n_a,), name='c0')
    a = a0
   
    outputs = []
    
   
    for t in range(Tx):
        
        
        x = Lambda(lambda x: X[:,t,:])(X)
       
        x = reshaper(x)
        
        a, _, c = LSTM_cell(x, initial_state=[a, c])
      
        out = densor(a)
       
        outputs.append(out)
        
    
        model = Model(inputs=[X, a0, c0], outputs=outputs)
    
   
    
    return model

model = djmodel(Tx=30, LSTM_cell=LSTM_cell, densor=densor, reshaper=reshaper)


opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)

model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])


model.summary()

m = 60
a0 = np.zeros((m, n_a))
c0 = np.zeros((m, n_a))

history = model.fit([X, a0, c0], list(Y), epochs=100, verbose=0)

The error happens exactly at model.fit for some reason it does not fit the model

I have spend a little over a week trying to pass it but I keep getting not sure what causes the error and would like some guidance or help on how to fix it please

Thank you in advance

1 Answers

This one worked for me:

    # Step 2.A: select the "t"th time step vector from X. 
    x = X[:,t,:]
    # Step 2.B: Use reshaper to reshape x to be (1, n_values) (≈1 line)
    x = reshaper(x)
    # Step 2.C: Perform one step of the LSTM_cell
    a, _, c = LSTM_cell(x, initial_state=[a, c])
    # Step 2.D: Apply densor to the hidden state output of LSTM_Cell
    out = densor(a)
    # Step 2.E: add the output to "outputs"
    outputs.append(out)
Related