# Tensorflow loop to analyze gradients derived from MRI and PET images

The code shown below takes the partial derivative of each pixel of the output fused image of a neural network with respect to input image of the neural network using tensorflow's tf.gradients(Y, X) and compute a Jacobian matrix for each of the two input images MRI and PET. However, the code is really slow since I currently define a for loop by iterating through each pixel to compute the Jacobian matrix.

jacob_matrix_mri = np.zeros((65536,256,256))
jacob_matrix_pet = np.zeros((65536,256,256))

count = 0
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess,'/home/nora/Desktop/MBIA 2019/Checkpoint/')
for i in range(image_width):
for j in range(image_length):
feed_dict{images_mri:test_mri,
images_pet:test_pet})

count = count+1
if count % 1000 == 0:
print count
t2 = time.time()
print (t2-t1)
t3 = time.time()

• What are you looking to get out of a review? The “however it’s very slow” bit makes it sound like you’d like tips on speeding up the code, but you don’t actually ask this... Wherever you post a question, the more explicit you are, the more likely you are of getting a useful answer! – Cris Luengo Jun 19 '19 at 2:00