Monday, 15 February 2021

any workaround to expand tensor with Taylor series in tensorflow?

In TensorFlow, I intend to manipulate tensor with Taylor series of sin(x) with certain approximation terms. To do so, I have tried to manipulate the grayscale image (shape of (32,32)) with Taylor series of sin(x) and it works fine. Now I have trouble manipulating the same things that worked for a grayscale image with the shape of (32,32) to RGB image with the shape of (32,32,3), and it doesn't give me the correct array. Intuitively, I am trying to manipulate tensor with Taylor's expansion of sin(x). Can anyone show me the possible way of doing this in tensorflow? Any idea?

my attempt:

here is taylor expansion of sin(x) at x=0: 1- x + x**2/2 - x**3/6 with three expansion term.

from tensorflow.keras.datasets import cifar10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

x= X_train[1,:,:,1]
n_terms = 3
func = 'sin(x)'

new_x = np.zeros((x.shape[0], x.shape[1]*n_terms))
new_x = new_x.astype('float32') 
nn = 0
for i in range(x.shape[1]):
    col_d = x[:,i].ravel()
    new_x[:,nn] = col_d
    if n_terms > 0:
        for j in range(1,n_terms):
            if func == 'sin(x)': # sin(x)
                new_x[:,nn+j] = new_x[:,nn+j-1] + (((-1)**j)/np.math.factorial(2*j+1))*(col_d**(2*j+1))

so the above attempt worked. But I am trying the same expansion to 3 dim arrays, it is not working.

x= X_train[1,:,:,:]
n_terms = 3
func = 'sin(x)'

new_x = np.zeros((x.shape[0], x.shape[1]*n_terms))
new_x = new_x.astype('float32') 
nn = 0
for i in range(x.shape[2]):
    # col_d = x[:,i].ravel()
    col_d = x.transpose(0,1,2).reshape(x.shape[1],-1)
    new_x[:,nn] = col_d
    if n_terms > 0:
        for j in range(1,n_terms):
            if func == 'sin(x)': # sin(x)
                new_x[:,nn+j] = new_x[:,nn+j-1] + (((-1)**j)/np.math.factorial(2*j+1))*(col_d**(2*j+1))

I think I could do this more efficiently with TensorFlow but that's not quite intuitive for me how to do it. Can anyone suggest a possible workaround to make this work? Any thought?

update:

In 2dim array col_d = x[:,i].ravel() is pixel vector which flattened 2 dim array. Similarly, we could reshape 3dim array to 2 dim by this way: x.transpose(0,1,2).reshape(x.shape[1],-1) in for loop, so it could be x[:,i].transpose(0,1,2).reshape(x.shape[1],-1), but this is still not correct. I think tensorflow might have better way of doing this. How can we manipulate the tensor with taylor series of sin(x) more efficiently? Any thoughts?

goal:

Intuitively, in Taylor series of sin(x), x is tensor, and if we want only 2, 3 approximation terms of Taylor series of sin(x) for each tensor, I want to concatenate them in new tensor. How should we do it efficiently in TensorFlow? Any thoughts?



from any workaround to expand tensor with Taylor series in tensorflow?

No comments:

Post a Comment