我正在尝试在keras中创建一个具有两个可训练变量的自定义层。我将这两个变量传递给另一个函数,在其中需要获取变量的ndims和形状。但是它显示错误。我的自定义图层代码是-
class MyLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
assert len(input_shape) >= 3
input_dim = input_shape[1:]
print(input_shape)
self.kernel1 = self.add_weight(
shape=self.output_dim[0],input_dim[0]),
name = 'kernel1',
initializer='uniform',
trainable=True)
print(self.kernel1)
self.kernel2 = self.add_weight(
shape=(self.output_dim[1],input_dim[1]),
name = 'kernel2',
initializer='uniform',
trainable=True)
print(self.kernel2)
super(MyLayer, self).build(input_shape) # Be sure to call this at
the end
def call(self, x):
print(x.shape)
mat1 =np.array(self.kernel1)
print(K.shape(mat1))
#print(mat1.ndim)
mat2 =np.array(self.kernel2)
print(K.shape(mat2))
#print(mat2.ndim)
output1 = Myoperation(x,mat1,1)
output = Myoperation(output1,mat2,2)
return output
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
在函数Myoperation中,它需要计算
m2=list(np.shape(M))[1]
; M是mat1或mat2输入代码
错误是-
IndexError:列表索引超出范围 如果我们使用mat1.shape检查形状,我们得到 TypeError:预期的二进制或Unicode字符串,得到
请帮助