【问题标题】:Tensorflow 2.3: AttributeError: 'Tensor' object has no attribute 'numpy'Tensorflow 2.3:AttributeError:“张量”对象没有属性“numpy”
【发布时间】:2020-08-24 09:09:42
【问题描述】:

我想加载从here借来的文本文件,其中每一行代表一个json字符串,如下所示:

{"overall": 2.0, "verified": true, "reviewTime": "02 4, 2014", "reviewerID": "A1M117A53LEI8", "asin": "7508492919", "reviewerName": "Sharon Williams", "reviewText": "DON'T CARE FOR IT.  GAVE IT AS A GIFT AND THEY WERE OKAY WITH IT.  JUST NOT WHAT I EXPECTED.", "summary": "CASE", "unixReviewTime": 1391472000}

我想使用 tensorflow 从数据集中仅提取 reviewTextoverall 特征,但遇到以下错误。

AttributeError: in user code:

    <ipython-input-4-419019a35c5e>:9 None  *
        line_dataset = line_dataset.map(lambda row: transform(row))
    <ipython-input-4-419019a35c5e>:2 transform  *
        str_example = example.numpy().decode("utf-8")

    AttributeError: 'Tensor' object has no attribute 'numpy'

我的代码 sn-p 如下所示:

def transform(example):
    str_example = example.numpy().decode("utf-8")
    json_example = json.loads(str_example)
    overall = json_example.get('overall', None)
    text = json_example.get('reviewText', None)
    return (overall, text)

line_dataset = tf.data.TextLineDataset(filenames = [file_path])
line_dataset = line_dataset.map(lambda row: transform(row))
for example in line_dataset.take(5):
    print(example)

我使用的是 tensorflow 2.3.0。

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    数据集的输入管道总是被追踪到一个图表中(就像你使用了@tf.function)以使其更快,这意味着你不能使用.numpy()。但是,您可以使用 tf.numpy_function 在图中以 NumPy 数组的形式访问数据:

    def transform(example):
        # example will now by a NumPy array
        str_example = example.decode("utf-8")
        json_example = json.loads(str_example)
        overall = json_example.get('overall', None)
        text = json_example.get('reviewText', None)
        return (overall, text)
    
    line_dataset = tf.data.TextLineDataset(filenames = [file_path])
    line_dataset = line_dataset.map(
        lambda row: tf.numpy_function(transform, row, (tf.float32, tf.string)))
    for example in line_dataset.take(5):
        print(example)
    

    【讨论】:

      【解决方案2】:

      有点罗嗦,但试试这样:

      def transform(example):     
          str_example = example.numpy().decode("utf-8")     
          json_example = json.loads(str_example)     
          overall = json_example.get('overall', None)     
          text = json_example.get('reviewText', None)     
          return (overall, text)  
      
      line_dataset = tf.data.TextLineDataset(filenames = [file_path]) 
      line_dataset = line_dataset.map(
          lambda input:     
              tf.py_function(transform, [input], (tf.float32, tf.string))
      )  
      for example in line_dataset.take(5):     
          print(example)
      

      这个特殊的 sn-p 适用于任何 python 函数,不仅适用于 numpy 函数。所以,如果你需要printinput等函数,你可以使用它。你不必知道所有细节,但如果你有兴趣,请问我。 :)

      【讨论】:

      • tf_function中使用装饰器的目的是什么?
      • 我修改了你的 sn-p 以缩短它,我的 sn-p 版本也可以工作。 def transform(example): str_example = example.numpy().decode("utf-8") json_example = json.loads(str_example) overall = json_example.get('overall', None) text = json_example.get('reviewText', None) return (overall, text) line_dataset = tf.data.TextLineDataset(filenames = [file_path]) line_dataset = line_dataset.map(lambda input: tf.py_function(transform, [input], (tf.float32, tf.string))) for example in line_dataset.take(5): print(example)
      • 在您允许的情况下,我会用您的 sn-p 更新我的答案。你的代码看起来更好:)
      猜你喜欢
      • 2020-06-06
      • 2019-02-20
      • 2020-10-22
      • 2017-12-06
      • 2018-07-02
      相关资源
      最近更新 更多