question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confused about how to implement 2d input and 2d output regression model

See original GitHub issue

I’m trying to build a regression model with 2d input and 2d output but I’m getting a bit confused about how to implement this. Taking the tutorial Coregionalized Regression Model as an example, I would just like to modify the model such that f_output1 and f_output2 are functions of two variables. I then thought I could simply change input_dim = 2 everywhere but this does not seem to work.

I’m pasting an example of the sort of thing I was trying below

f_output1 = lambda x, y: 4. * np.cos(x/5.) - .4*x - 35. + np.random.rand(x.size)[:,None] * 2. + y  
f_output2 = lambda x, y: 6. * np.cos(x/5.) + .2*x + 35. + np.random.rand(x.size)[:,None] * 8. + 2*y
X1 = np.random.rand(100)[:,None]; X1=X1*75
X2 = np.random.rand(100)[:,None]; X2=X2*70 + 30

X2d = np.array([(X1.T)[0],(X2.T)[0]]).T # Put input data in the form I was expecting to be necessary based on multiple input examples in other tutorials

Y1 = f_output1(X1,X2)
Y2 = f_output2(X1,X2)

K = GPy.kern.Matern32(1)
icm = GPy.util.multioutput.ICM(input_dim=2,num_outputs=2,kernel=K)
m = GPy.models.GPCoregionalizedRegression([X2d,X2d],[Y1,Y2],kernel=icm)
m['.*Mat32.var'].constrain_fixed(1.) #For this kernel, B.kappa encodes the variance now.
m.optimize()
print(m)

Any help would be much appreciated. I’m very new to all this. Also, many thanks for your fantastic work.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
SiyuanHuang95commented, Jun 11, 2019

@ric70x7 Yup! Now it appears to be working! THANK YOU!!!

Quick Question. So now you have a input_dim = 2, output_dim = 2, but how could you make a prediction, with m(np.asarray(X2d),Y_,etadata)? It always ask me: AssertionError: need at least column vectors as inputs to kernels for now, given X2.shape=(1, 100, 2) Any help is greatful!

0reactions
gehbiszumeiscommented, Apr 9, 2021
def convert_input_for_multi_output_model(x, num_outputs):
    """
    This functions brings test data to the correct shape making it possible to use the `predict()` method of a trained
    `GPy.util.multioutput.ICM` model (in the case that all outputs have the same input data).

    Behind the scenes, this model is using an extended input space with an additional dimension that points at the
    output each data point belongs to. To make use of the prediction function of GPy, this model needs the input array
    to have the extended format, i.e. adding a column indicating which input should be used for which output.

    This function works also for input dimensions > 1.

    :param x: the x data you want to predict on
    :param num_outputs: The number of outputs in your model
    """

    xt2d = np.array([x[:, i] for i in range(x.shape[1])]).T
    xt = [xt2d] * num_outputs
    identity_matrix = np.hstack([np.repeat(j, _x.shape[0]) for _x, j in zip(xt, range(num_outputs))])
    xt = np.vstack(xt)
    xt = np.hstack([xt, identity_matrix[:, None]])

    return xt

@SiyuanHuang95 To make predictions your inputs must be reshaped the same way as the model does shape it internally during training (i.e. adding an additional dimension indicating which input belongs to which output).

In the case that all outputs have common input values, this function might help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Develop Multi-Output Regression Models with Python
Multioutput regression are regression problems that involve predicting two or more numerical values given an input example.
Read more >
Understanding Input Output shapes in Convolution Neural ...
Thus we have to change the dimension of output received from the convolution layer to a 2D array. Snippet-3. We can do it...
Read more >
Machine learning with 2D matrix input and numerical output ...
I have a dataset, with a 2D matrix input for each point, and the corresponding target output value for that point. I am...
Read more >
Keras: 2D input -> 2D output? - neural network - Stack Overflow
I'm quite new to keras and neural nets, and I only know how to deal with the label when it is one dimensional...
Read more >
Keras: Multiple Inputs and Mixed Data - PyImageSearch
Multi-output models; Models that are both multiple input and multiple output; Directed acyclic graphs; Models with shared layers. For example, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found