예제 #1
0
def get_valid_loss(model, x_develop, y_develop, batch_size=32):
    loss = []
    for batch in range(0, len(x_develop), batch_size):
        model.reset_states()
        if batch + batch_size > len(x_develop):
            pass
        else:
            batch_x = fix_x_batch(x_train[batch:batch +
                                          batch_size, :, :].copy())
            batch_y = fix_y_batch_nomag(
                y_train[0][batch:batch + batch_size, :, :].copy(),
                y_train[1][batch:batch + batch_size, :, :].copy())
            loss.append(model.test_on_batch(batch_x, batch_y)[0])
    model.reset_states()
    return np.mean(loss)
예제 #2
0
def fix_data(x, y):
    new_x = fix_x_batch(x.copy())
    new_y = y.copy()

    #Renumber eq to be sequential
    for i, v in enumerate(y):
        evs = list(set(y[i, :, 0]))
        for n, pick in enumerate(v):
            new_y[i, n, 0] = evs.index(y[i, n, 0])

    #Length here is the maximum number of eq+1
    length = int(np.max(evs) + 1)

    #Create output sequence targets 1 for eq 0 otherwise
    neq = np.squeeze(np.max(new_y, axis=1))
    decoder_targets = np.zeros((len(y), length, 1))
    for b, v in enumerate(neq):
        decoder_targets[b, :int(v) + 1, 0] = 1
    #Create decoder inputs, which will be just zeros always (but the number of zeros determins the number of decoder steps)
    decoder_inputs = np.zeros((len(x), length, 1))

    output = ([new_x,
               decoder_inputs], [decoder_targets.astype('int32'), new_y])
    return output
예제 #3
0
    )  # from the available weeks of synthetic data select a subset
    [avail_index.remove(i)
     for i in batch_i]  # remove the batches selected from the available weeks
    valid_losses = []
    epoch_losses = []
    best_loss = None
    print('Epoch ' + str(epochs))
    while True:
        _bloss = []
        end = np.random.choice(
            range(50, 500)
        ) + start  # This is used to pull only a small time slice of each batch
        if end >= length:
            end = length - 1
        # Station longitude, station latitude, elevation, phase, pick time
        batch_x = fix_x_batch(x_train[batch_i,
                                      start:end, :].copy())  # get inputs
        # Source longitude, latitude, depth, travel time
        batch_y = fix_y_batch_nomag(
            y_train[0][batch_i, start:end, :].copy(),
            y_train[1][batch_i, start:end, :].copy())  # get outputs

        loss = model.train_on_batch(batch_x,
                                    batch_y)  # run a single gradient update
        _bloss.append(loss)  # append the loss

        start = end
        model.reset_states()  # clears network hidden states

        if start >= length - 1:  # if youve reached the end of the batch
            print('End of batch ' + str(len(avail_index) // batch_size) +
                  ' steps left')
예제 #4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 11 12:09:56 2020

Uses trained associator to make predictions

@author: searcy
"""

import numpy as np
import matplotlib.pyplot as plt
from asso_data import x_test, y_test
from associator import build_model
from asso_utils import fix_x_batch, decode_y
from sklearn.cluster import MeanShift, estimate_bandwidth

batch_size = 1
model = build_model(batch_size)
model.load_weights('best.tf')
pred = model.predict(fix_x_batch(x_test), batch_size=batch_size)
reg, cl = pred
reg_d = decode_y(pred)
np.save('predictions/Station_Regression', reg_d)
np.save('predictions/Station_Prediction', cl)
예제 #5
0
    y_train = [all_data[train, :, 5:-1], all_data[train, :, -1:]]
    y_test = [all_data[test, :, 5:-1], all_data[test, :, -1:]]
    y_develop = [all_data[develop, :, 5:-1], all_data[develop, :, -1:]]

    tag = 'v5'
    c = Checkpoint(tag=tag, restore=True)
    if os.path.exists(c.model_path):
        model = tf.keras.models.load_model('cp/' + tag + '_model.h5',
                                           custom_objects={
                                               'loss': loss,
                                               'lon_loss': lon_loss,
                                               'lat_loss': lat_loss,
                                               'depth_loss': depth_loss,
                                               'time_loss': time_loss
                                           })
    else:
        model = build_model()

    xt = fix_x_batch(x_train.copy())
    y_rt, y_lt = fix_y_batch_nomag(*y_train.copy())

    xd = fix_x_batch(x_develop.copy())
    y_rd, y_ld = fix_y_batch_nomag(*y_develop.copy())

    m = model.fit(xt, [y_rt, y_lt],
                  validation_data=(xd, [y_rd, y_ld]),
                  epochs=500,
                  callbacks=[c],
                  batch_size=128)