Tensorflow 2.3: AttributeError: 'Tensor' 对象没有属性 'numpy'

编程入门 行业动态 更新时间:2024-10-09 03:23:41
本文介绍了Tensorflow 2.3: AttributeError: 'Tensor' 对象没有属性 'numpy'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I wanted to load the text file borrowed from here, where each line represent a json string like following:

{"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 特征,但面临以下错误.

I would like to extract only reviewText and overall feature from the dataset using tensorflow but facing following error.

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'

我的代码片段如下所示:

My code snippet looks like following:

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.

I am using tensorflow 2.3.0.

推荐答案

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

The input pipeline of a dataset is always traced into a graph (as if you used @tf.function) to make it faster, which means, among other things, that you cannot use .numpy(). You can however use tf.numpy_function to access the data as a NumPy array within the graph:

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)

这篇关于Tensorflow 2.3: AttributeError: 'Tensor' 对象没有属性 'numpy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 02:01:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   AttributeError   Tensorflow   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!