Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow:如何计算张量和稀疏张量之间的平方误差_Python_Tensorflow_Sparse Matrix - Fatal编程技术网

Python Tensorflow:如何计算张量和稀疏张量之间的平方误差

Python Tensorflow:如何计算张量和稀疏张量之间的平方误差,python,tensorflow,sparse-matrix,Python,Tensorflow,Sparse Matrix,在tensorflow中有两个张量,一个是稀疏张量,称为a,另一个是张量,称为B。我想计算A和B之间的平方误差 当我表演时: import tensorflow as tf tf.reduce_sum(tf.square( B - A)) 然后我得到一个错误,说: TypeError: Expected float32 passed to parameter 'y' of op 'Sub', got <tensorflow.python.framework.sparse_tensor.S

tensorflow
中有两个张量,一个是稀疏张量,称为
a
,另一个是张量,称为
B
。我想计算
A
B
之间的平方误差

当我表演时:

import tensorflow as tf
tf.reduce_sum(tf.square( B - A))
然后我得到一个错误,说:

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

TypeError: bad operand type for unary -: 'SparseTensor'
TypeError: bad operand type for unary -: 'SparseTensor'
然后我测试:

tf.reduce_sum(tf.sparse_add(layer_Bi_12, - Bi))
tf.reduce_sum(tf.square(tf.sparse_add(layer_Bi_12, - Bi)))
错误显示:

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

我怎样才能做到这一点呢?

你不能添加稀疏张量和密集张量。首先需要将稀疏张量转换为稠密张量,例如

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