英文:
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
- process x in a number of suitable layers with unconstrained weights, and then
- 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!
- I hope that's a word.
答案1
得分: 0
回答自己,是的,这似乎是可能的;然而,神经网络不能是顺序的,特别是不能使用 keras_model_sequential()
。相反,
- 使用
layer_input(shape = ..., name = "...")
创建两个单独的输入层(命名为x
和y
),并分别赋值给input_x
和input_y
; - 将输入层
x
导入所需的处理层,权重不受限制,将结果的 "构建块" 赋值给intermediate_z
; - 使用
layer_concatenate(inputs = list(intermediate_z, input_y))
将这个构建块和输入层y
结合,赋值给output_f
; - 将其传递到进一步处理层,权重受限制;
- 使用
keras_model(inputs = list(input_x, input_y), output = output_f)
创建神经网络; - 使用
compile()
像往常一样编译神经网络; - 使用
fit(list("x" = train_x, "y" = train_y), train_f, ...)
训练神经网络(其中列表元素的名称对应于您在步骤 1 中分配给输入层的名称); - 使用
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,
- create two separate input layers using
layer_input(shape = ..., name = "...")
(name themx
andy
, say), and assign to e.g.input_x
andinput_y
; - 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
; - combine this building block and the input layer for
y
usinglayer_concatenate(inputs = list(intermediate_z, input_y))
and assign to e.g.output_f
; - pipe this into further processing layers with constrained weights;
- create the NN as
keras_model(inputs = list(input_x, input_y), output = output_f)
; - compile the NN as usual using
compile()
; - 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); - predict new values using
predict((list("x" = new_x, "y" = new_y))
.
I found this guide to the functional TensorFlow API helpful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论