Ejemplo n.º 1
0
def plot_model(data, model, dim, model_dims=[6, 7, 8, 12, 13, 14], delta=True):
    '''
    Function that takes in data of the form (states, inputs) and plots a specific state variable's ground truth and it's one step prediction. Ground truth is the actual state that occured and prediction is f(x,u) from the learned model. Dimension is the dimension of the state to look at, for simplicity.

    note this would work for 3-tuples of (next state, cur state, input) as well
    '''

    # Data of the form (dx, x, u) where dx, x, and u are sub arrays. Shape of data is (n_sample, 3).
    # data = sequencesXU2array(Seqs_X[:,::samp,:], Seqs_U[:,::samp,:]) from raw data
    dxs = data[:, 0]
    xs = data[:, 1]
    us = data[:, 2]

    if not delta:  # if trained for predicting raw state
        dxs = xs[:]
        # xs = xs[:-1]
        # us = us[:-1]

    # make data into matrices for a little bit easier unpacking
    dxs = np.vstack(dxs)
    xs = np.vstack(xs)
    us = np.vstack(us)

    # print(np.shape(dxs))
    # print(np.shape(xs))

    # Now need to iterate through all data and plot
    predictions = np.empty((0, np.shape(xs)[1]))
    for (dx, x, u) in zip(dxs, xs, us):
        # grab prediction value
        # pred = model.predict(x,u)
        pred = predict_nn(model, x, u, model_dims)
        # print(np.shape(pred))
        predictions = np.append(predictions, pred.reshape(1, -1), axis=0)

    # Debug
    # print(np.shape(predictions))

    # Grab correction dimension data
    ground_dim = dxs[:, dim]
    pred_dim = predictions[:, dim]

    # Sort with respect to ground truth
    ground_dim_sort, pred_dim_sort = zip(*sorted(zip(ground_dim, pred_dim)))

    # Plot
    modelacc_fig = plt.figure()
    title = 'Comparing Ground Truth of Dynamics to Model, Dim:' + str(dim)
    plt.title(title)
    plt.xlabel('Sorted ground truth state index')
    plt.ylabel('State Value')
    plt.plot(pred_dim_sort, label='Predicted Val')
    plt.plot(ground_dim_sort, label='Ground Truth')
    plt.legend()
    plt.show()
    return modelacc_fig
Ejemplo n.º 2
0
def eval(model, dims = [0,1,2,3,4]):
  SE = [0]*len(dims)
  for i in range(0, len(data) - 1):
    predictions = predict_nn(model, data[i][1], data[i][2], [0,1,2,3,4])
    for j,prediction in enumerate(predictions):
      SE[j] += (data[i+1][1][j] - prediction) ** 2
      
  MSE = [0]*len(dims)
  for i,error in enumerate(SE):
    MSE[i] = error / (len(data) - 1)
    
  return MSE
Ejemplo n.º 3
0
def simulate_learned(model, actions, generalNN = True, x0=[]):
    # returns a array of the states predicted by the learned dynamics model given states and the inputs
    if (x0 == []):
        x0 = np.zeros(model.x_dim,1)

    X = [x0]
    for a in actions:
        if generalNN:
            xnext = predict_nn(model, x0, a, model.state_idx_l)
        else:
            xnext = X[-1].flatten() + model.predict(X[-1], a)
        X.append(xnext)

    return np.array(X)
Ejemplo n.º 4
0
def simulate_learned(model, actions, generalNN=True, x0=[]):
    # returns a array of the states predicted by the learned dynamics model given states and the inputs
    if (x0 == []):
        x0 = np.zeros(model.x_dim, 1)

    X = [x0]
    elapsed = 0
    for a in actions:
        if generalNN:
            st = utils_data.millis()
            xnext = predict_nn(model, x0, a, model.state_idx_l)
            et = utils_data.millis()
            elapsed += (et - st)
        else:
            xnext = X[-1].flatten() + model.predict(X[-1], a)
        X.append(xnext)
    #print "NN for ", len(actions), " actions: ", (elapsed / len(actions)), " per action"
    return np.array(X)
Ejemplo n.º 5
0
with open(model_name + "--normparams.pkl", 'wb') as pickle_file:
    pickle.dump((normX, normU, normdX), pickle_file, protocol=2)
time.sleep(2)

quit()
delta = True
pred_dims = [0, 1, 2, 3, 4, 5, 6, 7, 8]
n = np.shape(xs)[0]
print(n)
# Now need to iterate through all data and plot
predictions_1 = np.empty((0, np.shape(xs)[1]))
print(np.shape(predictions_1))
for (dx, x, u) in zip(dxs, xs, us):
    # grab prediction value
    # pred = model.predict(x,u)
    pred = predict_nn(nn1, x, u, pred_dims)

    # print(np.shape(pred))
    #print('prediction: ', pred, ' x: ', x)
    if delta:
        pred = pred - x
    # print(pred)
    predictions_1 = np.append(predictions_1, pred.reshape(1, -1), axis=0)
    # print(pred)
print(np.shape(predictions_1))

# Return evaluation along each dimension of the model
MSE = np.zeros(len(pred_dims))
print(np.shape(dxs))

for i, d in enumerate(pred_dims):