This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Channel Mixing MLPs : Allow communication within channels ( features of embeddings ) | |
def channel_mixing( x , channel_mixing_mlp_dims ): | |
# x is a tensor of shape ( batch_size , num_patches , channels ) | |
x = tf.keras.layers.LayerNormalization( epsilon=1e-6 )( x ) | |
x = mlp( x , channel_mixing_mlp_dims ) | |
return x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Token Mixing MLPs : Allow communication within patches. | |
def token_mixing( x , token_mixing_mlp_dims ): | |
# x is a tensor of shape ( batch_size , num_patches , channels ) | |
x = tf.keras.layers.LayerNormalization( epsilon=1e-6 )( x ) | |
x = tf.keras.layers.Permute( dims=[ 2 , 1 ] )( x ) | |
# After transposition, shape of x -> ( batch_size , channels , num_patches ) | |
x = mlp( x , token_mixing_mlp_dims ) | |
return x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multilayer Perceptron with GeLU ( Gaussian Linear Units ) activation | |
def mlp( x , hidden_dims ): | |
y = tf.keras.layers.Dense( hidden_dims )( x ) | |
y = tf.nn.gelu( y ) | |
y = tf.keras.layers.Dense( x.shape[ -1 ] )( y ) | |
y = tf.keras.layers.Dropout( 0.4 )( y ) | |
return y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Launch a coroutine | |
coroutineScope.launch { | |
results = runOnCoroutine( my_data ) | |
// You can update the UI as well! | |
nameTextView.text = results.userName | |
ageTextView.text = results.userAge.toString() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private suspend fun runOnCoroutine( some_data : Data ) = | |
withContext( Dispatchers.Default ) { | |
results = my_lazy_function(data) | |
return@withContext results | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// Called when predict_button is clicked. ( See activity_main.xml ). | |
fun onPredictButtonClick( view : View ) { | |
// Split the String by "," | |
var strX = sampleInputEditText.text.toString().split( "," ).toTypedArray() | |
strX = strX.map{ xi -> xi.trim() }.toTypedArray() | |
// Convert the String[] to float[] | |
val x = dataFrame.convertStringArrayToFloatArray( strX ) | |
// Predict the class with GaussianNB. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
// View elements to take the inputs and display it to the user. | |
private lateinit var sampleInputEditText : TextInputEditText | |
private lateinit var outputTextView : TextView | |
// DataFrame object which will hold the data. | |
private lateinit var dataFrame: DataFrame | |
// GaussianNB object to perform the calculations and return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Predict the label for the given sample. | |
private fun predictLabel( sample : FloatArray ) { | |
val probArray = FloatArray( dataFrame.numClasses ) | |
for ( ( i , priorProb ) in priorProbabilities.values.withIndex()) { | |
// We take the log probabilities so as to avoid underflow. | |
var p = log10( priorProb ) | |
// While we take log, the product is transformed into a sum | |
// log( a . b ) = log(a) + log(b) | |
for ( j in 0 until dataFrame.numFeatures ) { | |
p += featureDistributions[ j ].getLogProb( sample[ i ] ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Class to implement Gaussian Naive Bayes | |
class GaussianNB( private var dataFrame : DataFrame ) { | |
// Array to store Gaussian Distributions for each feature. | |
private var featureDistributions : Array<GaussianDistribution> | |
// Prior probabilities stored in a HashMap of form ( column_name , prior_prob ) | |
private var priorProbabilities : HashMap<String,Float> | |
private var resultCallback : ResultCallback? = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Gaussian Distribution which will be constructed for every feature, given the mean and standard deviation. | |
class GaussianDistribution( var mean : Float , var stdDev : Float ) { | |
private val p = 1f / ( sqrt( 2.0 * Math.PI ) ).toFloat() | |
// Get the likelihood for given x. | |
fun getProb( x : Float ) : Float { | |
val exp = exp( -0.5 * ((x - mean) / stdDev ).pow( 2 ) ).toFloat() | |
return ( 1 / stdDev ) * p * exp | |
} |