Beispiel #1
0
variable_std = data.sel(**selection).variables['std'].values

os.makedirs(plot_directory, exist_ok=True)

#%% Make the verification and forecast

verification = verify.verification_from_samples(data.sel(**selection),
                                                forecast_steps=num_plot_steps,
                                                dt=model_dt)

# Scale the verification
if scale_variables:
    verification = verification * variable_std + variable_mean

print('Loading model %s...' % model)
dlwp = load_model(model)

# Build in some tolerance for old models trained with former APIs missing the is_convolutional and is_recurrent
# attributes. This may not always work!
if not hasattr(dlwp, 'is_recurrent'):
    dlwp.is_recurrent = False
    for layer in dlwp.model.layers:
        if 'LSTM' in layer.name.upper() or 'LST_M' in layer.name.upper():
            dlwp.is_recurrent = True
if not hasattr(dlwp, 'is_convolutional'):
    dlwp.is_convolutional = False
    for layer in dlwp.model.layers:
        if 'CONV' in layer.name.upper():
            dlwp.is_convolutional = True
if isinstance(dlwp, DLWPFunctional):
    if not hasattr(dlwp, '_n_steps'):
Beispiel #2
0
for m, model in enumerate(models):
    print('Loading model %s...' % model)

    # Some tolerance for using a weighted loss function. Unreliable but doesn't hurt.
    if 'weight' in model.lower():
        lats = validation_data.lat.values
        output_shape = (validation_data.dims['lat'], validation_data.dims['lon'])
        if crop_north_pole:
            lats = lats[1:]
        customs = {'loss': latitude_weighted_loss(mean_squared_error, lats, output_shape, axis=-2,
                                                  weighting='midlatitude')}
    else:
        customs = None

    # Load the model
    dlwp, history = load_model('%s/%s' % (root_directory, model), True, custom_objects=customs)

    # Assign forecast hour coordinate
    if isinstance(dlwp, DLWPFunctional):
        if not hasattr(dlwp, '_n_steps'):
            dlwp._n_steps = 6
        if not hasattr(dlwp, 'time_dim'):
            dlwp.time_dim = 2
        sequence = dlwp._n_steps
    else:
        sequence = None
    f_hours.append(np.arange(dt, num_forecast_steps * dt + 1., dt))

    # Build in some tolerance for old models trained with former APIs missing the is_convolutional and is_recurrent
    # attributes. This may not always work!
    if not hasattr(dlwp, 'is_recurrent'):
Beispiel #3
0

#%% Make forecasts

model_forecasts = []
if plot_laplace:
    laplace_forecasts = []
else:
    laplace_fill = None
num_forecast_steps = int(np.ceil(plot_forecast_hour / model_dt))
f_hour = np.arange(model_dt, num_forecast_steps * model_dt + 1, model_dt)
dlwp, p_val, t_val = None, None, None

for mod, model in enumerate(models):
    print('Loading model %s...' % model)
    dlwp, history = load_model('%s/%s' % (root_directory, model), True)

    # Create data generator
    if predictor_sel[mod] is not None:
        val_ds = data.sel(**predictor_sel[mod])
    else:
        val_ds = data.copy()
    if crop_north_pole:
        val_ds = val_ds.isel(lat=(val_ds.lat < 90.0))
    val_generator = DataGenerator(dlwp, val_ds, batch_size=216)
    p_val, t_val = val_generator.generate([], scale_and_impute=False)

    # Make a time series prediction and convert the predictors for comparison
    print('Predicting with model %s...' % model_labels[mod])
    time_series = dlwp.predict_timeseries(p_val, num_forecast_steps)
    if scale_variables: