Dataframe 添加“的最佳方式”;“列”;到tf.data.dataset
在运行时和最佳实践方面,向TensorFlow数据集中添加列的最佳方式是什么 假设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
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