栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从tf.tensor中获取字符串值,其中dtype是字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从tf.tensor中获取字符串值,其中dtype是字符串

您可以使用

tf.py_func
包装
load_audio_file()

import tensorflow as tftf.enable_eager_execution()def load_audio_file(file_path):    # you should depre bytes type to string type    print("file_path: ",bytes.depre(file_path),type(bytes.depre(file_path)))    return file_pathtrain_dataset = tf.data.Dataset.list_files('clean_4s_val/*.wav')train_dataset = train_dataset.map(lambda x: tf.py_func(load_audio_file, [x], [tf.string]))for one_element in train_dataset:    print(one_element)file_path:  clean_4s_val/1.wav <class 'str'>(<tf.Tensor: id=32, shape=(), dtype=string, numpy=b'clean_4s_val/1.wav'>,)file_path:  clean_4s_val/3.wav <class 'str'>(<tf.Tensor: id=34, shape=(), dtype=string, numpy=b'clean_4s_val/3.wav'>,)file_path:  clean_4s_val/2.wav <class 'str'>(<tf.Tensor: id=36, shape=(), dtype=string, numpy=b'clean_4s_val/2.wav'>,)

*TF 2 *更新

即使将替换

tf.py_func
tf.py_function
,上述解决方案也不适用于TF 2(已在2.2.0中测试)。

InvalidArgumentError: TypeError: descriptor 'depre' requires a 'bytes' object but received a 'tensorflow.python.framework.ops.EagerTensor'

要使其在TF 2中工作,请进行以下更改:

  • 删除
    tf.enable_eager_execution()
    (默认情况下,TF 2中启用了渴望,您可以通过
    tf.executing_eagerly()
    返回进行验证
    True
  • 替换
    tf.py_func
    tf.py_function
  • 替换的所有功能的参考
    file_path
    file_path.numpy()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号