seq2seq implémente l'ajout numérique

Titre

Il s'agit d'une implémentation seq2seq simple de l'addition numérique. Le principe est d'utiliser un réseau cyclique pour encoder Query, puis de le copier à la longueur de Answer. Suivi par un réseau neuronal récurrent multicouche. Enfin, softmax ajoute une perte d'entropie croisée.

model = Sequential()
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
# Note: In a situation where your input sequences have a variable length,
# use input_shape=(None, num_feature).
model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, len(chars))))
# As the decoder RNN's input, repeatedly provide with the last output of
# RNN for each time step. Repeat 'DIGITS + 1' times as that's the maximum
# length of output, e.g., when DIGITS=3, max output is 999+999=1998.
model.add(layers.RepeatVector(DIGITS + 1))
# The decoder RNN could be multiple layers stacked or a single layer.
for _ in range(LAYERS):
    # By setting return_sequences to True, return not only the last output but
    # all the outputs so far in the form of (num_samples, timesteps,
    # output_dim). This is necessary as TimeDistributed in the below expects
    # the first dimension to be the timesteps.
    model.add(RNN(HIDDEN_SIZE, return_sequences=True))

# Apply a dense layer to the every temporal slice of an input. For each of step
# of the output sequence, decide which character should be chosen.
model.add(layers.TimeDistributed(layers.Dense(len(chars), activation='softmax')))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()

Le modèle est relativement simple, principalement pour permettre à chacun de regarder le formulaire réseau.Pour le code complet, veuillez vous référer à l'exemple de cas d'utilisation de keras.

Je suppose que tu aimes

Origine blog.csdn.net/cyinfi/article/details/93138777
conseillé
Classement