如何在 TensorFlow 中将向量和标签的 Pandas DataFrame 转换为 RNN 的输入
How can I convert a Pandas DataFrame of vectors and labels into input for an RNN in TensorFlow
我正在使用 TensorFlow 中的 LSTM 开发文本分类器,但无法确定输入数据的格式。
我的输入数据是一个 Pandas 数据框,其中包含一个特征列和一个标签列。
我的特征列是一个表示向量数组的二维数组,我的标签列是一个字符串,下面是我的数据输入示例。
如何将此 Dataframe 转换为可用作 Tensorflow.Keras 模型输入的数据集?
我尝试使用 tf.data.Dataset.from_tensor_slices 将 Dataframe 转换为 TensorFlow.Dataset 数据集,但这会产生 TypeError
##Building input data
test01 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],'label1')
test02 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,0,0]],'label2')
test03 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,0,0],[1,1,1,1]],'label3')
test04 = ([[1,1,1,1],[0,0,0,0],[1,1,1,1],[1,1,1,1],[1,1,1,1]],'label1')
test_data = [test01,test02,test03,test04]
##DataFrame from data
columns = ['feature','label']
t_df = pd.DataFrame(data = test_data, columns = columns)
##Convert to TensorFlow Dataset
dataset = tf.data.Dataset.from_tensor_slices((t_df['feature'], t_df['label']))
这会产生以下错误:
TypeError: Expected binary or unicode string, got [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
##TensorFlow Model Example
model = tf.keras.Sequential([,
tf.keras.layers.LSTM(input_shape),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
在这种情况下,您传递的尺寸略有错误。 from_tensor_slices
需要对象列表,而不是嵌套列表。
dataset = tf.data.Dataset.from_tensor_slices(([i for i in t_df['feature']], t_df['label']))
我正在使用 TensorFlow 中的 LSTM 开发文本分类器,但无法确定输入数据的格式。 我的输入数据是一个 Pandas 数据框,其中包含一个特征列和一个标签列。
我的特征列是一个表示向量数组的二维数组,我的标签列是一个字符串,下面是我的数据输入示例。
如何将此 Dataframe 转换为可用作 Tensorflow.Keras 模型输入的数据集?
我尝试使用 tf.data.Dataset.from_tensor_slices 将 Dataframe 转换为 TensorFlow.Dataset 数据集,但这会产生 TypeError
##Building input data
test01 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],'label1')
test02 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,0,0]],'label2')
test03 = ([[1,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,0,0],[1,1,1,1]],'label3')
test04 = ([[1,1,1,1],[0,0,0,0],[1,1,1,1],[1,1,1,1],[1,1,1,1]],'label1')
test_data = [test01,test02,test03,test04]
##DataFrame from data
columns = ['feature','label']
t_df = pd.DataFrame(data = test_data, columns = columns)
##Convert to TensorFlow Dataset
dataset = tf.data.Dataset.from_tensor_slices((t_df['feature'], t_df['label']))
这会产生以下错误:
TypeError: Expected binary or unicode string, got [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
##TensorFlow Model Example
model = tf.keras.Sequential([,
tf.keras.layers.LSTM(input_shape),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
在这种情况下,您传递的尺寸略有错误。 from_tensor_slices
需要对象列表,而不是嵌套列表。
dataset = tf.data.Dataset.from_tensor_slices(([i for i in t_df['feature']], t_df['label']))