具有高级计算功能的Keras自定义图层 [英] Keras Custom Layer with advanced calculations
问题描述
我想编写一些自定义的Keras
图层并在该图层中进行一些高级计算,例如使用Numpy,Scikit,OpenCV ...
I want to write some custom Keras
Layers and do some advanced calculations in the layer, for example with Numpy, Scikit, OpenCV...
我知道keras.backend
中有一些数学函数可以对张量进行运算,但是我需要一些更高级的函数.
I know there are some math functions in keras.backend
that can operate on tensors, but i need some more advanced functions.
但是,我不知道如何正确执行此操作,我收到错误消息:
You must feed a value for placeholder tensor 'input_1' with dtype float and shape [...]
However, i have no clue how to implement this correctly, i get the error message:
You must feed a value for placeholder tensor 'input_1' with dtype float and shape [...]
这是我的自定义图层:
class MyCustomLayer(Layer):
def __init__(self, **kwargs):
super(MyCustomLayer, self).__init__(**kwargs)
def call(self, inputs):
"""
How to implement this correctly in Keras?
"""
nparray = K.eval(inputs) # <-- does not work
# do some calculations here with nparray
# for example with Numpy, Scipy, Scikit, OpenCV...
result = K.variable(nparray, dtype='float32')
return result
def compute_output_shape(self, input_shape):
output_shape = tuple([input_shape[0], 256, input_shape[3]])
return output_shape # (batch, 256, channels)
此虚拟模型中的错误出现在这里:
The error appears here in this dummy model:
inputs = Input(shape=(96, 96, 3))
x = MyCustomLayer()(inputs)
x = Flatten()(x)
x = Activation("relu")(x)
x = Dense(1)(x)
predictions = Activation("sigmoid")(x)
model = Model(inputs=inputs, outputs=predictions)
感谢所有提示...
推荐答案
我认为这种过程应在模型之前应用,因为该过程不包含变量,因此无法进行优化.
i think that this kinda process should apply before the model because the process does not contain variables so it cant be optimized.
K.eval(inputs)不起作用,因为您正在尝试评估一个占位符,而不是变量占位符没有用于评估的值.如果要获取值,则应该输入它,或者可以使用tf.unstack()一张一张地从张量中列出一个列表
K.eval(inputs) does not work beacuse you are trying to evaluate a placeholder not variable placeholders has not values for evaluate. if you want get values you should feed it or you can make a list from tensors one by one with tf.unstack()
nparray = tf.unstack(tf.unstack(tf.unstack(inputs,96,0),96,0),3,0)
您的调用函数是错误的,因为返回变量,您应该返回常量:
your call function is wrong because returns a variable you should return a constant:
result = K.constant(nparray, dtype='float32')
return result
这篇关于具有高级计算功能的Keras自定义图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!