pfc
pfc

Reputation: 1910

Tensorflow: How to compute the square error between a tensor and a sparse tensor

I have two tensors in tensorflow, one is a sparse tensor, called A, the other is a tensor, call B. I want to compute the square error between A and B.

When I perform:

import tensorflow as tf
tf.reduce_sum(tf.square( B - A))

then I got an error saying :

TypeError: Expected float32 passed to parameter 'y' of op 'Sub', got <tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x7f42a10bdf90> of type 'SparseTensor' instead.

Then I tested:

tf.reduce_sum(tf.sparse_add(layer_Bi_12, - Bi))

the error says:

TypeError: bad operand type for unary -: 'SparseTensor'

Then I tested:

tf.reduce_sum(tf.square(tf.sparse_add(layer_Bi_12, - Bi)))

The error says:

TypeError: bad operand type for unary -: 'SparseTensor'

How can I achieve this?

Upvotes: 1

Views: 474

Answers (1)

Jonas Adler
Jonas Adler

Reputation: 10759

You cannot add sparse and dense tensors. You need to first convert the sparse tensor to a dense tensor, e.g.

dense_A = tf.sparse_tensor_to_dense(A)
tf.reduce_sum(tf.square(B - dense_A))

Upvotes: 1

Related Questions