考虑到Keras张量,我想用数字1替换对角线。这类似于numpy函数fill_diagonal
。
答案 0 :(得分:1)
让inputTensor
成为您想要更改的张量,size
是维度1和2的大小:
#create a diagonal matrix
diag = K.eye(size)
#make it 3D by adding a dummy batch dimension
diag = K.expand_dims(diag,0)
#get a complement for wich complement + diag = ones
complement = 1 - diag
#annulate the diagonal from the input
noDiagInput = complement * inputTensor
#add diagonal 1
return noDiagInput + diag