how to calculate the euclidean distance between the vectors in one matrix?












3














I want to calculate the Euclidean distance of the vectors in the features, which is a tf.Tensor got from the network.



I tried it in the following way, but failed with error:



'Tensor' object is not iterable


So I want to calculate the distance between the rows in one matrix just through matrix,without iteration of every rows.



features, _ = mnist_net(images)
feature_matrix = np.zeros(shape=(FLAGS.batch_size,FLAGS.batch_size))
for i in range (FLAGS.batch_size):
for j in range (FLAGS.batch_size):
aa = tf.slice(features,[i,0],[1,50])
bb = tf.slice(features,[j,0],[1,50])
feature_matrix[i,j] = tf.sqrt(sum((aa-bb)**2))









share|improve this question
























  • What have you tried so far? It is better to give some code and explain your attempt.
    – Banghua Zhao
    Nov 20 at 1:30










  • What do you mean by distance here? There are a lot of distance measures
    – user7374610
    Nov 20 at 2:37










  • I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
    – Bobo Xi
    Nov 20 at 4:00
















3














I want to calculate the Euclidean distance of the vectors in the features, which is a tf.Tensor got from the network.



I tried it in the following way, but failed with error:



'Tensor' object is not iterable


So I want to calculate the distance between the rows in one matrix just through matrix,without iteration of every rows.



features, _ = mnist_net(images)
feature_matrix = np.zeros(shape=(FLAGS.batch_size,FLAGS.batch_size))
for i in range (FLAGS.batch_size):
for j in range (FLAGS.batch_size):
aa = tf.slice(features,[i,0],[1,50])
bb = tf.slice(features,[j,0],[1,50])
feature_matrix[i,j] = tf.sqrt(sum((aa-bb)**2))









share|improve this question
























  • What have you tried so far? It is better to give some code and explain your attempt.
    – Banghua Zhao
    Nov 20 at 1:30










  • What do you mean by distance here? There are a lot of distance measures
    – user7374610
    Nov 20 at 2:37










  • I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
    – Bobo Xi
    Nov 20 at 4:00














3












3








3


0





I want to calculate the Euclidean distance of the vectors in the features, which is a tf.Tensor got from the network.



I tried it in the following way, but failed with error:



'Tensor' object is not iterable


So I want to calculate the distance between the rows in one matrix just through matrix,without iteration of every rows.



features, _ = mnist_net(images)
feature_matrix = np.zeros(shape=(FLAGS.batch_size,FLAGS.batch_size))
for i in range (FLAGS.batch_size):
for j in range (FLAGS.batch_size):
aa = tf.slice(features,[i,0],[1,50])
bb = tf.slice(features,[j,0],[1,50])
feature_matrix[i,j] = tf.sqrt(sum((aa-bb)**2))









share|improve this question















I want to calculate the Euclidean distance of the vectors in the features, which is a tf.Tensor got from the network.



I tried it in the following way, but failed with error:



'Tensor' object is not iterable


So I want to calculate the distance between the rows in one matrix just through matrix,without iteration of every rows.



features, _ = mnist_net(images)
feature_matrix = np.zeros(shape=(FLAGS.batch_size,FLAGS.batch_size))
for i in range (FLAGS.batch_size):
for j in range (FLAGS.batch_size):
aa = tf.slice(features,[i,0],[1,50])
bb = tf.slice(features,[j,0],[1,50])
feature_matrix[i,j] = tf.sqrt(sum((aa-bb)**2))






python tensorflow






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 9:22









blue-phoenox

3,92181441




3,92181441










asked Nov 20 at 1:26









Bobo Xi

336




336












  • What have you tried so far? It is better to give some code and explain your attempt.
    – Banghua Zhao
    Nov 20 at 1:30










  • What do you mean by distance here? There are a lot of distance measures
    – user7374610
    Nov 20 at 2:37










  • I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
    – Bobo Xi
    Nov 20 at 4:00


















  • What have you tried so far? It is better to give some code and explain your attempt.
    – Banghua Zhao
    Nov 20 at 1:30










  • What do you mean by distance here? There are a lot of distance measures
    – user7374610
    Nov 20 at 2:37










  • I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
    – Bobo Xi
    Nov 20 at 4:00
















What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 at 1:30




What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 at 1:30












What do you mean by distance here? There are a lot of distance measures
– user7374610
Nov 20 at 2:37




What do you mean by distance here? There are a lot of distance measures
– user7374610
Nov 20 at 2:37












I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 at 4:00




I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 at 4:00












1 Answer
1






active

oldest

votes


















2














You can achieve that simply with tf.norm/tf.linalg.norm:



feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)


For example:



import tensorflow as tf

with tf.Session() as sess:
features = tf.placeholder(tf.float32, [None, None])
feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)
print(sess.run(feature_matrix, feed_dict={features: [[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]}))


Output:



[[ 0.        5.196152 10.392304 15.588457]
[ 5.196152 0. 5.196152 10.392304]
[10.392304 5.196152 0. 5.196152]
[15.588457 10.392304 5.196152 0. ]]




EDIT:



If you cannot use tf.norm, the following is an equivalent implementation:



sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1))





share|improve this answer























  • I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
    – Bobo Xi
    Nov 20 at 10:22










  • @BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
    – jdehesa
    Nov 20 at 10:24












  • @ jdehesa The later solution works, thank you very much!
    – Bobo Xi
    Nov 20 at 11:40










  • @BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
    – jdehesa
    Nov 20 at 12:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384948%2fhow-to-calculate-the-euclidean-distance-between-the-vectors-in-one-matrix%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can achieve that simply with tf.norm/tf.linalg.norm:



feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)


For example:



import tensorflow as tf

with tf.Session() as sess:
features = tf.placeholder(tf.float32, [None, None])
feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)
print(sess.run(feature_matrix, feed_dict={features: [[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]}))


Output:



[[ 0.        5.196152 10.392304 15.588457]
[ 5.196152 0. 5.196152 10.392304]
[10.392304 5.196152 0. 5.196152]
[15.588457 10.392304 5.196152 0. ]]




EDIT:



If you cannot use tf.norm, the following is an equivalent implementation:



sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1))





share|improve this answer























  • I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
    – Bobo Xi
    Nov 20 at 10:22










  • @BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
    – jdehesa
    Nov 20 at 10:24












  • @ jdehesa The later solution works, thank you very much!
    – Bobo Xi
    Nov 20 at 11:40










  • @BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
    – jdehesa
    Nov 20 at 12:41
















2














You can achieve that simply with tf.norm/tf.linalg.norm:



feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)


For example:



import tensorflow as tf

with tf.Session() as sess:
features = tf.placeholder(tf.float32, [None, None])
feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)
print(sess.run(feature_matrix, feed_dict={features: [[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]}))


Output:



[[ 0.        5.196152 10.392304 15.588457]
[ 5.196152 0. 5.196152 10.392304]
[10.392304 5.196152 0. 5.196152]
[15.588457 10.392304 5.196152 0. ]]




EDIT:



If you cannot use tf.norm, the following is an equivalent implementation:



sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1))





share|improve this answer























  • I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
    – Bobo Xi
    Nov 20 at 10:22










  • @BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
    – jdehesa
    Nov 20 at 10:24












  • @ jdehesa The later solution works, thank you very much!
    – Bobo Xi
    Nov 20 at 11:40










  • @BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
    – jdehesa
    Nov 20 at 12:41














2












2








2






You can achieve that simply with tf.norm/tf.linalg.norm:



feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)


For example:



import tensorflow as tf

with tf.Session() as sess:
features = tf.placeholder(tf.float32, [None, None])
feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)
print(sess.run(feature_matrix, feed_dict={features: [[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]}))


Output:



[[ 0.        5.196152 10.392304 15.588457]
[ 5.196152 0. 5.196152 10.392304]
[10.392304 5.196152 0. 5.196152]
[15.588457 10.392304 5.196152 0. ]]




EDIT:



If you cannot use tf.norm, the following is an equivalent implementation:



sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1))





share|improve this answer














You can achieve that simply with tf.norm/tf.linalg.norm:



feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)


For example:



import tensorflow as tf

with tf.Session() as sess:
features = tf.placeholder(tf.float32, [None, None])
feature_matrix = tf.linalg.norm(features[:, tf.newaxis] - features, axis=-1)
print(sess.run(feature_matrix, feed_dict={features: [[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]}))


Output:



[[ 0.        5.196152 10.392304 15.588457]
[ 5.196152 0. 5.196152 10.392304]
[10.392304 5.196152 0. 5.196152]
[15.588457 10.392304 5.196152 0. ]]




EDIT:



If you cannot use tf.norm, the following is an equivalent implementation:



sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 10:33

























answered Nov 20 at 10:14









jdehesa

22.2k43150




22.2k43150












  • I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
    – Bobo Xi
    Nov 20 at 10:22










  • @BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
    – jdehesa
    Nov 20 at 10:24












  • @ jdehesa The later solution works, thank you very much!
    – Bobo Xi
    Nov 20 at 11:40










  • @BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
    – jdehesa
    Nov 20 at 12:41


















  • I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
    – Bobo Xi
    Nov 20 at 10:22










  • @BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
    – jdehesa
    Nov 20 at 10:24












  • @ jdehesa The later solution works, thank you very much!
    – Bobo Xi
    Nov 20 at 11:40










  • @BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
    – jdehesa
    Nov 20 at 12:41
















I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
– Bobo Xi
Nov 20 at 10:22




I tried but it didn't work. The error is 'module 'tensorflow' has no attribute 'linalg'' , and the verion of Tensorflow is 1.0.1.
– Bobo Xi
Nov 20 at 10:22












@BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 at 10:24






@BoboXi Oh right, try with tf.norm ... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 at 10:24














@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 at 11:40




@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 at 11:40












@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 at 12:41




@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 at 12:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384948%2fhow-to-calculate-the-euclidean-distance-between-the-vectors-in-one-matrix%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?