How to implement maclaurin series in keras?

2024/10/4 11:25:48

I am trying to implement expandable CNN by using maclaurin series. The basic idea is the first input node can be decomposed into multiple nodes with different orders and coefficients. Decomposing single nodes to multiple ones can generate different non-linear line connection that generated by maclaurin series. Can anyone give me a possible idea of how to expand CNN with maclaurin series non-linear expansion? any thought?

I cannot quite understand how to decompose the input node to multiple ones with different non-linear line connections that generation by maclaurin series. as far as I know, the maclaurin series is an approximation function but the decomposing node is not quite intuitive to me in terms of implementation. How to implement a decomposing input node to multiple ones in python? How to make this happen easily? any idea?

my attempt:

import tensorflow as tf
import numpy as np
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.datasets import cifar10
from keras.utils import to_categorical(train_imgs, train_label), (test_imgs, test_label)= cifar10.load_data()
output_class = np.unique(train_label)
n_class = len(output_class)nrows_tr, ncols_tr, ndims_tr = train_imgs.shape[1:]
nrows_ts, ncols_ts, ndims_ts = test_imgs.shape[1:]
train_data = train_imgs.reshape(train_imgs.shape[0], nrows_tr, ncols_tr, ndims_tr)test_data = test_imgs.reshape(test_imgs.shape[0], nrows_ts, ncols_ts, ndims_ts)
input_shape = (nrows_tr, ncols_tr, ndims_tr)
train_data = train_data.astype('float32')
trast_data = test_data.astype('float32')
train_data //= 255
test_data //= 255
train_label_one_hot = to_categorical(train_label)
test_label_one_hot = to_categorical(test_label)def pown(x,n):return(x**n)def expandable_cnn(input_shape, output_shape, approx_order):inputs=Input(shape=(input_shape))x= Dense(input_shape)(inputs)y= Dense(output_shape)(x)model = Sequential()model.add(Conv2D(filters=32, kernel_size=(3,3), padding='same', activation="relu", input_shape=input_shape))model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(Dropout(0.25))model.add(Flatten())model.add(Dense(512, activation='relu'))model.add(Dropout(0.5))for i in range(2, approx_order+1):y=add([y, Dense(output_shape)(Activation(lambda x: pown(x, n=i))(x))])model.add(Dense(n_class, activation='softmax')(y))return model

but when I ran the above model, I had bunch of compile errors and dimension error. I assume that the way for Tylor non-linear expansion for CNN model may not be correct. Also, I am not sure how to represent weight. How to make this work? any possible idea of how to correct my attempt?

desired output:

I am expecting to extend CNN with maclaurin series non-linear expansion, how to make the above implementation correct and efficient? any possible idea or approach?

Answer

Interesting question. I have implemented a Keras model that computes the Taylor expansion as you described:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Lambdadef taylor_expansion_network(input_dim, max_pow):x = Input((input_dim,))# 1. Raise input x_i to power p_i for each i in [0, max_pow].def raise_power(x, max_pow):x_ = x[..., None]  # Shape=(batch_size, input_dim, 1)x_ = tf.tile(x_, multiples=[1, 1, max_pow + 1])  # Shape=(batch_size, input_dim, max_pow+1)pows = tf.range(0, max_pow + 1, dtype=tf.float32)  # Shape=(max_pow+1,)x_p = tf.pow(x_, pows)  # Shape=(batch_size, input_dim, max_pow+1)x_p_ = x_p[..., None]  # Shape=(batch_size, input_dim, max_pow+1, 1)return x_p_x_p_ = Lambda(lambda x: raise_power(x, max_pow))(x)# 2. Multiply by alpha coefficientsh = LocallyConnected2D(filters=1,kernel_size=1,  # This layer is computing a_i * x^{p_i} for each i in [0, max_pow]use_bias=False)(x_p_)  # Shape=(batch_size, input_dim, max_pow+1, 1)# 3. Compute s_i for each i in [0, max_pow]def cumulative_sum(h):h = tf.squeeze(h, axis=-1)  # Shape=(batch_size, input_dim, max_pow+1)s = tf.cumsum(h, axis=-1)  # s_i = sum_{j=0}^i h_j. Shape=(batch_size, input_dim, max_pow+1)s_ = s[..., None]  # Shape=(batch_size, input_dim, max_pow+1, 1)return s_s_ = Lambda(cumulative_sum)(h)# 4. Compute sum w_i * s_i each i in [0, max_pow]s_ = LocallyConnected2D(filters=1,  # This layer is computing w_i * s_i for each i in [0, max_pow]kernel_size=1,use_bias=False)(s_)  # Shape=(batch_size, input_dim, max_pow+1)y = Lambda(lambda s_: tf.reduce_sum(tf.squeeze(s_, axis=-1), axis=-1))(s_)  # Shape=(batch_size, input_dim)# Return Taylor expansion modelmodel = Model(inputs=x, outputs=y)model.summary()return model

The implementation applies the same Taylor expansion to each element of the flattened tensor with shape (batch_size, input_dim=512) coming from the convolutional network.


UPDATE: As we discussed in the comments section, here is some code to show how your function expandable_cnn could be modified to integrate the model defined above:

def expandable_cnn(input_shape, nclass, approx_order):inputs = Input(shape=(input_shape))h = inputsh = Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=input_shape)(h)h = Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(h)h = MaxPooling2D(pool_size=(2, 2))(h)h = Dropout(0.25)(h)h = Flatten()(h)h = Dense(512, activation='relu')(h)h = Dropout(0.5)(h)taylor_model = taylor_expansion_network(input_dim=512, max_pow=approx_order)h = taylor_model(h)h = Activation('relu')(h)print(h.shape)h = Dense(nclass, activation='softmax')(h)model = Model(inputs=inputs, outputs=h)return model

Please note that I do not guarantee that your model will work (e.g. that you will get good performance). I just provided a solution based on my interpretation of what you want.

https://en.xdnf.cn/q/70609.html

Related Q&A

Rowwise min() and max() fails for column with NaNs

I am trying to take the rowwise max (and min) of two columns containing datesfrom datetime import date import pandas as pd import numpy as np df = pd.DataFrame({date_a : [date(2015, 1, 1), date(2012…

Convert column suffixes from pandas join into a MultiIndex

I have two pandas DataFrames with (not necessarily) identical index and column names. >>> df_L = pd.DataFrame({X: [1, 3], Y: [5, 7]})>>> df_R = pd.DataFrame({X: [2, 4], Y: [6, 8]})I c…

sys-package-mgr*: cant create package cache dir when run python script with Jython

I want to run Python script with Jython. the result show correctly, but at the same time there is an warning message, "sys-package-mgr*: cant create package cache dir"How could I solve this p…

Python WWW macro

i need something like iMacros for Python. It would be great to have something like that:browse_to(www.google.com) type_in_input(search, query) click_button(search) list = get_all(<p>)Do you know …

Django custom context_processors in render_to_string method

Im building a function to send email and I need to use a context_processor variable inside the HTML template of the email, but this dont work.Example:def send_email(plain_body_template_name, html_body_…

Using string as variable name

Is there any way for me to use a string to call a method of a class? Heres an example that will hopefully explain better (using the way I think it should be):class helloworld():def world(self):print &…

How to sum all amounts by date in pandas dataframe?

I have dataframe with fields last_payout and amount. I need to sum all amount for each month and plot the output. df[[last_payout,amount]].dtypeslast_payout datetime64[ns] amount float64 d…

Unable to import decimal in Python 2.7 or Python 3.3 [duplicate]

This question already has answers here:Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError(4 answers…

I Get ImportError: No module named pathlib, even after installing pathlib with pip

This is my first time asking on this site, so sorry if my question is not layed out correctlyy@DESKTOP-MQJ3NCT:~/Real-Time-Voice-Cloning$ python demo_toolbox.py Traceback (most recent call last):File &…

Python regex separate space-delimited words into a list

If I have a string = "hello world sample text"I want to be able to convert it to a list = ["hello", "world", "sample", "text"]How can I do that with re…