Keras Lambda Layer & Theano代码:访问TensorVariable的维度和值。

时间:2022-12-12 12:57:07

First, I'm very new to Keras and Theano. I'm implementing a CNN which uses local response normalization(LRN). As far as i know there's no such layer implemented in the basic functionality of both keras and theano. So i tried using a keras lambda layer to implement LRN.

首先,我对Keras和Theano非常陌生。我正在实现一个使用本地响应标准化(LRN)的CNN。据我所知,在keras和theano的基本功能中没有实现这一层。所以我尝试使用keras lambda层来实现LRN。

'def local_response_normalization(x): #LRN parameters k = 2 n = 5 alpha = 0.0001 beta = 0.75

'def local_response_正常化(x): #LRN参数k = 2n = 5 alpha = 0.0001 beta = 0.75。

result = x.eval()
x_tmp = x.eval()


#building functions for the theano computation graph
scalar_op = T.dscalar('scalar_op')
matrix1_op = T.dmatrix('matrix1_op')
matrix2_op = T.dmatrix('matrix2_op')
mul_result = scalar_op * matrix1_op
add_result = scalar_op + matrix1_op
pow_result = matrix1_op ** scalar_op
div_result = matrix1_op / matrix2_op

sc_mat_mul_f = function([scalar_op, matrix1_op], mul_result)
sc_mat_add_f = function([scalar_op, matrix1_op], add_result)
sc_mat_pow_f = function([scalar_op, matrix1_op], pow_result)
mat_div_f = function([matrix1_op, matrix2_op], div_result)

#x is supposed to be a 3-dimensional tensor (a x b x c)
a_dim = x_tmp.shape[0]
b_dim = x_tmp.shape[1]
c_dim = x_tmp.shape[2]

#iterating through channels
for i in range(0, a_dim):
    j_l = max(0, i-(n/2))# j_l(ower_bound)
    j_u = min(N-1, i+(n/2))# j_u(pper_bound)

    x_tmp = x.eval()
    #retrieving set of local 'neurons' 
    x_tmp = x_tmp[j_l:j_u+1,:,:]
    #building squared sum
    x_tmp = T.sqr(x_tmp)#TensorVariable
    x_tmp = T.sum(x_tmp, axis=0)#axis no. 0 = 'channel' axis
    #x_tmp is now 2-dimensional
    x_tmp = sc_mat_mul_f(alpha, x_tmp.eval())
    x_tmp = sc_mat_add_f(k, x_tmp)
    x_tmp = sc_mat_pow_f(beta, x_tmp)
    x_tmp = mat_div_f(result[i], x_tmp)
    #exchanging channel i with x_tmp ( = LRN )
    result[i] = x_tmp

return result`

I'm integrating this layer into the model by using model.add(local_response_normalization, ...) When trying to compile and fit the model i get:

我使用模型将这一层集成到模型中。添加(local_response_normalization…)在尝试编译和拟合模型时,我得到:

theano.gof.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute AbstractConv2d{convdim=2, border_mode='half', subsample=(4, 4), filter_flip=True, imshp=(None, 3, 220, 220), kshp=(96, 3, 11, 11), filter_dilation=(1, 1)}(/conv2d_1_input, InplaceDimShuffle{3,2,0,1}.0), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.

theano.gof.fg。missinginpu恐怖:输入0(索引从0开始),用于计算AbstractConv2d{convdim=2, border_mode='half', subsample=(4,4), filter_flip=True, imshp=(None, 3,220, 220), kshp=(96, 3, 11, 11), filter_dilation=(1,1)}(/conv2d_1_输入,InplaceDimShuffle{3,2,0,1} 0),没有提供,没有给出一个值。使用Theano flag exception_verbosity='high',以获得关于此错误的更多信息。

The main issue seems to be that eval() cannot be called during compilation of a model. I can't find another way to access and operate on elements of x (being an output tensor of a conv2d layer) other than converting it into a numpy array by using eval(), but that clearly doesn't work. It seems to me that i'm missing a main concept behind Lambda Layers and TensorVariables in general.
I spent the last two days dealing with this problem and i'm really stuck.

主要问题似乎是在编译模型时不能调用eval()。我无法找到另一种方法来访问和操作x的元素(作为一个conv2d层的输出张量),而不是使用eval()将它转换成一个numpy数组,但是这显然不行。在我看来,我忽略了一个主要的概念,在Lambda层和TensorVariables中。在过去的两天里,我一直在处理这个问题。

1 个解决方案

#1


0  

I've figured out a way to do all computations for LRN without evaluating TensorVariables. Which was kind of obvious.

我已经找到了一种方法来完成LRN的所有计算,而不需要对TensorVariables进行评估。这是显而易见的。

#1


0  

I've figured out a way to do all computations for LRN without evaluating TensorVariables. Which was kind of obvious.

我已经找到了一种方法来完成LRN的所有计算,而不需要对TensorVariables进行评估。这是显而易见的。