INNER CODE UNIT · Python
softmax
deepchem/deepchem · contrib/tensorflow_models/__init__.py:29
def softmax(x):
"""Simple numpy softmax implementation
"""
# (n_samples, n_classes)
if len(x.shape) == 2:
row_max = np.max(x, axis=1)
x -= row_max.reshape((x.shape[0], 1))
x = np.exp(x)
row_sum = np.sum(x, axis=1)
x /= row_sum.reshape((x.shape[0], 1))
# (n_samples, n_tasks, n_classes)
elif len(x.shape) == 3:
row_max = np.max(x, axis=2)
x -= row_max.reshape(x.shape[:2] + (1,))
x = np.exp(x)
row_sum = np.sum(x, axis=2)
x /= row_sum.reshape(x.shape[:2] + (1,))
return x