I have the next function:
def np_get_matrix(nq, array):
full = np.empty((nq, nq))
offset = 0
for i in range(nq):
for j in range(nq):
if j > i:
full[i, j] = array[offset, 0]
offset += 1
elif j < i:
full[i, j] = full[j, i]
else:
full[i, j] = 0
full[i, i] = 1 - full[i].sum()
return full
It takes an array of size nq * (nq – 1) // 2 and builds a matrix to be multiplied to a column vector of size nq. When using numpy, everything works.
The problem is, I want to use this matrix in a custom keras layer, and “array” is not a numpy array but a keras tensor, so I can’t (or I don’t know how) retrieve its values (symbolic tensor).
How can I adapt this function to work with a keras symbolic tensor? The function is only called inside the layer’s call method.
I’ve already tried changing the “full” variable to a tensorflow variable. Even so, tensorflow says I can’t assign values to it.
def tf_get_matrix(nq, weights):
full = tf.Variable(lambda : tf.zeros((nq, nq)))
offset = 0
for i in range(nq):
full[i, :i+1] = weights[offset:nq-i+offset]
offset += nq - i
for j in range(i):
if j < i:
full[i, j] = full[j, i]
sums = tf.reduce_sum(full, 1)
for i in range(nq):
full[i, i] = 1 - sums[i]
return full