如何在Keras中实现自定义图层? def调用中的参数'x'是什么意思?
我需要实现具有(num,4,4)权重的层,它将(num,4,4)数组作为输入并将每个切片(每个num(4,4)输入数组)乘以相应的权重切片。所以我想要(num,1,1)输出。
我尝试这样做:
def call(self, x, mask=None):
num = self.input_len
res=np.zeros((num,1,1))
for i in range(num):
print(x[i,:,:])
res[i,0,0]=K.dot(x[i,:,:], self.W[i,:,:])
return res
但我有错误:
/home/universal/anaconda3/envs/practicecourse2/mylayer.py in call(self, x, mask)
26 for i in range(num):
27 print(x[i,:,:])
---> 28 res[i,0,0]=K.dot(x[i,:,:], self.W[i,:,:])
29 return res
30
ValueError: setting an array element with a sequence.
我的代码出了什么问题?
答案 0 :(得分:1)
我应该使用
res= K.sum(x*self.W,axis=(2,3))
而不是
for i in range(num):
print(x[i,:,:])
res[i,0,0]=K.dot(x[i,:,:], self.W[i,:,:])