| 199 | return h_all #shape:[batch_size,block_size,hidden_size] |
| 200 | |
| 201 | def loss(self, l2_lambda=0.0001): # 0.001 |
| 202 | with tf.name_scope("loss"): |
| 203 | # input: `logits`:[batch_size, num_classes], and `labels`:[batch_size] |
| 204 | # output: A 1-D `Tensor` of length `batch_size` of the same type as `logits` with the softmax cross entropy loss. |
| 205 | losses = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.answer_single,logits=self.logits); # sigmoid_cross_entropy_with_logits.#losses=tf.nn.softmax_cross_entropy_with_logits(labels=self.input_y,logits=self.logits) |
| 206 | # print("1.sparse_softmax_cross_entropy_with_logits.losses:",losses) # shape=(?,) |
| 207 | loss = tf.reduce_mean(losses) # print("2.loss.loss:", loss) #shape=() |
| 208 | l2_losses = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables() if ('bias' not in v.name ) and ('alpha' not in v.name)]) * l2_lambda |
| 209 | loss = loss + l2_losses |
| 210 | return loss |
| 211 | |
| 212 | def loss_multilabel(self, l2_lambda=0.0001): #this loss function is for multi-label classification |
| 213 | with tf.name_scope("loss"): |