-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathLoss.py
34 lines (24 loc) · 851 Bytes
/
Loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import useful packages
import tensorflow as tf
def loss(y, prediction, l2_norm=True):
'''
This is the Loss Function (Euclidean Distance)
We will provide more functions later.
Args:
y: The true label
prediction: predicted label
l2_norm: l2 regularization, set True by default
Returns:
loss: The loss of the Model
'''
train_variable = tf.trainable_variables()
if l2_norm == False:
loss = tf.reduce_mean(tf.square(y - prediction))
else:
norm_coefficient = 0.001
regularization_loss = norm_coefficient * tf.reduce_sum([tf.nn.l2_loss(v) for v in train_variable])
model_loss = tf.reduce_mean(tf.square(y - prediction))
loss = tf.reduce_mean(model_loss + regularization_loss)
return loss