微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在张量流中使用 CNN 进行特征提取后使用 PCA 作为降维

如何解决在张量流中使用 CNN 进行特征提取后使用 PCA 作为降维

我正在尝试使用 PCA 过滤器来减少从 tensorflow 中使用 CNN 的图像中提取的特征的维度 我的问题是我的模型没有学习,我认为梯度没有上升到我的 PCA 层以上,这是我的 PCA 函数

def PCAF(cov_mat):
eigen_values,eigen_vectors = tf.linalg.eigh(cov_mat)
sorted_index = tf.argsort(eigen_values)[::-1]

sorted_eigenvalues = tf.gather(eigen_values,sorted_index)
sorted_eigenvectors = tf.gather(eigen_vectors,sorted_index)
vard = eigen_values
eigenvector_subset = sorted_eigenvectors[:,0:256]

return eigenvector_subset

我确实把这个函数放在了一个 Lambda 层,这是我的完整模型

# the final model

# extracting the feature map from images using MobileNetV2
extracted_features = tf.keras.layers.Flatten()(last_output)

# the PCA filter

meanX = Lambda(lambda x: tf.reduce_mean(x,axis=0,keepdims=True))(extracted_features)
# subtract the mean from matrix
standardized = tf.keras.layers.Subtract()([extracted_features,meanX])
# create the covarience matrix
Cov_mat = Lambda(lambda x : tf.matmul(tf.transpose(x),x))(standardized)
Cov_mat = Lambda(lambda x: x / 256)(Cov_mat)

# extract the principal components from the cov matrix
principal_features = Lambda(PCAF)(Cov_mat)

# project my data on the principal axis extracted with PCA
whitened_encoding = Lambda(lambda x : tf.matmul(x[0],x[1]))([standardized,principal_features])

# classification phase 
class_layer1 = tf.keras.layers.Dense(1024,activation='relu')(whitened_encoding)
class_layer2 = tf.keras.layers.Dense(512,activation='relu')(class_layer1)
class_layer3 = tf.keras.layers.Dense(256,activation='relu')(class_layer2)
final_class_layer = tf.keras.layers.Dense(45,activation='softmax')(class_layer)
outputs = final_class_layer

model = tf.keras.Model(pre_trained_model.input,outputs)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。