[英]Lambda layer to perform if then in keras/tensorflow
I'm tearing my hair out with this one.我正在用这个把头发扯掉。
I asked a question over here If then inside custom non-trainable keras layer but I'm still having difficulties.我在这里问了一个问题, 如果然后在自定义的不可训练的 keras 层中,但我仍然遇到困难。
I tried his solution, but it didn't work - I thought I'd post my complete code with his solution我尝试了他的解决方案,但没有用 - 我想我会用他的解决方案发布我的完整代码
I have a custom Keras layer that I want to return specific output from specific inputs.我有一个自定义 Keras 层,我想从特定输入返回特定输出。 I don't want it to be trainable.
我不希望它是可训练的。
The layer should do the following该层应执行以下操作
if input = [1,0] then output = 1
if input = [0,1] then output = 0
Here's the lambda layer code for doing this:这是用于执行此操作的 lambda 层代码:
input_tensor = Input(shape=(n_hots,))
def custom_layer_1(tensor):
if tensor == [1,0]:
resp_1 = np.array([1,],dtype=np.int32)
k_resp_1 = backend.variable(value=resp_1)
return k_resp_1
elif tensor == [0,1]:
resp_0 = np.array([0,],dtype=np.int32)
k_resp_0 = backend.variable(value=resp_0)
return k_resp_0
else:
resp_e = np.array([-1,])
k_resp_e = backend.variable(value=resp_e)
return k_resp_e
print(tensor.shape)
layer_one = keras.layers.Lambda(custom_layer_1,output_shape = (None,))(input_tensor)
_model = Model(inputs=input_tensor, outputs = layer_one)
When i fit my model it always computes -1 despite the inputs.当我拟合我的模型时,尽管有输入,它总是计算 -1。
This is what the model looks like:这是模型的样子:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 2) 0
_________________________________________________________________
lambda_1 (Lambda) (None, None) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
Here's the full code for the model:这是模型的完整代码:
import numpy as np
from keras.models import Model
from keras import layers
from keras import Input
from keras import backend
import keras
from keras import models
import tensorflow as tf
# Generate the datasets:
n_obs = 1000
n_hots = 2
obs_mat = np.zeros((n_obs,n_hots),dtype=np.int32)
resp_mat = np.zeros((n_obs,1),dtype=np.int32)
# which position in the array should be "hot" ?
hot_locs = np.random.randint(n_hots, size=n_obs)
# set the bits:
for row,loc in zip(np.arange(n_obs),hot_locs):
obs_mat[row,loc] = 1
for idx in np.arange(n_obs):
if( (obs_mat[idx,:]==[1,0]).all() == True ):
resp_mat[idx] = 1
if( (obs_mat[idx,:]==[0,1]).all() == True ):
resp_mat[idx] = 0
# test data:
test_suite = np.identity(n_hots)
# Build the network
input_tensor = Input(shape=(n_hots,))
def custom_layer_1(tensor):
if tensor == [1,0]:
resp_1 = np.array([1,],dtype=np.int32)
k_resp_1 = backend.variable(value=resp_1)
return k_resp_1
elif tensor == [0,1]:
resp_0 = np.array([0,],dtype=np.int32)
k_resp_0 = backend.variable(value=resp_0)
return k_resp_0
else:
resp_e = np.array([-1,])
k_resp_e = backend.variable(value=resp_e)
return k_resp_e
print(tensor.shape)
layer_one = keras.layers.Lambda(custom_layer_1,output_shape = (None,))(input_tensor)
_model = Model(inputs=input_tensor, outputs = layer_one)
# compile
_model.compile(optimizer="adam",loss='mse')
#train (even thought there's nothing to train)
history_mdl = _model.fit(obs_mat,resp_mat,verbose=True,batch_size = 100,epochs = 10)
# test
_model.predict(test_suite)
# outputs: array([-1., -1.], dtype=float32)
test = np.array([1,0])
test = test.reshape(1,2)
_model.predict(test,verbose=True)
# outputs: -1
This seems like fairly simple stuff, why isn't it working?这看起来很简单的东西,为什么它不起作用? Thanks
谢谢
There are a few reasons:有几个原因:
(samples, hots)
with a 1D tensor (hots)
.(samples, hots)
与 1D 张量(hots)
。if
while tf
is a tensor framework. if
tf
是一个张量框架,你可能不会用普通的方式获得好的结果。 So, the suggestion is:所以,建议是:
from keras import backend as K
def custom_layer(tensor):
#comparison tensors with compatible shape 2D: (dummy_batch, hots)
t10 = K.reshape(K.constant([1,0]), (1,2))
t01 = K.reshape(K.constant([0,1]), (1,2))
#comparison results - elementwise - shape (batch_size, 2)
is_t10 = K.equal(tensor, t10)
is_t01 = K.equal(tensor, t01)
#comparison results - per sample - shape (batch_size,)
is_t10 = K.all(is_t10, axis=-1)
is_t01 = K.all(is_t01, axis=-1)
#result options
zeros = K.zeros_like(is_t10, dtype='float32') #shape (batch_size,)
ones = K.ones_like(is_t10, dtype='float32') #shape (batch_size,)
negatives = -ones #shape (batch_size,)
#selecting options
result_01_or_else = K.switch(is_t01, zeros, negatives)
result = K.switch(is_t10, ones, result_01_or_else)
return result
Warnings :
警告:
- this layer is not differentiable (it returns constants) - you will not be able to train anything that comes before this layer and if you try you will get "An operation has
None
for gradient" error.此层不可微(它返回常量)-您将无法训练此层之前的任何内容,如果您尝试,您将收到“一个操作
None
梯度”错误。- The input
tensor
cannot be outputs of other layers because you're requiring it to be exact ones or zeros.输入
tensor
不能是其他层的输出,因为您需要它是精确的 1 或 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.