コード例 #1
0
def train(text, epochs=100, save_freq=10):

    # character to index and index to char mappings
    char_to_idx = {ch: i for (i, ch) in enumerate(sorted(list(set(text))))}
    print("Number of unique characters: " + str(len(char_to_idx)))  #86

    with open(os.path.join(DATA_DIR, 'char_to_idx.json'), 'w') as f:
        json.dump(char_to_idx, f)

    idx_to_char = {i: ch for (ch, i) in char_to_idx.items()}
    vocab_size = len(char_to_idx)

    #model_architecture
    model = build_model(BATCH_SIZE, SEQ_LENGTH, vocab_size)
    model.summary()
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    #Train data generation
    T = np.asarray(
        [char_to_idx[c] for c in text],
        dtype=np.int32)  #convert complete text into numerical indices

    print("Length of text:" + str(T.size))  #129,665

    steps_per_epoch = (len(text) / BATCH_SIZE - 1) / SEQ_LENGTH

    log = TrainLogger('training_log.csv')

    for epoch in range(epochs):
        print('\nEpoch {}/{}'.format(epoch + 1, epochs))

        losses, accs = [], []

        for i, (X, Y) in enumerate(read_batches(T, vocab_size)):

            print(X)

            loss, acc = model.train_on_batch(X, Y)
            print('Batch {}: loss = {}, acc = {}'.format(i + 1, loss, acc))
            losses.append(loss)
            accs.append(acc)

        log.add_entry(np.average(losses), np.average(accs))

        if (epoch + 1) % save_freq == 0:
            save_weights(epoch + 1, model)
            print('Saved checkpoint to', 'weights.{}.h5'.format(epoch + 1))
コード例 #2
0
    Bidirectional(GRU(units=50, return_sequences=True)),
    tfa.layers.GroupNormalization(),
    Dropout(0.2),
    Bidirectional(GRU(units=50, return_sequences=True)),
    tfa.layers.GroupNormalization(),
    Dropout(0.2),
    Bidirectional(LSTM(units=50)),
    tfa.layers.GroupNormalization(),
    Dropout(0.2),
    Dense(units=1, activation='sigmoid')
]
name = 'bgru_lstm'
epochs = 100
batch_size = 32

# Training the model.
model = rnn_model.build_model(input_shape, layers)
model, history = rnn_model.train(model,
                                 name,
                                 x_train,
                                 y_train,
                                 epochs=epochs,
                                 batch_size=batch_size)

# Making predictions.
y_predict = model.predict(x_test)

# Visualising the prediction.
stocks.plot_prediction(y_test, y_predict, 'Real Tesla Stock Price',
                       'Predicted Tesla Stock Price')