Skip to content

Instantly share code, notes, and snippets.

View risenW's full-sized avatar

Rising Odegua risenW

View GitHub Profile
@risenW
risenW / weighted-cross-entropy.py
Last active July 16, 2018 18:46
Code for weigted cross entropy using two weights
#comparing two weights
weight = tf.constant(1.)
x_entropy_weighted_vals = tf.nn.weighted_cross_entropy_with_logits(targets=Y_labels, logits=Y_pred, pos_weight=weight)
x_entropy_weighted_out = sess.run(x_entropy_weighted_vals)
weight2 = tf.constant(0.5)
x_entropy_weighted_val_2 = tf.nn.weighted_cross_entropy_with_logits(targets=Y_labels, logits=Y_pred, pos_weight=weight2)
x_entropy_weighted_out_2 = sess.run(x_entropy_weighted_val_2)
#ploting the predicted values against the Sigmoid cross entropy loss
@risenW
risenW / L2-loss.py
Created July 16, 2018 18:49
Code for L2 loss function
#Calculating the L2 loss
val = tf.square(Y_truth - Y_pred)
L2_val = sess.run(val)
#ploting the predicted values against the L2 loss
Y_array = sess.run(Y_pred)
plt.plot(Y_array, L2_val, 'b-', label='L2 loss' )
plt.title('L2 loss')
plt.xlabel('$Y_{pred}$', fontsize=15)
plt.ylabel('$Y_{true}$', fontsize=15)
@risenW
risenW / startcode.py
Last active July 17, 2018 11:40
Imports for loss function tutorial on mediumi
# Import libraries
import matplotlib.pyplot as plt
import tensorflow as tf
#Start a tensorflow session
sess = tf.Session()
# Create our sample data from a line space
Y_pred = tf.linspace(-1., 1., 500)
#Create our target as a zero constant tensor
Y_truth = tf.constant(0.)
@risenW
risenW / L1-loss.py
Last active July 17, 2018 11:53
Code for L1 norm loss/ Absolute loss function
#Computing L1 loss with the same values
temp = tf.abs(Y_truth - Y_pred)
L1_val = sess.run(temp)
#ploting the predicted values against the L2 loss
Y_array = sess.run(Y_pred)
plt.plot(Y_array, L1_val, 'r-' )
plt.title('L1 loss')
plt.xlabel('$Y_{pred}$', fontsize=15)
plt.ylabel('$Y_{true}$', fontsize=15)
@risenW
risenW / P-huber-loss.py
Created July 16, 2018 18:54
Code for Pseudo Huber loss
#Plot of the Pseudo-Huber loss
delta = tf.constant(0.24)
temp_ph = tf.multiply(tf.square(delta),tf.sqrt(1. + tf.square((Y_truth - Y_pred) / delta)) - 1. )
pseudo_h_vals = sess.run(temp_ph)
#ploting the predicted values against the L2 loss
Y_array = sess.run(Y_pred)
plt.plot(Y_array, pseudo_h_vals, 'g-' )
plt.title('Pseudo Huber loss')
plt.xlabel('$Y_{pred}$', fontsize=15)
plt.ylabel('$Y_{true}$', fontsize=15)
@risenW
risenW / cross-entropy.py
Created July 16, 2018 18:56
Code for cross entropy loss
#Redefining our data
Y_pred = tf.linspace(-4., 6., 500)
Y_label = tf.constant(1.)
Y_labels = tf.fill([500,], 1.)
#applying sigmoid
x_entropy_vals = - tf.multiply(Y_label, tf.log(Y_pred)) - tf.multiply((1. - Y_label), tf.log(1. - Y_pred))
x_entropy_loss = sess.run(x_entropy_vals)
#ploting the predicted values against the cross entropy loss
Y_array = sess.run(Y_pred)
@risenW
risenW / sig-cross-entropy.py
Created July 16, 2018 18:58
Code for Sigmoid cross entropy loss
x_entropy_sigmoid_vals = tf.nn.sigmoid_cross_entropy_with_logits(labels= Y_labels, logits=Y_pred)
x_entropy_sigmoid_out = sess.run(x_entropy_sigmoid_vals)
#ploting the predicted values against the Sigmoid cross entropy loss
Y_array = sess.run(Y_pred)
plt.plot(Y_array, x_entropy_sigmoid_out, 'y-' )
plt.title('Sigmoid cross entropy loss')
plt.xlabel('$Y_{pred}$', fontsize=15)
plt.ylabel('$Y_{label}$', fontsize=15)
plt.ylim(-2, 5)
plt.show()
@risenW
risenW / softmax-ce-loss.py
Created July 16, 2018 18:59
code for Softmax cross entropy loss
y_pred_dist = tf.constant([[1., -3., 10.]])
target_dist = tf.constant([[0.1, 0.02, 0.88]])
softmax_xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=target_dist, logits=y_pred_dist)
print(sess.run(softmax_xentropy))
@risenW
risenW / softmax-sparse-ce.py
Created July 16, 2018 19:00
Code for Sparse softmax cross entropy
y_pred = tf.constant([[1., -3., 10.]])
sparse_target_dist = tf.constant([2]) #true value is in the second position of the sparse tensor
sparse_x_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels= sparse_target_dist, logits=y_pred)
print(sess.run(sparse_x_entropy))
@risenW
risenW / stochastic_training.py
Last active July 17, 2018 13:25
code for stochastic training on python using Tensorflow
#Load our libraries
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
sess = tf.Session()
# We create our data, placeholders and variables
x_val = np.random.normal(1, 0.1, 100) #input values
y_val = np.repeat(10., 100) #target values