Exemplo n.º 1
0
def get_grad_vid(model, input_images, input_states, car_sizes, device='cuda'):
    input_images, input_states = input_images.clone(), input_states.clone()
    input_images, input_states = utils.normalize_inputs(input_images,
                                                        input_states,
                                                        model.policy_net.stats,
                                                        device=device)
    input_images.requires_grad = True
    input_states.requires_grad = True
    input_images.retain_grad()
    input_states.retain_grad()

    proximity_cost, _ = utils.proximity_cost(input_images[:, -1:],
                                             input_states.data[:, -1:],
                                             car_sizes,
                                             unnormalize=True,
                                             s_mean=model.stats['s_mean'],
                                             s_std=model.stats['s_std'])
    proximity_loss = torch.mean(proximity_cost)
    lane_cost, _ = utils.lane_cost(input_images[:, -1:], car_sizes)
    lane_loss = torch.mean(lane_cost)

    opt = model.policy_net.options
    loss = proximity_loss + \
           opt.lambda_l * lane_loss
    loss.backward()

    return input_images.grad[:, -1, :3].abs().clamp(max=1.)
    y = pre_loaded_data['y'].T.values
    x_train = pre_loaded_data['x_train'].T.values
    x_cv = pre_loaded_data['x_cv'].T.values
    x_test = pre_loaded_data['x_test'].T.values
    y_train = pre_loaded_data['y_train'].T.values
    y_cv = pre_loaded_data['y_cv'].T.values
    y_test = pre_loaded_data['y_test'].T.values

# Model setup ----------------------------------------------------------------------------------------------------------
num_features = x.shape[0]
num_examples = x.shape[1]
assert (y.shape[1] == x.shape[1]
        )  # Check that input and outputs have same num_examples

x_train_normalized = np.log(x_train)
x_train_normalized, mu, sigma = normalize_inputs(x_train_normalized)
x_cv_normalized = np.log(x_cv)
x_test_normalized = np.log(x_test)
x_cv_normalized, _, _ = normalize_inputs(x_cv_normalized, mu, sigma)
x_test_normalized, _, _ = normalize_inputs(x_test_normalized, mu, sigma)
model_parameters['mu'] = mu
model_parameters['sigma'] = sigma

# Initialize Neural Network
fx_prediction_model = SoftmaxNN(alpha=alpha,
                                num_epochs=num_epochs,
                                mini_batch_size=mini_batch_size,
                                adam_beta1=adam_beta1,
                                adam_beta2=adam_beta2,
                                adam_epsilon=adam_epsilon,
                                lambda_reg=lambda_reg,
# Initialize Neural Network
fx_prediction_model = SoftmaxNN(alpha=m['alpha'],
                                num_epochs=m['num_epochs'],
                                mini_batch_size=m['mini_batch_size'],
                                adam_beta1=m['adam_beta1'],
                                adam_beta2=m['adam_beta2'],
                                adam_epsilon=m['adam_epsilon'],
                                lambda_reg=m['lambda_reg'],
                                drop_rate=m['drop_rate'],
                                seed=m['seed'],
                                layer_dims=m['layer_dims'],
                                activation_functions=m['activation_functions'])

print('Total data set Model Performance ------------------')
x_normalized = np.log(x)
x_normalized, _, _ = normalize_inputs(x_normalized, m['mu'], m['sigma'])
full_data_metrics, full_data_predictions = fx_prediction_model.evaluate_model(
    x_normalized, y, trained_model)

# Daily time series over the entire data set of prediction values ------------------------------------------------------
writer = pd.ExcelWriter(output_root + 'evaluation_full_data.xlsx')
pd.DataFrame(x.T, index=eval_data.index[:-1],
             columns=eval_data.columns).to_excel(writer, 'inputs')
pd.DataFrame(full_data_predictions,
             index=eval_data.index[:-1],
             columns=y_labels).to_excel(writer, 'predictions')
pd.DataFrame(y.T, index=eval_data.index[:-1],
             columns=y_labels).to_excel(writer, 'outputs')
writer.close()