Ejemplo n.º 1
0
def create_rnn_model(input_shape):
    inputs = layers.Input(shape=input_shape)
    x = layers.LSTM(128)(inputs)
    x = layers.RepeatVector(DIGITS + 1)(x)
    x = layers.LSTM(128, return_sequences=True)(x)
    x = layers.TimeDistributed(layers.Dense(len(CHARS)))(x)
    out = layers.Activation('softmax')(x)
    return models.Model(inputs=inputs, outputs=out)
Ejemplo n.º 2
0
def get_multi_io_temporal_model():
    timesteps = 2
    inp_1 = layers.Input(shape=(1, ), name='input_1')
    inp_2 = layers.Input(shape=(1, ), name='input_2')
    x = layers.RepeatVector(timesteps)
    out_1 = layers.TimeDistributed(Bias(), name='output_1')
    out_2 = layers.TimeDistributed(Bias(), name='output_2')

    branch_a = [inp_1, x, out_1]
    branch_b = [inp_2, x, out_2]
    return testing_utils.get_multi_io_model(branch_a, branch_b)
def create_mem_network():
    sentence = layers.Input(shape=(story_maxlen,), dtype=tf.int32)
    encoded_sentence = layers.Embedding(input_dim=vocab_size, output_dim=50)(sentence)
    encoded_sentence = layers.Dropout(0.3)(encoded_sentence)

    question = layers.Input(shape=(query_maxlen,), dtype=tf.int32)
    encoded_ques = layers.Embedding(input_dim=vocab_size, output_dim=50)(question)
    encoded_ques = layers.Dropout(0.3)(encoded_ques)
    encoded_ques = layers.LSTM(50)(encoded_ques)
    encoded_ques = layers.RepeatVector(story_maxlen)(encoded_ques)

    merged = layers.add([encoded_sentence, encoded_ques])
    merged = layers.LSTM(50)(merged)
    merged = layers.Dropout(0.3)(merged)
    preds = layers.Dense(vocab_size, activation=None)(merged)
    return models.Model(inputs=[sentence, question], outputs=preds)
# Try replacing GRU, or SimpleRNN.
RNN = layers.LSTM
HIDDEN_SIZE = 128
BATCH_SIZE = 128
LAYERS = 1

print('Build model...')
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 hidden state 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))))
model.add(layers.Activation('softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
Ejemplo n.º 5
0
                               activation=None)
convolution_17_output = convolution_17(convolution_layer_4_output)

activation_1 = layers.Activation("softmax")
activation_1_output = activation_1(convolution_17_output)

decoder = models.Model(inputs=[input_2], outputs=[activation_1_output])

decoder.summary()

input_3 = layers.Input(shape=(word_length, len(dictionary)))
input_4 = layers.Input(shape=(window_size * 2, word_length, len(dictionary)))

encoder_output_1 = encoder(input_3)

repeat_vector_1 = layers.RepeatVector(window_size * 2)
repeat_vector_1_output = repeat_vector_1(encoder_output_1)

timedistributed_encoder_1 = layers.TimeDistributed(encoder)
timedistributed_encoder_1_output = timedistributed_encoder_1(input_4)

lambda_1 = layers.Lambda(cosine_proximity)
lambda_1_output = lambda_1(
    [repeat_vector_1_output, timedistributed_encoder_1_output])

dense_3 = layers.Dense(1, activation="sigmoid")
dense_3_output = dense_3(lambda_1_output)

decoder_output_1 = decoder(encoder_output_1)

model = models.Model(inputs=[input_3, input_4],
Ejemplo n.º 6
0
y = np.zeros((DATASET_SIZE, OUTPUT, len(chars)), dtype=np.bool)
for i, sentence in enumerate(seq_samples):
    x[i] = rtable.encode(sentence, INPUT)
for i, sentence in enumerate(seq_labels):
    y[i] = dtable.encode(sentence, OUTPUT)

# Train / test split: 3/4 samples for training, 1/4 samples for test
split_at = 3 * DATASET_SIZE // 4
(x_train, x_val) = x[:split_at], x[split_at:]
(y_train, y_val) = y[:split_at], y[split_at:]

# Build the network and train it in a loop
print('Build model...')
model = Sequential()
model.add(layers.LSTM(128, input_shape=(INPUT, len(romans))))
model.add(layers.RepeatVector(OUTPUT))
model.add(layers.LSTM(128, return_sequences=True))
model.add(layers.Dense(len(chars), activation='softmax'))

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

# Train the model each generation and show predictions against the validation dataset.
for iteration in range(1, 500):
    print()
    print('-' * 50)
    print('Iteration', iteration)
    H = model.fit(x_train,
                  y_train,