MultiLabelClassifierTask在_build_net中插入gru层之后报错
Created by: onewaymyway
- 版本、环境信息 1)PaddleHub和PaddlePaddle版本:paddlehub-1.6.1 paddlepaddle-gpu-1.7.1.post97 2)系统环境:AIStudio,python版本2.7
代码:
class MultiLabelClassifyReaderSP(hub.reader.MultiLabelClassifyReader):
def _pad_batch_records(self, batch_records, phase=None):
batch_token_ids = [record.token_ids for record in batch_records]
batch_text_type_ids = [record.text_type_ids for record in batch_records]
batch_position_ids = [record.position_ids for record in batch_records]
# padding
padded_token_ids, input_mask, batch_seq_lens = pad_batch_data(
batch_token_ids,
pad_idx=self.pad_id,
max_seq_len=self.max_seq_len,
return_input_mask=True,
return_seq_lens=True)
padded_text_type_ids = pad_batch_data(
batch_text_type_ids,
max_seq_len=self.max_seq_len,
pad_idx=self.pad_id)
padded_position_ids = pad_batch_data(
batch_position_ids,
max_seq_len=self.max_seq_len,
pad_idx=self.pad_id)
if phase != "predict":
batch_labels_ids = [record.label_id for record in batch_records]
num_label = len(self.dataset.get_labels())
batch_labels = np.array(batch_labels_ids).astype("int64").reshape(
[-1, num_label])
return_list = [
padded_token_ids, padded_position_ids, padded_text_type_ids,
input_mask, batch_labels, batch_seq_lens
]
if self.use_task_id:
padded_task_ids = np.ones_like(
padded_token_ids, dtype="int64") * self.task_id
return_list = [
padded_token_ids, padded_position_ids, padded_text_type_ids,
input_mask, padded_task_ids, batch_labels, batch_seq_lens
]
else:
return_list = [
padded_token_ids, padded_position_ids, padded_text_type_ids,
input_mask, batch_seq_lens
]
if self.use_task_id:
padded_task_ids = np.ones_like(
padded_token_ids, dtype="int64") * self.task_id
return_list = [
padded_token_ids, padded_position_ids, padded_text_type_ids,
input_mask, padded_task_ids, batch_seq_lens
]
return return_list
class MultiLabelClassifierTaskSP(hub.MultiLabelClassifierTask):
def __init__(self,
feature,
num_classes,
feed_list,
data_reader,
startup_program=None,
config=None,
hidden_units=None,
metrics_choices="default"):
super(MultiLabelClassifierTaskSP, self).__init__(
data_reader=data_reader,
feature=feature,
num_classes=num_classes,
feed_list=feed_list,
startup_program=startup_program,
config=config,
hidden_units=hidden_units,
metrics_choices=metrics_choices)
@property
def feed_list(self):
print("myFeedlist")
feed_list = [varname for varname in self._base_feed_list]
if self.is_train_phase or self.is_test_phase:
feed_list += [self.labels[0].name, self.seq_len.name]
else:
feed_list += [self.seq_len.name]
return feed_list
def _add_loss(self):
label_split = fluid.layers.split(
self.labels[0], self.num_classes, dim=-1)
#print("ls",label_split)
total_loss = fluid.layers.fill_constant(
shape=[1], value=0.0, dtype='float64')
print("outputs",len(self.outputs),self.outputs[0])
#ttp=fluid.layers.Print(self.outputs, message="The content of input layer:")
for index, probs in enumerate(self.outputs):
ce_loss = fluid.layers.cross_entropy(
input=probs, label=label_split[index])
total_loss += fluid.layers.reduce_sum(ce_loss)
loss = fluid.layers.mean(x=total_loss)
return loss
def _add_metrics(self):
#labels = fluid.layers.sequence_unpad(self.labels[0],
#self.seq_len_used)
#print(self.labels[0])
label_split = fluid.layers.split(
self.labels[0], self.num_classes, dim=-1)
# metrics change to auc of every class
eval_list = []
for index, probs in enumerate(self.outputs):
current_auc, _, _ = fluid.layers.auc(
input=probs, label=label_split[index])
eval_list.append(current_auc)
return eval_list
def _build_net(self):
self.seq_len = fluid.layers.data(
name="seq_len", shape=[1], dtype='int64')
if version_compare(paddle.__version__, "1.6"):
self.seq_len_used = fluid.layers.squeeze(self.seq_len, axes=[1])
else:
self.seq_len_used = self.seq_len
#seq_len = fluid.layers.assign(self.seq_len)
#tsqprint=fluid.layers.Print(self.seq_len, message="The content of input layer:")
unpad_enc_out = fluid.layers.sequence_unpad(
self.feature, length=self.seq_len_used)
cls_feats = fluid.layers.dropout(
x=unpad_enc_out,
dropout_prob=0.1,
dropout_implementation="upscale_in_train")
tprint=fluid.layers.Print(cls_feats, message="The content of input layer:")
grnn_hidden_dim=128
bigru_num = 2
init_bound = 0.1
def _bigru_layer(input_feature):
"""define the bidirectional gru layer
"""
pre_gru = fluid.layers.fc(
input=input_feature,
size=grnn_hidden_dim * 3,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound),
regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=1e-4)))
gru = fluid.layers.dynamic_gru(
input=pre_gru,
size=grnn_hidden_dim,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound),
regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=1e-4)))
pre_gru_r = fluid.layers.fc(
input=input_feature,
size=grnn_hidden_dim * 3,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound),
regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=1e-4)))
#print("pre_gru_r",pre_gru_r)
gru_r = fluid.layers.dynamic_gru(
input=pre_gru_r,
size=grnn_hidden_dim,
is_reverse=True,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound),
regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=1e-4)))
bi_merge = fluid.layers.concat(input=[gru, gru_r], axis=1)
return bi_merge
input_feature=cls_feats
#print("input_feature",input_feature)
for i in range(bigru_num):
bigru_output = _bigru_layer(input_feature)
input_feature = bigru_output
cls_feats=bigru_output
if self.hidden_units is not None:
for n_hidden in self.hidden_units:
cls_feats = fluid.layers.fc(
input=cls_feats, size=n_hidden, act="relu")
probs = []
print("numclss:",self.num_classes)
for i in range(self.num_classes):
probs.append(
fluid.layers.fc(
input=cls_feats,
size=2,
param_attr=fluid.ParamAttr(
name="cls_out_w_%d" % i,
initializer=fluid.initializer.TruncatedNormal(
scale=0.02)),
bias_attr=fluid.ParamAttr(
name="cls_out_b_%d" % i,
initializer=fluid.initializer.Constant(0.)),
act="softmax"))
return probs
错误信息
Python Call Stacks (More useful to users):
File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/framework.py", line 2525, in append_op attrs=kwargs.get("attrs", None)) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/layers/loss.py", line 277, in cross_entropy2 attrs=attrs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/layers/loss.py", line 239, in cross_entropy return cross_entropy2(input, label, ignore_index) File "triggercl_gru.py", line 241, in _add_loss input=probs, label=label_split[index]) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 405, in _build_env self.env.loss = self._add_loss() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 508, in main_program self._build_env() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 833, in load_checkpoint main_program=self.main_program) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 367, in init_if_necessary if not self.load_checkpoint(): File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 879, in finetune self.init_if_necessary() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddlehub/finetune/task/base_task.py", line 864, in finetune_and_eval return self.finetune(do_eval=True) File "triggercl_gru.py", line 484, in multi_label_cls_task.finetune_and_eval()
Error Message Summary:
Error: ShapeError: Input(X) and Input(Label) shall have the same shape except the last dimension. But received: the shape of Input(X) is [742, 2],the shape of Input(Label) is [12, 1]. [Hint: Expected framework::slice_ddim(x_dims, 0, rank - 1) == framework::slice_ddim(label_dims, 0, rank - 1), but received framework::slice_ddim(x_dims, 0, rank - 1):742 != framework::slice_ddim(label_dims, 0, rank - 1):12.] at (/paddle/paddle/fluid/operators/cross_entropy_op.cc:51) [operator < cross_entropy2 > error]