dwd31415 / tensorflow-xlogx

A TensorFlow OP for evaluating x*log(x)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tensorflow-xlogx

This package implements a C++/Cuda OP for evaluating this ℓ function(called x*log(x) for reasons of simplicity in the rest of the README file):

This extends the usual x*log(x) by defining it for x=0, the inserted value of 0 is the limit of said function for x -> 0:

Installation

Just clone this repo and run

python3 setup.py install

TensorFlow already has to be installed and should be up to date.

Example

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow_xlogx import xlogx as tf_xlogx

xs = np.linspace(0,3,100)
sess = tf.Session()
ys = sess.run(tf_xlogx(xs))
plt.plot(xs,ys)
plt.show()

Configuration

By changing the content of src/settings.h and reinstalling you can configure this package.

THROW_ERROR_FOR_NEGATIVE_LOGS_ON_CPU

This is a simple on/off switch if you set it to 1, the xlogx function will not return NaN for negtive inputs any more, but instead throw an error, this behaviour is only implemented on the GPU right now.

Default: 0

FIX_DERIVATIVE_FROM_0_TO_EPSILON

The limit of log(x) + 1(the deriavtive of x*log(x)) for x->0 is not a real number, but -∞(an extended real number), therefore we have to choose some value for it, this option allows you to extend this and make the derivative of x*log(x) return a fixed value for all 0 ≤ x ≤ ϵ. This allows you to make the derivative a continuous function.

Default: 0

EPSILON

The ϵ mentioned in FIX_DERIVATIVE_FROM_0_TO_EPSILON.

Default: 1e-3

FIXED_DERIVATIVE_VALUE

The value for the derivative of x*log(x) for x=0 or 0 ≤ x ≤ ϵ(see FIX_DERIVATIVE_FROM_0_TO_EPSILON).

Default: -1.0

Use Case

Implementing the ℓ function in tensorflow is to my knowledge only possible with some kind of switch, if raw performance is paramount this is very inefficent, by implementing ℓ in pure C++ and CUDA C this package is a way around the need for symbolic switches in your TensorFlow graph.

About

A TensorFlow OP for evaluating x*log(x)

License:Apache License 2.0


Languages

Language:C++ 53.4%Language:Python 16.4%Language:Cuda 13.4%Language:CMake 10.1%Language:TeX 3.9%Language:C 2.8%