Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow使用函数拆分tf数据集中的字符串张量会产生错误_Python_Tensorflow2.x_Tf.data.dataset - Fatal编程技术网

Python Tensorflow使用函数拆分tf数据集中的字符串张量会产生错误

Python Tensorflow使用函数拆分tf数据集中的字符串张量会产生错误,python,tensorflow2.x,tf.data.dataset,Python,Tensorflow2.x,Tf.data.dataset,我是tensorflow新手,正在学习使用tensorflow数据集。尝试将tf.TextLineDataset的每个字符串张量拆分为两个张量时,如下所示 下面是一个最小的可复制示例 预期的 将tensorflow导入为tf dataset=tf.data.TextLineDataset() 对于数据集中的x,取(2): 打印(x) #Tensor(b'sample text\t示例标签',shape=(),dtype=string) 对于数据集中的x,取(2): a、 b=tf.strings

我是tensorflow新手,正在学习使用tensorflow数据集。尝试将
tf.TextLineDataset
的每个字符串张量拆分为两个张量时,如下所示

下面是一个最小的可复制示例

预期的

将tensorflow导入为tf
dataset=tf.data.TextLineDataset()
对于数据集中的x,取(2):
打印(x)
#Tensor(b'sample text\t示例标签',shape=(),dtype=string)
对于数据集中的x,取(2):
a、 b=tf.strings.split(x,sep='\t')
印刷品(a)
印刷品(b)
#张量(b'sample text',shape=(),dtype=string)
#tf.Tensor(b'sample label',shape=(),dtype=string)
使用函数

def标签机(示例):
'''
将每行拆分为文本和标签序列
'''
text_seq,label_seq=tf.strings.split(例如,sep='\t')
返回文本顺序,标签顺序
标记的_dataset=dataset.map(lambda x:labeler(x))
上面的代码返回以下错误

OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
任何人都可以帮助解决上述错误

更新

将函数定义更改为以下有助于解决此错误

def labeler(example):
    '''
        Splits each line into text and label sequence
    '''
    return tf.strings.split(example, sep='\t')
从直觉上看,这两种方法似乎都是一样的。我现在对tensorflow如何以不同的方式处理这两种方法感到困惑

任何见解都会非常有用