In [1]:
import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
In [2]:
logits = np.array([[2, 1, 0], [0,1,2]], dtype=np.float32)
one_hot_labels = np.array([[0,1,0], [0,0,1]], dtype=np.int32)
sparse_labels = np.array([0,2], dtype=np.int32)
sparse_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=sparse_labels)
softmax_loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=one_hot_labels)
# weighted cross entropy is sigmoid. Requires targets to be same type as logits
targets = one_hot_labels.astype(np.float32)
weighted = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=1)
weighted_2 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=2)
weighted_neg_1 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=-1)
print 'sparse softmax crossentropy', sess.run(tf.reduce_mean(sparse_loss))
print 'regular softmax cross entropy', sess.run(tf.reduce_mean(softmax_loss))
print 'weighted cross weight 1', sess.run(tf.reduce_mean(weighted))
print 'weighted cross weight 2', sess.run(tf.reduce_mean(weighted_2))
print 'weighted cross weight -1', sess.run(tf.reduce_mean(weighted_neg_1))
In [3]:
n = 100
logits = np.reshape(np.linspace(-10,10, n), (n,1))
labels = np.zeros((n,1), dtype=np.int32) + 1
targets = np.zeros((n,1) ) + 1
softmax_loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)
weighted_loss_1 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=1)
weighted_loss_2 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=2)
weighted_loss_3 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=3)
weighted_loss_neg_1 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=-1)
weighted_loss_zero = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=0)
sl = sess.run(softmax_loss)
w1 = np.squeeze(sess.run(weighted_loss_1))
w2 = np.squeeze(sess.run(weighted_loss_2))
w3 = np.squeeze(sess.run(weighted_loss_3))
wneg1 = np.squeeze(sess.run(weighted_loss_neg_1))
w0= np.squeeze(sess.run(weighted_loss_zero))
In [4]:
data = {'logits':np.squeeze(logits), 'SMCE':sl, 'WCE_-1':wneg1, 'WCE_0': w0, 'WCE_1':w1, 'WCE_2':w2, 'WCE_3':w3}
df = pd.DataFrame.from_dict(data)
df.plot(x='logits')
plt.show()
In [33]:
n = 10
logits = np.array([np.linspace(-10,10, n), np.linspace(10,-10, n)]).T
labels = np.array([1] * n)
targets = np.array([np.zeros(n, dtype=np.float32), [1] *n]).T
softmax_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)
weighted_loss_1 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=1)
weighted_loss_2 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=2)
weighted_loss_3 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=3)
weighted_loss_neg_1 = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=-1)
weighted_loss_zero = tf.nn.weighted_cross_entropy_with_logits(targets, logits, pos_weight=0)
sl = sess.run(softmax_loss)
w1 = np.mean(sess.run(weighted_loss_1), 1)
w2 = np.mean(sess.run(weighted_loss_2), 1)
w3 = np.mean(sess.run(weighted_loss_3), 1)
wneg1 = np.mean(sess.run(weighted_loss_neg_1), 1)
w0= np.mean(sess.run(weighted_loss_zero), 1)
#print diff_logits.shape
diff_logits = logits[:,1] - logits[:,0]
In [34]:
data = {'logits':diff_logits, 'SMCE':sl, 'WCE_-1':wneg1, 'WCE_0': w0, 'WCE_1':w1, 'WCE_2':w2, 'WCE_3':w3}
print data
df = pd.DataFrame.from_dict(data)
df.plot(x='logits')
plt.show()
In [ ]: