如何为单输入和多输出训练回归模型? [英] How to train a Regression model for single input and multiple output?

查看:505
本文介绍了如何为单输入和多输出训练回归模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个回归模型,该模型可以近似计算方程的权重: Y = R + B + G 为此,我提供了R,B,G和Y的预定值作为训练数据,并且在训练了模型之后,该模型能够针对给定的R,B和G值成功地预测Y的值.具有3个输入的神经网络,具有2个神经元的1个密集层(隐藏层)和具有单个神经元的输出层(输出).

I have trained a regression model that approximates the weights for the equation : Y = R+B+G For this, I provide pre-determined values of R, B and G and Y, as training data and after training the model, the model is successfully able to predict the value of Y for given values of R, B and G. I used a neural network with 3 inputs, 1 dense layer (hidden layer) with 2 neurons and the output layer (output) with a single neuron.

    hidden = tf.keras.layers.Dense(units=2, input_shape=[3])
    output = tf.keras.layers.Dense(units=1)

但是,我需要实现相反的功能.即,我需要训练一个模型,该模型采用Y值并预测与该Y值相对应的R,B和G值. 我刚刚了解到,回归完全是关于单个输出的.因此,我无法想到解决方案及其解决之道. 请帮助.
(PS是否可以使用我已经训练的模型来做到这一点?我的意思是,一旦确定了R,B和G的权重,是否有可能操纵该模型以使用这些权重来映射Y到R,B和G?)

But, I need to implement the inverse of this. i.e., I need to train a model that takes in value of Y and predicts values of R, B and G that corrspond to that value of Y. I have just learnt that regression is all about a single output. So, I am unable to think of a solution and the path to it. Kindly Help.
(P.S Is it possible to use the model that I have already trained, to do this? I mean, once, the weights have been determined for R, B and G, is it possible to manipulate the model to use these weights to map Y to R, B and G?)

推荐答案

以下是在Tensorflow中使用神经网络开始解决问题的示例.

Here is an example to start solving your problem using neural network in tensorflow.

import numpy as np
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model

X=np.random.random(size=(100,1))
y=np.random.randint(0,100,size=(100,3)).astype(float)   #Regression

input1 = Input(shape=(1,))
l1 = Dense(10, activation='relu')(input1)
l2 = Dense(50, activation='relu')(l1)
l3 = Dense(50, activation='relu')(l2)
out = Dense(3)(l3)

model = Model(inputs=input1, outputs=[out])
model.compile(
    optimizer='adam',
    loss=['mean_squared_error']
    )

history = model.fit(X, [y], epochs=10, batch_size=64)

这篇关于如何为单输入和多输出训练回归模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆