简体   繁体   中英

How to convert this model to a Keras sequential model?

I want to have this model as a sequential mode, but not sure how. I want to be in the typical model.sequential() then model.add style. But I don't know how to do this with autoencoders.

query_layer = tf.keras.layers.Conv1D(filters=100, kernel_size=4, padding='same')
value_layer = tf.keras.layers.Conv1D(filters=100, kernel_size=4, padding='same')

attention = tf.keras.layers.Attention()
concat = tf.keras.layers.Concatenate()

cells = [tf.keras.layers.LSTMCell(256), tf.keras.layers.LSTMCell(64)]
rnn = tf.keras.layers.RNN(cells)
output_layer = tf.keras.layers.Dense(1)

for batch in ds['train'].batch(32):
    text = batch['text']
    embeddings = embedding_layer(vectorize_layer(text))
    query = query_layer(embeddings)
    value = value_layer(embeddings)
    query_value_attention = attention([query, value])
    print("Shape after attention is (batch, seq, filters):", query_value_attention.shape)
    attended_values = concat([query, query_value_attention])
    print("Shape after concatenating is (batch, seq, filters):", attended_values.shape)
    logits = output_layer(rnn(attended_values))
    loss = tf.keras.losses.binary_crossentropy(tf.expand_dims(batch['label'], -1), logits, from_logits=True)
    print(loss)

It is impossible because topology of your model is not linear.

Try Functional API:

input = tf.keras.Input(tf.shape(text))
embeddings = embedding_layer(vectorize_layer(text))
query = query_layer(embeddings)
value = value_layer(embeddings)
query_value_attention = attention([query, value])
attended_values = concat([query, query_value_attention])
logits = output_layer(rnn(attended_values))
model = tf.keras.Model(input, logits)

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