TensorFlow之sparse tensor

打印 上一主题 下一主题

主题 1592|帖子 1592|积分 4776

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
媒介

sparse tensor 希罕tensor, tensor中大部门元素是0, 少部门元素黑白0.
创建sparse tensor

  1. import tensorflow as tf
  2. # indices指示正常值的索引, 即哪些索引位置上是正常值.
  3. # values表示这些正常值是多少.
  4. # indices和values是一一对应的. [0, 1]表示第0行第1列的值是1, [1,0]表示第一行第0列的值是2, [2, 3]表示第2行第3列的值是3, 以此类推.
  5. # dense_shape表示这个SparseTensor的shape是多少
  6. s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],
  7.                     values = [1., 2., 3.],
  8.                     dense_shape = [3, 4])
  9. print(s)
  10. # 把sparse tensor转化为稠密矩阵
  11. print(tf.sparse.to_dense(s))
复制代码
结果如下:
  1. SparseTensor(indices=tf.Tensor(
  2. [[0 1]
  3. [1 0]
  4. [2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
  5. tf.Tensor(
  6. [[0. 1. 0. 0.]
  7. [2. 0. 0. 0.]
  8. [0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
复制代码
sparse tensor的运算:

  1. import tensorflow as tf
  2. # indices指示正常值的索引, 即哪些索引位置上是正常值.
  3. # values表示这些正常值是多少.
  4. # indices和values是一一对应的. [0, 1]表示第0行第1列的值是1, [1,0]表示第一行第0列的值是2, [2, 3]表示第2行第3列的值是3, 以此类推.
  5. # dense_shape表示这个SparseTensor的shape是多少
  6. s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],
  7.                     values = [1., 2., 3.],
  8.                     dense_shape = [3, 4])
  9. print(s)
  10. # 乘法
  11. s2 = s * 2.0
  12. print(s2)
  13. try:
  14.     # 加法不支持.
  15.     s3 = s + 1
  16. except TypeError as ex:
  17.     print(ex)
  18. s4 = tf.constant([[10., 20.],
  19.                   [30., 40.],
  20.                   [50., 60.],
  21.                   [70., 80.]])
  22. # 得到一个3 * 2 的矩阵
  23. print(tf.sparse.sparse_dense_matmul(s, s4))
复制代码
结果如下:
  1. SparseTensor(indices=tf.Tensor(
  2. [[0 1]
  3. [1 0]
  4. [2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
  5. SparseTensor(indices=tf.Tensor(
  6. [[0 1]
  7. [1 0]
  8. [2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([2. 4. 6.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
  9. unsupported operand type(s) for +: 'SparseTensor' and 'int'
  10. tf.Tensor(
  11. [[ 30.  40.]
  12. [ 20.  40.]
  13. [210. 240.]], shape=(3, 2), dtype=float32)
复制代码
注意在定义sparse tensor的时候 indices必须是排好序的. 假如不是,定义的时候不会报错, 但是在to_dense的时候会报错:
  1. import tensorflow as tf
  2. s5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],
  3.                     values = [1., 2., 3.],
  4.                     dense_shape = [3, 4])
  5. print(s5)
  6. #print(tf.sparse.to_dense(s5)) #报错:indices[1] = [0,1] is out of order. Many sparse ops require sorted indices.
  7. # 可以通过reorder对排序, 这样to_dense就没问题了.
  8. s6 = tf.sparse.reorder(s5)
  9. print(tf.sparse.to_dense(s6))
复制代码
结果如下:
  1. SparseTensor(indices=tf.Tensor(
  2. [[0 2]
  3. [0 1]
  4. [2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
  5. tf.Tensor(
  6. [[0. 2. 1. 0.]
  7. [0. 0. 0. 0.]
  8. [0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表