LSTM model.fit问题您的图层或模型处于无效状态

2024-10-05 10:08:21 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我希望一切都好

在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

以下代码如下:

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)

错误正好发生在model.fit 由于某些原因,它不符合模型

我花了一个多星期的时间试图通过考试,但我一直不确定是什么原因导致了错误,希望得到一些关于如何修复错误的指导或帮助

先谢谢你


Tags: theininputmodel错误celloutputsa0
1条回答
网友
1楼 · 发布于 2024-10-05 10:08:21

这一个对我有用:

    # 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)

相关问题 更多 >

    热门问题