简体   繁体   English

如何在keras.backend和keras.layers之间选择?

[英]How to choose between keras.backend and keras.layers?

I found there are a lot of same names in keras.backend or keras.layers , for example keras.backend.concatenate and keras.layers.Concatenate . 我发现keras.backendkeras.layers中有很多相同的名称,例如keras.backend.concatenatekeras.layers.Concatenate I know vaguely that one is for tensor while the other is for layer. 我隐约知道一个是张量,另一个是层。 But when the code is so big, so many function made me confused that which is tensor or which is layer. 但是,当代码太大时,那么多函数使我感到困惑,即张量或层。 Anybody has a good idea to solve this problem? 有人有解决这个问题的好主意吗?

One way I found is to define all placeholders in one function at first, but the function take it as variable may return layers at end, while another function take this layer as variable may return another variable. 我发现的一种方法是首先在一个函数中定义所有占位符,但是该函数将其作为变量可能会在最后返回层,而另一个函数将这一层作为变量可能会返回另一个变量。

You should definitely use keras.layers if there is a layer that achieves what you want to do. 如果存在可以实现您想要的功能的图层,则绝对应该使用keras.layers That's because, when building a model, Keras layers only accept Keras Tensors (ie the output of layers) as the inputs. 这是因为在构建模型时,Keras图层仅接受Keras张量(即图层的输出)作为输入。 However, the output of methods in keras.backend.* is not a Keras Tensor (it is the backend Tensor, such as TensorFlow Tensor ) and therefore you can't pass them directly to a layer. 但是, keras.backend.*中方法的输出不是keras.backend.* Tensor(它是后端Tensor,例如TensorFlow Tensor ),因此您不能将它们直接传递到图层。

Although, if there is an operation that could not be done with a layer, then you can use keras.backned.* methods in a Lambda layer to perform that custom operation/computation. 但是,如果存在无法通过图层完成的操作,则可以在Lambda图层中使用keras.backned.*方法执行该自定义操作/计算。

Note: Keras Tensor is actually the same type as the backend Tensor (eg tf.Tensor ); 注意: Keras Tensor实际上与后端Tensor具有相同的类型(例如tf.Tensor ); however, it has been augmented with some additional Keras-specific attributes which Keras needs when building a model. 但是,它增加了Keras在构建模型时需要的一些其他特定于Keras的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM