我正在尝试使用 Pandas 数据框提供 tensorflow 多头模型。
输入函数如下:
feature_column_names = [...]
input_fn_test_action = tf.compat.v1.estimator.inputs.pandas_input_fn(
x=test_preflop_dataframe[feature_column_names],
y=test_preflop_dataframe[['target1','target2']],
batch_size=BATCH,
num_epochs=EPOCHS,
shuffle=True,
queue_capacity=1000,
target_column='target'
)
似乎有效。
然后我构建多头模型如下:
feature_columns = [ ... ]
head1 = tf.estimator.MultiLabelHead(n_classes = 4,
weight_column=weights,
label_vocabulary=Action_vocab,
loss_reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE,
loss_fn=None,
name='head1'
)
head2 = tf.estimator.RegressionHead(label_dimension=1,
weight_column=weights,
loss_reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE,
loss_fn=None,
inverse_link_fn=None,
name='head2'
)
multi_head = tf.estimator.MultiHead([head1, head2])
model1 = tf.estimator.DNNEstimator(head = multi_head,
hidden_units = [14,9,6],
feature_columns = feature_columns,
model_dir='./Model3',
optimizer=tf.keras.optimizers.Adam(learning_rate=LRATE, beta_1= 0.9,beta_2=0.999, epsilon = 1e-08),
activation_fn=tf.nn.softmax,
dropout=DROPOUT,
config=tf.estimator.RunConfig().replace(save_summary_steps=10,save_checkpoints_secs=60),
warm_start_from=None,
batch_norm=True
)
似乎也有效...
但是当我尝试用这个进行训练时:
hookModel1 = tf.estimator.experimental.stop_if_no_decrease_hook(model1, metric_name='average_loss', max_steps_without_decrease=5000, min_steps=5000,run_every_secs=120, run_every_steps=None,)
train_spec1 = tf.estimator.TrainSpec(input_fn=input_fn_train_action,hooks=[hookModel1])
eval_spec1 = tf.estimator.EvalSpec(input_fn=input_fn_test_action,start_delay_secs = 60,throttle_secs = 60)
tf.estimator.train_and_evaluate(model1, train_spec1, eval_spec1)
我遇到了这个错误:
回溯(最近一次调用最后一次):(...) raise ValueError('labels has missing values for head(s): {}'.format( 值错误:标签缺少头部值:['head1', 'head2']
我猜该模型无法将每个头部与相应的标签相关联,因为 'tf.compat.v1.estimator.inputs.pandas_input_fn' 返回一个特征字典,但返回一个标签数组(在本例中为两个),但没有与之关联的名称head 和 multihead 必须使用带有 head 规范 {'head1' : [label_array1], 'head2' : [label_array2]}
的标签字典。
有人对此有解决方案吗?
答案 0 :(得分:0)
其实这里描述的问题在TF 2.3中已经解决了:github.com/tensorflow/tensorflow/issues/19182
现在,如果提供了熊猫系列列表,tf.compat.v1.estimator.inputs.pandas_input_fn 将返回特征字典和标签字典。标签标志是 pd 的标题。
那么解决办法就是给每个head的名字和pd的serie中的label一样!!