I
code:
from tensorflow.keras.layers import Layer
class PCA(Layer) :
def __init__(self, n_components):
super(PCA,self).__init__()
self.n_components = n_components
self.components = None
self.mean = None
def build (self, X):
#mean centering
self.mean = tf.reduce_mean(tf.shape(X),axis=0,keepdims=True) #np.mean(X, axis=0)
X = X - self.mean
#covariance, function needs samples as columns
cov = np.cov(X.T)
# Eigenvectors, eigenvalues
eigenvectors, eigenvalues = np.linalg.eig(cov)
# Sort eigenvectors
idxs = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]
self.components = eigenvectors[:self.n_components]
def call (self,X):
#project data
X = X - self.mean
return np.dot(X, self.components.T)
def mpox(inputs):
features = feature_extraction(inputs)
pca = PCA(n_components=100)
reduced_features = pca(features)
classification_output = classifier(reduced_features)
mpox_Model = Model(inputs = inputs, outputs = classification_output)
return mpox_Model
I wanted to sandwich a custome PCA layer into a model right after the feature extraction function. I want to reduce the dimension of the extracted features before classification.But I seem to be getting ValueError: Cannot convert a partially known TensorShape all the time I have tried using the tf.shape but it isn't working.