Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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
Dataframe 添加“的最佳方式”;“列”;到tf.data.dataset_Dataframe_Tensorflow_Tensorflow Datasets - Fatal编程技术网

Dataframe 添加“的最佳方式”;“列”;到tf.data.dataset

Dataframe 添加“的最佳方式”;“列”;到tf.data.dataset,dataframe,tensorflow,tensorflow-datasets,Dataframe,Tensorflow,Tensorflow Datasets,在运行时和最佳实践方面,向TensorFlow数据集中添加列的最佳方式是什么 假设dataset是通过make\u CSV\u dataset从CSV生成的PrefetchDataset。我想基于数据集的预先存在的功能列a,添加一个功能列B 我试图修改教程中的代码(参见函数PackNumericFeatures())来创建类 class add_column(object): def __call__(self, features, labels): features

在运行时和最佳实践方面,向TensorFlow数据集中添加列的最佳方式是什么

假设
dataset
是通过
make\u CSV\u dataset
从CSV生成的
PrefetchDataset
。我想基于
数据集
的预先存在的功能列
a
,添加一个功能列
B

我试图修改教程中的代码(参见函数
PackNumericFeatures()
)来创建类

class add_column(object):

    def __call__(self, features, labels):

        features['B'] = tf.map_fn(
            lambda x: len(str(x+"postfix")), features['A'])

        return features, labels
然后使用

dataset = dataset.map(add_column())
假设预先存在的功能
A
是一个字符串,这将成功地向从
A
复制的每个字符串添加
“后缀”
。但是,如果我想用
A
中每个元素的长度填充
B
,那么将lambda函数替换为
lambda x:len(x)
会产生错误

TypeError: len is not well defined for symbolic Tensors. (TensorArrayV2Read/TensorListGetItem:0) Please call `x.shape` rather than `len(x)` for shape information.
OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed: AutoGraph is disabled in this function. Try decorating it directly with @tf.function.
类似地,一个基本函数(如
lambda x:a)在x
中给出错误

TypeError: len is not well defined for symbolic Tensors. (TensorArrayV2Read/TensorListGetItem:0) Please call `x.shape` rather than `len(x)` for shape information.
OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed: AutoGraph is disabled in this function. Try decorating it directly with @tf.function.
使用熊猫数据帧,添加从已有列处理的新列要容易得多。我做错了什么?将
map\u fn()
add\u column()
等类一起使用是否是最佳实践?

y\u dataset=x\u dataset.map(fn1)

您可以根据需要定义fn1

@tf.function
def fn1(x):
    ##use x to derive additional columns u want. Set the shape as well
    y = {}
    y.update(x)
    y['new1'] = new1
    y['new2'] = new2
    return y