how to calculate the euclidean distance between the vectors in one matrix?
up vote
3
down vote
favorite
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 |
up vote
3
down vote
favorite
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 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
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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 at 9:22
blue-phoenox
3,59681440
3,59681440
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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 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 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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 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 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
add a comment |
up vote
2
down vote
accepted
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 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 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
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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 at 10:33
answered Nov 20 at 10:14
jdehesa
21.8k43150
21.8k43150
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 withtf.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
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 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 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
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.
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.
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 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