tf.int32 在构造tensorflow数据集时被解释为tf.string
tf.int32 Being interpreted as tf.string when constructing a tensorflow dataset
我有一个 Pandas 数据框,我打算将其数据导出到 Tensorflow 数据集。该数据框有 4 列,其中 2 列是字符串列表,其余两列是整数列表。目前最重要的列是input_ids
和attention_mask
,它们构成了模型的输入数据。
train_input_ids = train_df["input_ids"].values.tolist()
train_attention_mask = train_df["attention_mask"].values.tolist()
如 head()
方法所示,这些列存储整数列表。 print(train_df["input_ids"].head(3))
returns 以下内容:
0 [101, 24918, 7821, 5983, 46106, 21905, 10789...
1 [101, 33198, 10173, 14657, 25287, 55610, 10789...
2 [101, 10109, 19217, 34768, 16294, 17953, 51733...
Name: input_ids, dtype: object
其他列之一 codes
存储包含要进行单热编码的代码的字符串列表:
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(train_df["codes"].values)
我创建的数据集如下:
train_dataset = (
tf.data.Dataset
.from_tensor_slices((( tf.convert_to_tensor(train_input_ids), tf.convert_to_tensor(train_attention_mask)), y_train))
.repeat()
.shuffle(2048)
.batch(BATCH_SIZE)
.prefetch(BATCH_SIZE * 2)
)
然而,在尝试从张量切片构建数据集并打印生成的数据集后,我发现 train_input_ids
和 train_attention_mask
都被解释为 tf.string
:
<PrefetchDataset shapes: (((None,), (None,)), (None,)), types: ((tf.string, tf.string), tf.int64)>
据我所知,它们应该被推断为 types: ((tf.int32, tf.int32)
因为数据框包含整数列表而不是字符串列表(这也是模型在输入层定义中接受输入的方式).我错过了什么?
我最终发现,在一天结束时,我遇到了一个与 this question 中描述的问题非常相似的问题。因此,由于此数据框存储列表的所有列,我通过定义一个函数来调整建议的解决方案之一,该函数将在从其相应的 .tsv 文件中检索数据框后调用:
from ast import literal_eval
def _remove_extra_quotes(df):
def apply_lambda(column):
df[column] = df[column].apply(lambda x: literal_eval(str(x)))
return
for column_name in df:
apply_lambda(column_name)
return df
我有一个 Pandas 数据框,我打算将其数据导出到 Tensorflow 数据集。该数据框有 4 列,其中 2 列是字符串列表,其余两列是整数列表。目前最重要的列是input_ids
和attention_mask
,它们构成了模型的输入数据。
train_input_ids = train_df["input_ids"].values.tolist()
train_attention_mask = train_df["attention_mask"].values.tolist()
如 head()
方法所示,这些列存储整数列表。 print(train_df["input_ids"].head(3))
returns 以下内容:
0 [101, 24918, 7821, 5983, 46106, 21905, 10789...
1 [101, 33198, 10173, 14657, 25287, 55610, 10789...
2 [101, 10109, 19217, 34768, 16294, 17953, 51733...
Name: input_ids, dtype: object
其他列之一 codes
存储包含要进行单热编码的代码的字符串列表:
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(train_df["codes"].values)
我创建的数据集如下:
train_dataset = (
tf.data.Dataset
.from_tensor_slices((( tf.convert_to_tensor(train_input_ids), tf.convert_to_tensor(train_attention_mask)), y_train))
.repeat()
.shuffle(2048)
.batch(BATCH_SIZE)
.prefetch(BATCH_SIZE * 2)
)
然而,在尝试从张量切片构建数据集并打印生成的数据集后,我发现 train_input_ids
和 train_attention_mask
都被解释为 tf.string
:
<PrefetchDataset shapes: (((None,), (None,)), (None,)), types: ((tf.string, tf.string), tf.int64)>
据我所知,它们应该被推断为 types: ((tf.int32, tf.int32)
因为数据框包含整数列表而不是字符串列表(这也是模型在输入层定义中接受输入的方式).我错过了什么?
我最终发现,在一天结束时,我遇到了一个与 this question 中描述的问题非常相似的问题。因此,由于此数据框存储列表的所有列,我通过定义一个函数来调整建议的解决方案之一,该函数将在从其相应的 .tsv 文件中检索数据框后调用:
from ast import literal_eval
def _remove_extra_quotes(df):
def apply_lambda(column):
df[column] = df[column].apply(lambda x: literal_eval(str(x)))
return
for column_name in df:
apply_lambda(column_name)
return df