我有一个多标记数据集,我想用一个宽正深层神经网络对样本进行分类。
这是一个非常小的例子只是为了测试:
import numpy as np
import pandas as pd
import tensorflow as tf
tf.enable_eager_execution()
training_df: pd.DataFrame = pd.DataFrame(
data={
'feature1': np.random.rand(10),
'feature2': np.random.rand(10),
'feature3': np.random.rand(10),
'feature4': np.random.randint(0, 3, 10),
'feature5': np.random.randint(0, 3, 10),
'feature6': np.random.randint(0, 3, 10),
'target1': np.random.randint(0, 2, 10),
'target2': np.random.randint(0, 2, 10),
'target3': np.random.randint(0, 2, 10)
}
)
features = ['feature1', 'feature2', 'feature3','feature4', 'feature5', 'feature6']
targets = ['target1', 'target2', 'target3']
Categorical_Cols = ['feature4', 'feature5', 'feature6']
Numerical_Cols = ['feature1', 'feature2', 'feature3']
wide_columns = [tf.feature_column.categorical_column_with_vocabulary_list(key=x, vocabulary_list=[0, 1, -1])
for x in Categorical_Cols]
deep_columns = [tf.feature_column.numeric_column(x) for x in Numerical_Cols]
def wrap_dataset(df, features, labels):
dataset = (
tf.data.Dataset.from_tensor_slices(
(
tf.cast(df[features].values, tf.float32),
tf.cast(df[labels].values, tf.int32),
)
)
)
return(dataset)
input_fn_train = wrap_dataset(training_df, features, targets)
m = tf.contrib.estimator.DNNLinearCombinedEstimator(
head=tf.contrib.estimator.multi_label_head(n_classes=2),
# wide settings
linear_feature_columns=wide_columns,
# linear_optimizer=tf.train.FtrlOptimizer(...),
# deep settings
dnn_feature_columns=deep_columns,
# dnn_optimizer=tf.train.ProximalAdagradOptimizer(...),
dnn_hidden_units=[10, 30, 10])
m.train(input_fn=input_fn_train)
在这个例子中,我们有6个功能,包括:
其中,每个样品具有三个标签,并且每个标签具有一个二进制值:0或1。
该错误是关于输入功能,我无法弄清楚如何以正确的方式定义的输入功能。任何帮助纠正代码表示赞赏。
UPDATE:错误是:
TypeError: <TensorSliceDataset shapes: ((6,), (3,)), types: (tf.float32, tf.int32)> is not a callable object
因为它说,它不是一个可调用对象,你可以简单地添加拉姆达,它应该工作
input_fn_train = lambda: wrap_dataset(training_df, features, targets)
此外,我认为你需要理清你如何通过你的数据的估计。因为你正在使用的功能列这可能需要字典。现在你逝去的张量的张量,而不是字典。看看这个useful post。
最后,我想出如何使代码工作。我张贴在这里,以帮助人们谁愿意使用内置的功能DNNLinearCombinedEstimator
从tensorflow包,版本1.13做多标签分类。
import numpy as np
import pandas as pd
import tensorflow as tf
# from tensorflow import contrib
tf.enable_eager_execution()
training_df: pd.DataFrame = pd.DataFrame(
data={
'feature1': np.random.rand(10),
'feature2': np.random.rand(10),
'feature3': np.random.rand(10),
'feature4': np.random.randint(0, 3, 10),
'feature5': np.random.randint(0, 3, 10),
'feature6': np.random.randint(0, 3, 10),
'target1': np.random.randint(0, 2, 10),
'target2': np.random.randint(0, 2, 10),
'target3': np.random.randint(0, 2, 10)
}
)
features = ['feature1', 'feature2', 'feature3','feature4', 'feature5', 'feature6']
targets = ['target1', 'target2', 'target3']
Categorical_Cols = ['feature4', 'feature5', 'feature6']
Numerical_Cols = ['feature1', 'feature2', 'feature3']
wide_columns = [tf.feature_column.categorical_column_with_vocabulary_list(key=x, vocabulary_list=[0, 1, -1])
for x in Categorical_Cols]
deep_columns = [tf.feature_column.numeric_column(x) for x in Numerical_Cols]
def input_fn(df):
# Creates a dictionary mapping from each continuous feature column name (k) to
# the values of that column stored in a constant Tensor.
continuous_cols = {k: tf.constant(df[k].values)
for k in Numerical_Cols}
# Creates a dictionary mapping from each categorical feature column name (k)
# to the values of that column stored in a tf.SparseTensor.
categorical_cols = {k: tf.SparseTensor(
indices=[[i, 0] for i in range(df[k].size)],
values=df[k].values,
dense_shape=[df[k].size, 1])
for k in Categorical_Cols}
# Merges the two dictionaries into one.
feature_cols = continuous_cols.copy()
feature_cols.update(categorical_cols)
labels =tf.convert_to_tensor(training_df.as_matrix(training_df[targets].columns.tolist()), dtype=tf.int32)
return feature_cols, labels
def train_input_fn():
return input_fn(training_df)
def eval_input_fn():
return input_fn(training_df)
m = tf.contrib.learn.DNNLinearCombinedEstimator(
head=tf.contrib.learn.multi_label_head(n_classes=3),
# wide settings
linear_feature_columns=wide_columns,
# linear_optimizer=tf.train.FtrlOptimizer(...),
# deep settings
dnn_feature_columns=deep_columns,
# dnn_optimizer=tf.train.ProximalAdagradOptimizer(...),
dnn_hidden_units=[10, 10])
m.train(input_fn=train_input_fn, steps=20)
results = m.evaluate(input_fn=eval_input_fn, steps=1)
print("#########################################################")
for key in sorted(results):
print("%s: %s" % (key, results[key]))