Skip to content

Instantly share code, notes, and snippets.

View shubham0204's full-sized avatar
🎯
Focusing

Shubham Panchal shubham0204

🎯
Focusing
View GitHub Profile
# 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
# 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
# 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
// 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()
private suspend fun runOnCoroutine( some_data : Data ) =
withContext( Dispatchers.Default ) {
results = my_lazy_function(data)
return@withContext results
}
...
// 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.
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
// 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 ] )
// 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
// 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
}