将未经处理的输入添加到神经网络中的后续层

huangapple go评论63阅读模式
英文:

Adding unprocessed input to later layers in a neural network

问题

抱歉,我无法完成您的请求。

英文:

I apologize if this is a bit of an open-ended question. I'm working in R, using the keras library; I'd like to train a neural network to approximate a function f(y, x) where I know that the true f is increasing in y (but not in x). y is a scalar, while x is a vector.

I need the estimate of f represented by the trained NN -- let's call it g -- to be increasing in y as well. My idea was to use non-negativity constraints on the weights of any node that has y among its ancestors; nodes that only process x have unconstrained weights so that non-monotonous responses to changes in x are possible.

How do I do this in keras? Well, one idea I had was to

  1. process x in a number of suitable layers with unconstrained weights, and then
  2. process the output of those layers (let's call it z) and y in further layers in which all weights are constrained to be non-negative.

Neither step is difficult in isolation, but what I'm having difficulty with is figuring out how to, in essence, feed a new input to the NN after other input has already been processed.

It's not as easy as splitting the whole NN in two, of course, i.e. gluing two NNs together. There is no single "right" z resulting from the initial processing of x; in fact I cannot even measure how "good" the intermediate result z is at all, I can only evaluate the overall response g(y, x) by comparing it to the known f(y, x) in my training data.

That said, perhaps there is an easy/straightforward way of architecting1 the NN in such a way that y passes the initial layers unprocessed and only gets used as an input in one of the later layers.

I've looked at the keras documentation as well as searched Google etc., but to no avail. I'm no expert on neural networks or any kind of statistical learning, nor on keras (much less TensorFlow and whatever else underlies it). I'd merely like to use this keras for my application needs, and could use a little not-too-technical help re: how to do the above.

Side note: I'm not too hung up on my specific approach. Ultimately, anything that gives me an NN for which g(y, x) is guaranteed to be increasing in y but not in x is fine with me.

Thank you!

  1. I hope that's a word.

答案1

得分: 0

回答自己,是的,这似乎是可能的;然而,神经网络不能是顺序的,特别是不能使用 keras_model_sequential()。相反,

  1. 使用 layer_input(shape = ..., name = "...") 创建两个单独的输入层(命名为 xy),并分别赋值给 input_xinput_y
  2. 将输入层 x 导入所需的处理层,权重不受限制,将结果的 "构建块" 赋值给 intermediate_z
  3. 使用 layer_concatenate(inputs = list(intermediate_z, input_y)) 将这个构建块和输入层 y 结合,赋值给 output_f
  4. 将其传递到进一步处理层,权重受限制;
  5. 使用 keras_model(inputs = list(input_x, input_y), output = output_f) 创建神经网络;
  6. 使用 compile() 像往常一样编译神经网络;
  7. 使用 fit(list("x" = train_x, "y" = train_y), train_f, ...) 训练神经网络(其中列表元素的名称对应于您在步骤 1 中分配给输入层的名称);
  8. 使用 predict((list("x" = new_x, "y" = new_y)) 预测新值。

我发现这个有关TensorFlow功能性API的指南很有帮助。

英文:

Answering myself, yes, this seems to be possible; however, the NN cannot be sequential, and in particular you cannot use keras_model_sequential(). Instead,

  1. create two separate input layers using layer_input(shape = ..., name = "...") (name them x and y, say), and assign to e.g. input_x and input_y;
  2. pipe the input layer for x into the desired processing layers with unconstrained weights, and assign the resulting "building block" of the NN to e.g. intermediate_z;
  3. combine this building block and the input layer for y using layer_concatenate(inputs = list(intermediate_z, input_y)) and assign to e.g. output_f;
  4. pipe this into further processing layers with constrained weights;
  5. create the NN as keras_model(inputs = list(input_x, input_y), output = output_f);
  6. compile the NN as usual using compile();
  7. train the NN using fit(list("x" = train_x, "y" = train_y), train_f, ...) (where the names of the list elements correspond to the names you assigned to the input layers in step 1);
  8. predict new values using predict((list("x" = new_x, "y" = new_y)).

I found this guide to the functional TensorFlow API helpful.

huangapple
  • 本文由 发表于 2023年3月8日 17:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定