Wednesday, 22 February 2023

Tensorflow : Trainable variable not getting learnt

I am trying to implement a custom modified ReLU in Tensorflow 1, in which I use two learnable parameters. But the parameters are not getting learnt even after running 1000 training steps, as suggested by printing their values before and after training. I have observed that inside the function, when I do not split x, i.e. execute the commented lines, then the coefficients are learnt. Could anyone suggest why splitting the input results in the trainable coefficients not being learnt and how this can be resolved?

import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

def weight_variable(shape,vari_name):                   
    initial = tf.truncated_normal(shape, stddev=0.1,dtype=tf.float32)
    return tf.Variable(initial,name = vari_name)
        
def init_Prelu_coefficient(var1, var2):
    coeff = tf.truncated_normal(([1]), stddev=0.1,dtype=tf.float32)
    coeff1 = tf.truncated_normal(([1]), stddev=0.1,dtype=tf.float32)

    return tf.Variable(coeff, trainable=True, name=var1), tf.Variable(coeff1, trainable=True, name=var2)
        
def Prelu(x, coeff, coeff1):
    s = int(x.shape[-1])    
    sop = x[:,:,:,:s//2]*coeff+x[:,:,:,s//2:]*coeff1
    sop1 = x[:,:,:,:s//2]*coeff-x[:,:,:,s//2:]*coeff1
    copied_variable = tf.concat([sop, sop1], axis=-1)
    copied_variable = tf.math.maximum(copied_variable,0)/copied_variable
    # copied_variable = tf.identity(x)
    # copied_variable = tf.math.maximum(copied_variable*coeff+copied_variable*coeff1,0)/copied_variable
    # copied_variable = tf.multiply(copied_variable,x)
    return copied_variable
    
def conv2d_dilate(x, W, dilate_rate):
    return tf.nn.convolution(x, W,padding='VALID',dilation_rate = [1,dilate_rate])

matr = np.random.rand(1, 60, 40, 8)
target = np.random.rand(1, 58, 36, 8)

def learning(sess):
    # define placeholder for inputs to network
    
    Input = tf.placeholder(tf.float32, [1, 60, 40, 8])                                   
    input_Target = tf.placeholder(tf.float32, [1, 58, 36, 8])             

    kernel = weight_variable([3, 3, 8, 8],'G1')
    coeff, coeff1 = init_Prelu_coefficient('alpha', 'alpha1')
    conv = Prelu(conv2d_dilate(Input, kernel , 2), coeff, coeff1) 

    error_norm = 1*tf.norm(input_Target - conv) 
    print("MOMENTUM LEARNING")
    train_step = tf.train.MomentumOptimizer(learning_rate=0.001,momentum=0.9,use_nesterov=False).minimize(error_norm)
    
    if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
        init = tf.initialize_all_variables()
    else:
        init = tf.global_variables_initializer()
    sess.run(init)
    print("INIT coefficient ", sess.run(coeff), sess.run(coeff1))
    init_var = tf.trainable_variables()

    error_prev = 1  # initial error, we set 1 and it began to decrease.
    for i in range(1000):
        
        sess.run(train_step, feed_dict={Input: matr, input_Target: target})
        if i % 100 == 0:                                                                         
            error_now=sess.run(error_norm,feed_dict={Input : matr, input_Target: target})     
            print('The',i,'th iteration gives an error',error_now)                              
            
    error = sess.run(error_norm,feed_dict={Input: matr, input_Target: target})

    print(sess.run(kernel))
    print("LEARNT coefficient ", sess.run(coeff), sess.run(coeff1))

sess = tf.Session() 
learning(sess)


from Tensorflow : Trainable variable not getting learnt

Tuesday, 21 February 2023

Android: How to Send user to Install an App on Google Play Store and then Detect Install Start and progress

I'm looking to send a user to install an app from the Play Store, then once they click the install button I want to send them back to my app and show a progress bar and helpful info.

There is an app called Mistplay that does this. I know they detect the app installation, but I'm not sure if they are faking the app Install progress or not.

Any suggestions on this? I'm able to use intents to launch the play store market but I don't know how to detect the user clicking install in Play Store and sending the user back to my app or any of the steps past that.

Edit 1:

So I am able to detect when the installation finishes by including the target app in my Manifest Queries section + having this code (overly verbose for testing) in my main activity onCreate() + onDestroy unregister.

        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_PACKAGE_ADDED)
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED)
        filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED)
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED)
        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED)
        filter.addDataScheme("package")
        registerReceiver(appInstallBroadcastReceiver, filter)


from Android: How to Send user to Install an App on Google Play Store and then Detect Install Start and progress

Attempting to mock child method, fails if parent module imports it using object destructuring

I was doing some basic jest unit testing in attempt to learn it more.

I have this issue I do not know how to explain

This file has the child function add

// FileB.js 

const add = (a, b) => {
  return a + b;
}

module.exports = {
  add,
};

This file has the parent function addTen

// FileA.js

const { add } = require('./FileB');

const addTen = num => {
  return add(10, num);
}

module.exports = {
  addTen,
};

this is my test file, where I am trying to either check <mockedFunction/spy>.mock.calls or do toHaveBeenCalledWith to see if the inner child method, add, is being passed in 10,10 when i call addTen(10);

This file is not used in any real env, its simply me trying to learn jest + unit testing more.

// randomTest.js

const { addTen } = require('../src/FileA');
const fileB = require('../src/FileB'); 

describe('temp', () => {
  it('temp', () => {
    const addSpy = jest.spyOn(fileB, 'add');

    addTen(10);
    console.log(addSpy.mock.calls);

    expect(addSpy).toHaveBeenCalledWith(10,10)
  });
});

Now for the issue, the test fails, saying add was never called, or nothing passed in. I logged the add function within FileB

However, if I modify FileA in this way, by importing entore module instead of destructuring, the test passes and I cna log out the functions and everything and see it works

This is what works

// FileA.js

const fileB = require('./FileB');

const addTen = num => {
  return fileB.add(10, num);
}

module.exports = {
  addTen,
};

Why does this slight change work? And is there a way to avoid this and keep my destrcutoring?



from Attempting to mock child method, fails if parent module imports it using object destructuring