'Create a Custom Keras layer that takes many (x, y) pairs with nth order polynomial activation function and weights as polynomial coefficients

I am trying to make a layer that does the following calculation for its output [w0, w1, w2, ... ,wn] * [x^0, x^1, x^2, ... , x^n].T where {w0,...,wn} are trainable weights, {x^0,...,x^n} are an input x to the power of 0 through n, and n is a predefined polynomial order. Basically a single node that does polynomial regression and the weights for that layer can be seen as the coefficients.

This is the layer that I have written: (can only take 1 x at a time, planning to expand once this is working)

class NthOrderPoly(tf.keras.layers.Layer):
def __init__(self, order):
    # units is always = 1
    # nth order diff eq. with m derivatives of the input
    super(NthOrderPoly, self).__init__()
    self.order = order
    self.w = self.add_weight(
        shape=(self.order, 1),
        initializer="random_normal",
        trainable=True,
    )
    
def call(self, inp):
    """
    Expects in of size one
    """
    x = tf.constant([inp**i for i in range(self.order + 1)])
    return tf.matmul(self.w, x, transpose_a=True)

This is me trying to implement it:

x = np.array([1])
y = np.array([2])
model = tf.keras.Sequential()
model.add(NthOrderPoly(2))
model.compile(loss='MSE', optimizer='adam', metrics=['accuracy'])
history = model.fit(x, y, epochs=1)

Error Code: Seems to indicate that I cannot use list comprehension with a tensor? But I am unsure of how to generate the [x^0, ... , x^n].

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_29439/3574454862.py in <module>
----> 1 history = model.fit(x, y, epochs=1)

~/anaconda3/lib/python3.9/site-packages/keras/utils/traceback_utils.py in    error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

 ~/anaconda3/lib/python3.9/site-packages/tensorflow/python/framework/func_graph.py in    autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

TypeError: in user code:

File "/home/mlove/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "/home/mlove/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 867, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/home/mlove/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 860, in run_step  **
    outputs = model.train_step(data)
File "/home/mlove/anaconda3/lib/python3.9/site-packages/keras/engine/training.py", line 808, in train_step
    y_pred = self(x, training=True)
File "/home/mlove/anaconda3/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

TypeError: Exception encountered when calling layer "nth_order_poly_9" (type NthOrderPoly).

in user code:

    File "/tmp/ipykernel_29439/4079227607.py", line 17, in call  *
        x = tf.constant([inp**i for i in range(self.order + 1)])

    TypeError: Expected any non-tensor type, but got a tensor instead.


Call arguments received:
  • inp=tf.Tensor(shape=(None,), dtype=int64)


Solution 1:[1]

it shows invalid credentials

Because your authenticate() returns None

authenticate() takes username and password by default for the authentication and you are providing email and password for authentication. If you want to authenticate from email and password write your own custom authentication backend.

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

class CustomEmailBackend(ModelBackend):
    def authenticate(self, request, email=None, password=None, **kwargs):
        UserModel = get_user_model()
        try:
            user = UserModel.objects.get(email=email)
        except UserModel.DoesNotExist:
            return None
        else:
            if user.check_password(password):
                return user
        return None

After that point your custom authentication in settings.py

AUTHENTICATION_BACKENDS = ['path.to.CustomEmailBackend']

Also make your email filed unique=True

Solution 2:[2]

By default authentication is done by username field of the auth user model, you need to set username_field to email field in the auth user model.Your auth user model should be like this

class User(AbstractBaseUser,PermissionsMixin):
    **fields**
    USERNAME_FIELD = 'email'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sathwik