Tensorflow Python API 翻译(sparse_ops)

作者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai
简书地址:https://www.jianshu.com/p/c233e09d2f5f


计划现将 tensorflow 中的 Python API 做一个学习,这样方便以后的学习。
原文链接

该章介绍有关稀疏张量的API


稀疏张量表示

对于多维稀疏数据,TensorFlow提供了稀疏张量表示。稀疏张量里面的值都是采用IndexedSlices索引来表示,这样能更加高效的表示数据。


class tf.SparseTensor

解释:这个函数的作用是表示一个稀疏张量。

Tensorflow使用三个密集张量:indicesvaluesdense_shape,来表示一个稀疏张量。在Python接口中,这三个张量被整合到一个SparseTensor类中,如果你调换了这三个密集张量的位置,那么在进行操作之前,SparseTensor类会自动调换三个张量的位置。

具体的说,稀疏张量表示为SparseTensor(values, indices, dense_shape):

  • indices: 一个二维的张量,数据类型是int64,数据维度是[N, ndims]
  • values: 一个一维的张量,数据类型是任意的,数据维度是[N]
  • dense_shape: 一个一维的张量,数据类型是int64,数据维度是[ndims]

其中,N表示稀疏张量中存在N个值,ndims表示SparseTensor的维度。

相应的密集张量满足:

dense.shape = dense_shape
dense[tuple(indices[i])] = values[i]

按照惯例,indices中的索引应该按照从小到大的顺序排序。SparseTensor中三个密集张量的顺序不是强制的,你可以乱序,SparseTensor会自动将它排序。

比如:

SparseTensor(values=[1, 2], indices=[[0, 0], [1, 2]], shape=[3, 4])

那么密集张量就是:

[[1, 0, 0, 0]
 [0, 0, 2, 0]
 [0, 0, 0, 0]]

tf.SparseTensor.__init__(indices, values, shape)

解释:这个函数的作用是构建一个SparseTensor

输入参数:

  • indices: 一个二维的张量,数据类型是int64,数据维度是[N, ndims]
  • values: 一个一维的张量,数据类型是任意的,数据维度是[N]
  • dense_shape: 一个一维的张量,数据类型是int64,数据维度是[ndims]

输出参数:
* 一个稀疏张量SparseTensor


tf.SparseTensor.indices

解释:这个函数的作用是取出密集矩阵中非零值得索引。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices=[[4, 1], [1, 2]], values=[1, 2], shape=[3, 4])
b = a.indices
sess = tf.Session()
print sess.run(a)
print sess.run(b)
sess.close()

输出参数:
* 一个二维的张量,数据类型是int64,数据维度是[N, ndims]。其中,N表示在稀疏张量中非零值的个数,ndims表示稀疏张量的秩。


tf.SparseTensor.values

解释:这个函数的作用是取出密集矩阵中非零值。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices=[[4, 1], [1, 2]], values=[1, 2], shape=[3, 4])
b = a.values
sess = tf.Session()
print sess.run(a)
print sess.run(b)
sess.close()

输出参数:
* 一个一维的张量,数据类型是任意的。


tf.SparseTensor.dtype

解释:这个函数的作用是返回张量中元素的类型。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices=[[4, 1], [1, 2]], values=tf.constant([1, 2]), shape=[3, 4])
b = a.dtype
sess = tf.Session()
print b
sess.close()

输出参数:

  • 返回张量中元素的类型。

tf.SparseTensor.shape

解释:这个函数的作用是返回稀疏张量的维度。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices=[[4, 1], [1, 2]], values=tf.constant([1, 2]), shape=[3, 4])
b = a.shape
sess = tf.Session()
print sess.run(b)
sess.close()

输出参数:

  • 返回稀疏张量的维度。

tf.SparseTensor.graph

解释:这个函数的作用是返回包含该稀疏张量的图。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices=[[4, 1], [1, 2]], values=tf.constant([1, 2]), shape=[3, 4])
b = a.graph
sess = tf.Session()
print b
sess.close()

输出参数:

  • 返回包含该稀疏张量的图。

class tf.SparseTensorValue

解释:这个函数的作用是查看设置稀疏张量的值。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensorValue(indices=[[4, 1], [1, 2]], values=tf.constant([1, 2]), shape=[3, 4])
sess = tf.Session()
print a
print a[0]
print a[1]
print a[2]
sess.close()

tf.SparseTensorValue.indices

解释:这个函数的作用是返回稀疏张量中值的存在位置。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensorValue(indices=[[4, 1], [1, 2]], values=tf.constant([1, 2]), shape=[3, 4])
sess = tf.Session()
print a.indices
sess.close()

输出参数:

  • 返回稀疏张量中值的存在位置。

tf.SparseTensorValue.shape

解释:这个函数的作用是返回稀疏张量的维度。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensorValue(values=tf.constant([1, 2]), indices=[[4, 1], [1, 2]], shape=[3, 4])
sess = tf.Session()
print a.shape
sess.close()

输出参数:

  • 返回稀疏张量的维度。

tf.SparseTensorValue.shape

解释:这个函数的作用是返回稀疏张量中的元素。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensorValue(values=tf.constant([1, 2]), indices=[[4, 1], [1, 2]], shape=[3, 4])
sess = tf.Session()
print sess.run(a.values)  # 这是一个张量,所以用sess.run()
sess.close()

输出参数:

  • 返回稀疏张量中的元素。

稀疏张量与密集张量的转换

TensorFlow提供了稀疏张量与密集张量之间的转换操作。


tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)

解释:这个函数的作用是将一个稀疏表示转换成一个密集张量。具体将稀疏张量sparse转换成密集张量dense如下:

# If sparse_indices is scalar
dense[i] = (i == sparse_indices ? sparse_values : default_value)

# If sparse_indices is a vector, then for each i
dense[sparse_indices[i]] = sparse_values[i]

# If sparse_indices is an n by d matrix, then for each i in [0, n)
dense[sparse_indices[i][0], ..., sparse_indices[i][d-1]] = sparse_values[i]

默认情况下,dense中的填充值default_value都是0,除非该值被设置成一个标量。

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.sparse_to_dense(sparse_indices = [[1,2],[2,1]], output_shape = [3,3], 
    sparse_values = [2,3], default_value = 1)
sess = tf.Session()
print sess.run(a) 
sess.close()

输入参数:

  • sparse_indices: 一个Tensor,数据类型必须是int32或者int64。数据维度0维,一维或者二维都可以,或者更加高纬度的sparse_indices[i]
  • output_shape: 一个Tensor,数据类型必须和sparse_indices相同。数据维度是一维,表示输出密集张量的维度。
  • sparse_values: 一个Tensor,数据维度是一维,其中的每一个元素对应sparse_indices中坐标的值。
  • default_value: 一个Tensor,数据类型必须和sparse_values相同,数据维度是一个标量。设置稀疏索引不指定的值。
  • name: (可选)为这个操作取一个名字。

输出参数:

  • 一个Tensor,数据类型和sparse_values相同。密集张量的数据维度是output_shape

tf.sparse_tensor_to_dense(sp_input, default_value, name=None)

解释:这个函数的作用是将一个稀疏张量SparseTensor转换成一个密集张量。

这个操作是一个便利的将稀疏张量转换成密集张量的方法。

比如,sp_input的数据维度是[3, 5],非空值为:

[0, 1]: a
[0, 3]: b
[2, 0]: c

default_value值为x,那么输出的密集张量的维度是[3, 5],具体的展示形式如下:

[[x a x b x]
 [x x x x x]
 [c x x x x]]

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[0, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[3, 5])
b = tf.sparse_tensor_to_dense(a, default_value = 11)
sess = tf.Session()
print sess.run(b)
sess.close()

输入参数:

  • sp_input: 一个SparseTensor
  • default_value: 数据维度是一个标量,设置稀疏索引不指定的值。
  • name: (可选)设置返回张量名称的前缀。

输出参数:

  • 一个密集张量,数据维度是sp_input.shape,密集张量里面的值为sp_input中指定的值,没有索引的值为default_value值。

异常:

  • 类型错误: 如果sp_input不是一个SparseTensor,将报错。

tf.sparse_to_indicator(sp_input, vocab_size, name=None)

解释:这个函数的作用是将稀疏张量SparseTensor的坐标转换成密集张量中的布尔坐标。

sp_input中的最后一维被丢弃,并且用sp_input在该位的值来代替,如果sp_input.shape = [D0, D1, D2, ..., Dn, K],其中K是最后一维,那么output.shape = [D0, D1, D2, ..., Dn, vocab_size],其中:

output[d_0, d_1, ..., d_n, sp_input[d_0, d_1, ..., d_n, k]] = True

output中其余值为False

比如,sp_input.shape = [2, 3, 4],非空值如下:

[0, 0, 0]: 0
[0, 1, 0]: 10
[1, 0, 3]: 103
[1, 1, 2]: 112
[1, 1, 3]: 113
[1, 2, 1]: 121

并且vocab_size = 200,那么输出output.shape = [2, 3, 200],并且output中的值都是False,除了以下位置:

(0, 0, 0), (0, 1, 10), (1, 0, 103), (1, 1, 112), (1, 1, 113), (1, 2, 121).

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[0, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[3, 5])
b = tf.sparse_to_indicator(a, 10)
sess = tf.Session()
print sess.run(b)
sess.close()

输入参数:

  • sp_input: 一个SparseTensor,数据类型是int32或者int64
  • vocab_size: sp_Input最后一维的新的维度,并且0 <= sp_input.shape > vocab_size
  • name: (可选)设置返回张量名称的前缀。

输出参数:

  • 一个经过修改的密集布尔张量。

异常:

  • 类型错误: 如果sp_input不是一个SparseTensor,将报错。

稀疏张量的操作

TensorFlow提供了一些对于稀疏张量的操作函数。


tf.sparse_concat(concat_dim, sp_inputs, name=None)

解释:这个函数的作用是将一系列的SparseTensor,按照指定的维度进行合并。

具体合并思路是,先将稀疏张量看成是一个密集张量,然后按照指定的维度进行张量合并,最后将合并成的密集张量看成是一个稀疏张量。

输入的数据中,SparseTensor的数据维度必须是相同的,并且indicesvaluesshapes的长度必须相同。

输出数据的维度将由输入数据的维度决定,除了需要合并的那一维度,这一维度是所有数据该维度的相加总和。

输出张量中的元素将会被重新保存在稀疏张量中,并且按照原来的顺序进行排序。

这个操作的时间复杂度是O(M log M),其中,M是输入数据中所有非空元素的个数总和。

比如,当concat_dim = 1时:

sp_inputs[0]: shape = [2, 3]
[0, 2]: "a"
[1, 0]: "b"
[1, 1]: "c"

sp_inputs[1]: shape = [2, 4]
[0, 1]: "d"
[0, 2]: "e"

那么输出数据为:

shape = [2, 7]
[0, 2]: "a"
[0, 4]: "d"
[0, 5]: "e"
[1, 0]: "b"
[1, 1]: "c"

用图形表示,如下:

[    a] concat [  d e  ] = [    a   d e  ]
[b c  ]           [         ]     [b c          ]

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[0, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[3, 5])
aa = tf.SparseTensor(indices = [[1, 1], [1, 3], [2, 1]], values=[11,12,13], shape=[3, 5])
b = tf.sparse_concat(0, [a, aa])
sess = tf.Session()
print sess.run(b)
print sess.run(tf.sparse_tensor_to_dense(b))
sess.close()

输入参数:

  • concat_dim: 需要合并的维度。
  • sp_inputs: 一个需要合并的SparseTensor列表。
  • name: (可选)设置返回张量名称的前缀。

输出参数:

  • 一个经过合并的SparseTensor

异常:

  • 类型错误: 如果sp_inputs不是一个SparseTensor列表。

tf.sparse_reorder(sp_input, name=None)

解释:这个函数的作用是将SparseTensor中的元素进行重新排列,按照索引从小到大进行排序。

重排列不会影响SparseTensor的维度。

比如,如果sp_input的维度是[4, 5]indices / values如下:

[0, 3]: b
[0, 1]: a
[3, 1]: d
[2, 0]: c

那么输出的SparseTensor的维度还是[4, 5]indices / values如下:

[0, 1]: a
[0, 3]: b
[2, 0]: c
[3, 1]: d

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[2, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[3, 5])
b = tf.sparse_reorder(a)
sess = tf.Session()
print sess.run(b)
sess.close()

输入参数:

  • sp_input: 一个SparseTensor
  • name: (可选)设置返回张量名称的前缀。

输出参数:

  • 一个SparseTensor,数据维度和数据类型都不变,只有其中的值进行了有序的排序。

异常:

  • 类型错误: 如果sp_input不是一个SparseTensor

tf.sparse_retain(sp_input, to_retain, name=None)

解释:这个函数的作用是保留SparseTensor中指定的非空元素。

比如,如果sp_input的数据维度是[4, 5],并且拥有4个非空值如下:

[0, 1]: a
[0, 3]: b
[2, 0]: c
[3, 1]: d

而且to_retain = [True, False, False, True],那么最后输出数据SparseTensor的数据维度是[4, 5],并且保留两个非空值如下:

[0, 1]: a
[3, 1]: d

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[2, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[3, 5])
b = tf.sparse_retain(a, [False, False, True])
sess = tf.Session()
print sess.run(b)
sess.close()

输入参数:

  • sp_input: 一个SparseTensor,包含N个非空元素。
  • to_retain: 一个布尔类型的向量,向量长度是N,并且其中包含MTrue值。

输出参数:

  • 一个SparseTensor,数据维度和输入数据相同,其中包含M个非空值,该值的位置根据True的位置来决定。

异常:

  • 类型错误: 如果sp_input不是一个SparseTensor

tf.sparse_fill_empty_rows(sp_input, default_value, name=None)

解释:这个函数的作用是将二维的SparseTensor中,将空的行中填充指定元素的值。

如果一行中不存在元素,那么就将改行的坐标[row, 0]填上default_value

比如,我们假设sp_input的数据维度是[5, 6],并且非空值如下:

[0, 1]: a
[0, 3]: b
[2, 0]: c
[3, 1]: d

因为在稀疏张量中,第一行和第四行中不存在值,那么我们需要在[1, 0][4, 0]坐标填上default_value,如下:

[0, 1]: a
[0, 3]: b
[1, 0]: default_value
[2, 0]: c
[3, 1]: d
[4, 0]: default_value

请注意,输入可能有空列在最后,但对这个操作没有任何影响。

输出的SparseTensor将是一个按照从小到大的顺序进行排序,并且输出数据和输入数据拥有相同的数据维度。

这个操作还会返回一个布尔向量,其中的布尔值,如果是True值,那么表示该行添加了一个default_value,计算公式如下:

empty_row_indicator[i] = True iff row i was an empty row.

使用例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np

a = tf.SparseTensor(indices = [[2, 1], [0, 3], [2, 0]], values=[1,2,3], shape=[6, 5])
b, bb = tf.sparse_fill_empty_rows(a, 10)
sess = tf.Session()
print sess.run(b)
print '----'
print sess.run(bb)
sess.close()

输入参数:

  • sp_input: 一个SparseTensor,数据维度是[N, M]
  • default_value: 需要向空行填充的值,数据类型和sp_input相同。
  • name: (可选)设置返回张量名称的前缀。

输出参数:

  • sp_ordered_output: 一个SparseTensor,数据维度是[N, M],并且其中所有空行填充了default_value
  • empty_row_indicator: 一个布尔类型的向量,数据长度是N,如果该行填充了default_value,那么该位置的布尔值为True

异常:

  • 类型错误: 如果sp_input不是一个SparseTensor

作者:chen_h
微信号 & QQ:862251340
简书地址:https://www.jianshu.com/p/c233e09d2f5f

CoderPai 是一个专注于算法实战的平台,从基础的算法到人工智能算法都有设计。如果你对算法实战感兴趣,请快快关注我们吧。加入AI实战微信群,AI实战QQ群,ACM算法微信群,ACM算法QQ群。长按或者扫描如下二维码,关注 “CoderPai” 微信号(coderpai)
这里写图片描述

2025-09-10 00:47:05.762545: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2025-09-10 00:47:05.762651: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. A module that was compiled using NumPy 1.x cannot be run in NumPy 2.2.6 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "D:\pythonProject4\main.py", line 1, in <module> import tensorflow as tf File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\__init__.py", line 37, in <module> from tensorflow.python.eager import context File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\eager\context.py", line 35, in <module> from tensorflow.python.client import pywrap_tf_session File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\client\pywrap_tf_session.py", line 19, in <module> from tensorflow.python.client._pywrap_tf_session import * AttributeError: _ARRAY_API not found A module that was compiled using NumPy 1.x cannot be run in NumPy 2.2.6 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "D:\pythonProject4\main.py", line 1, in <module> import tensorflow as tf File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\__init__.py", line 42, in <module> from tensorflow.python import data File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\__init__.py", line 21, in <module> from tensorflow.python.data import experimental File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 96, in <module> from tensorflow.python.data.experimental import service File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 419, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 24, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\util\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\util\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\eager\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\dtypes.py", line 29, in <module> from tensorflow.python.lib.core import _pywrap_bfloat16 AttributeError: _ARRAY_API not found ImportError: numpy.core._multiarray_umath failed to import ImportError: numpy.core.umath failed to import Traceback (most recent call last): File "D:\pythonProject4\main.py", line 1, in <module> import tensorflow as tf File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\__init__.py", line 42, in <module> from tensorflow.python import data File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\__init__.py", line 21, in <module> from tensorflow.python.data import experimental File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 96, in <module> from tensorflow.python.data.experimental import service File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 419, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 24, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\util\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\data\util\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\eager\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "D:\miniconda\envs\pytorch_env\lib\site-packages\tensorflow\python\framework\dtypes.py", line 34, in <module> _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type() TypeError: Unable to convert function return value to a Python type! The signature was () -> handle
最新发布
09-11
W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2025-08-16 17:23:58.233691: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\framework\dtypes.py:585: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar. np.object, Traceback (most recent call last): File "e:\神经网络\test1.py", line 3, in <module> from tensorflow import keras File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\__init__.py", line 41, in <module> from tensorflow.python.tools import module_util as _module_util File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module> from tensorflow.python import data File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module> from tensorflow.python.data import experimental File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 97, in <module> from tensorflow.python.data.experimental import service File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 353, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 26, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module> from tensorflow.python.data.util import structure File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module> from tensorflow.python.data.util import nest File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in <module> from tensorflow.python.framework import constant_op File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module> from tensorflow.python.eager import execute File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in <module> from tensorflow.python.framework import dtypes File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\tensorflow\python\framework\dtypes.py", line 585, in <module> np.object, File "C:\Users\Administrator\.conda\envs\env1\lib\site-packages\numpy\__init__.py", line 324, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'object'. `np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值