Реализация функции в керасе тензор потока - PullRequest
0 голосов
/ 06 июня 2019

Предположим, у меня есть форма трех тензоров (n,1) T,r,E, и я хотел реализовать функцию, которая вычисляет: sum(i,j) (T[j] < T[i]) (r[j] > r[i]) E[j].Как мне продолжить?

Это то, что у меня есть

#tensor examples
T=K.constant([1,4,5])
r=K.constant([.8,.3,.7])
E=K.constant([1,0,1])

# cartesian product of T to compare element wise
c = tf.stack(tf.meshgrid(T, T, indexing='ij'), axis=-1)
cartesian_T = tf.reshape(c, (-1, 2))

# cartesian product of r to compare elemento wise
r = tf.stack(tf.meshgrid(r, r, indexing='ij'), axis=-1)
cartesian_r = tf.reshape(r, (-1, 2))

# TO DO: 
# compare the two columns in cartesian T and cast to integer 1/0 if  
# second column in T less/greater than first column in T => return tensor


# compare the two columns in cartesian E and cast to integer 1/0 if  
# second column in r greater/less than first column in r => return tensor

# multiply previous tensors by a broadcasted version of E, then do K.sum()

Как вы думаете, я на правильном пути?Что бы вы предложили для реализации этого?

1 Ответ

1 голос
/ 06 июня 2019

Попробуйте:

import keras.backend as K
import tensorflow as tf

T=K.constant([1,4,5])
r=K.constant([.8,.3,.7])
E=K.constant([1,0,1])

T_new = tf.less(T[tf.newaxis,:],T[:,tf.newaxis])
r_new = tf.greater(r[tf.newaxis,:],r[:,tf.newaxis])
E_row,_ = tf.meshgrid(E, E)
result = tf.reduce_sum(tf.boolean_mask(E_row,tf.logical_and(T_new,r_new)))

with tf.Session() as sess:
    print(sess.run(result))

#print
2.0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...