how to calculate the euclidean distance between the vectors in one matrix?
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
add a comment |
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
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00
add a comment |
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
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
python tensorflow
edited Nov 20 '18 at 9:22
blue-phoenox
4,226101745
4,226101745
asked Nov 20 '18 at 1:26
Bobo XiBobo Xi
336
336
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00
add a comment |
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00
add a comment |
1 Answer
1
active
oldest
votes
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))
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 '18 at 10:22
@BoboXi Oh right, try withtf.norm
... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
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 '18 at 10:22
@BoboXi Oh right, try withtf.norm
... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
add a comment |
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))
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 '18 at 10:22
@BoboXi Oh right, try withtf.norm
... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
add a comment |
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))
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))
edited Nov 20 '18 at 10:33
answered Nov 20 '18 at 10:14
jdehesajdehesa
24.7k43554
24.7k43554
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 '18 at 10:22
@BoboXi Oh right, try withtf.norm
... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
add a comment |
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 '18 at 10:22
@BoboXi Oh right, try withtf.norm
... 1.0.1 is pretty old though, I'm not sure if that is there either.
– jdehesa
Nov 20 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 10:24
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@ jdehesa The later solution works, thank you very much!
– Bobo Xi
Nov 20 '18 at 11:40
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
@BoboXi Glad it worked. Please considering marking the answer as accepted if you feel it solved your problem.
– jdehesa
Nov 20 '18 at 12:41
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What have you tried so far? It is better to give some code and explain your attempt.
– Banghua Zhao
Nov 20 '18 at 1:30
What do you mean by distance here? There are a lot of distance measures
– zrelova
Nov 20 '18 at 2:37
I have fixed the question, can you help me solve it?@Banghua Zhao@user7374610
– Bobo Xi
Nov 20 '18 at 4:00