鱼C论坛

 找回密码
 立即注册
查看: 880|回复: 1

[技术交流] Python实现PNN【tensorflow2.0】

[复制链接]
发表于 2021-1-2 12:51:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2021-1-2 18:28 编辑

说明:待验证,有问题的地方欢迎批评指正

  1. #Inner Product-based Neural Network
  2. import tensorflow as tf
  3. from tensorflow.keras import layers, optimizers, Sequential
  4. from tensorflow import keras

  5. class PNN_Inner(keras.Model):
  6.     def __init__(self, D1):
  7.         super(PNN_Inner, self).__init__()
  8.         self.D1 = D1
  9.         
  10.     def build(self, input_shape):
  11.         self.N = input_shape[1]
  12.         self.M = input_shape[-1]
  13.         #[N, M, D1]
  14.         self.Wz = self.add_weight(shape = (self.N, self.M, self.D1),
  15.                                  trainable = True)
  16.         #[D1, N]
  17.         self.theta = self.add_weight(shape = (self.D1, self.N),
  18.                                  trainable = True)
  19.         #[batch, D1] -> [batch, 1]
  20.         self.l1 = layers.Dense(24, activation = tf.nn.leaky_relu)
  21.         self.bn1 = layers.BatchNormalization(axis = 1)
  22.         self.drop1 = layers.Dropout(0.5)
  23.         self.l2 = layers.Dense(12, activation = tf.nn.leaky_relu)
  24.         self.bn2 = layers.BatchNormalization(axis = 1)
  25.         self.drop2 = layers.Dropout(0.5)
  26.         self.l3 = layers.Dense(1, activation = tf.nn.sigmoid)
  27.         super(PNN_Inner, self).build(input_shape)
  28.    
  29.     def call(self, x, training = None):
  30.         #x:[batch, N, M]
  31.         #w:[N, M, D1]
  32.         linear = []
  33.         for i in range(self.D1):
  34.             #[N, M, 1] -> [N, M]
  35.             w = self.Wz[:, :, i]
  36.             #[batch, N. M] * [N, M] -> [batch, N, M]
  37.             temp = tf.multiply(x, w)
  38.             #[batch, N, M] -> [batch, 1]
  39.             temp = tf.expand_dims(tf.reduce_mean(temp, axis = [1, 2]), axis = 1)
  40.             linear.append(temp)
  41.         #linear:[batch, D1]
  42.         linear = tf.concat(linear, axis = 1)
  43.                            
  44.         product = []
  45.         #[batch, N, M] * [batch, N, M] -> [batch, N, M]
  46.         p = tf.multiply(x, x)
  47.         for i in range(self.D1):
  48.             #[N] -> [1, N]
  49.             theta = tf.expand_dims(self.theta[i,:], axis =0)
  50.             #[N, 1] @ [1, N] -> [N, N]
  51.             w = tf.matmul(tf.transpose(theta), theta)
  52.             #[batch, N, M] -> [batch, M, N]
  53.             f = tf.transpose(p, perm = [0, 2, 1])
  54.             #[batch, M, N] * [N, N] -> [batch, M, N]
  55.             temp = tf.matmul(f, w)
  56.             #[batch, M, N] -> [batch, 1]
  57.             temp = tf.expand_dims(tf.reduce_mean(temp, axis = [1, 2]), axis = 1)            
  58.             product.append(temp)
  59.         #product:[batch, D1]
  60.         product = tf.concat(product, axis = 1)
  61.         #[batch, D1] -> [batch, 2D1]
  62.         out = tf.concat([linear, product],axis = 1)
  63.         out = self.drop1(self.bn1(self.l1(out), training), training)
  64.         out = self.drop2(self.bn2(self.l2(out), training), training)
  65.         out = self.l3(out)
  66.         return out

  67.       
  68. model = PNN_Inner(D1 = 20)
  69. model.build((None,60, 30))
  70. model.summary()
复制代码



  1. #Outer Product-based Neural Network
  2. import tensorflow as tf
  3. from tensorflow.keras import layers, optimizers, Sequential
  4. from tensorflow import keras

  5. class PNN_Inner(keras.Model):
  6.     def __init__(self, D1):
  7.         super(PNN_Inner, self).__init__()
  8.         self.D1 = D1
  9.         
  10.     def build(self, input_shape):
  11.         self.N = input_shape[1]
  12.         self.M = input_shape[-1]
  13.         #[N, M, D1]
  14.         self.Wz = self.add_weight(shape = (self.N, self.M, self.D1),
  15.                                  trainable = True)
  16.         #[D1, M]
  17.         self.theta = self.add_weight(shape = (self.D1, self.M),
  18.                                  trainable = True)
  19.         #[batch, D1] -> [batch, 1]
  20.         self.l1 = layers.Dense(24, activation = tf.nn.leaky_relu)
  21.         self.bn1 = layers.BatchNormalization(axis = 1)
  22.         self.drop1 = layers.Dropout(0.5)
  23.         self.l2 = layers.Dense(12, activation = tf.nn.leaky_relu)
  24.         self.bn2 = layers.BatchNormalization(axis = 1)
  25.         self.drop2 = layers.Dropout(0.5)
  26.         self.l3 = layers.Dense(1, activation = tf.nn.sigmoid)
  27.         super(PNN_Inner, self).build(input_shape)
  28.    
  29.     def call(self, x, training = None):
  30.         #x:[batch, N, M]
  31.         #w:[N, M, D1]
  32.         linear = []
  33.         for i in range(self.D1):
  34.             #[N, M, 1] -> [N, M]
  35.             w = self.Wz[:, :, i]
  36.             #[batch, N. M] * [N, M] -> [batch, N, M]
  37.             temp = tf.multiply(x, w)
  38.             #[batch, N, M] -> [batch, 1]
  39.             temp = tf.expand_dims(tf.reduce_mean(temp, axis = [1, 2]), axis = 1)
  40.             linear.append(temp)
  41.         #linear:[batch, D1]
  42.         linear = tf.concat(linear, axis = 1)
  43.                            
  44.         product = []
  45.         #[batch, N, M] -> [batch, M]
  46.         fi = tf.reduce_mean(x, axis = 1)
  47.         #[batch, M] !* [batch, M] -> [batch, M, M]
  48.         p = tf.einsum('ai,aj->aij', fi, fi)
  49.         for i in range(self.D1):
  50.             #[M] -> [1, M]
  51.             theta = tf.expand_dims(self.theta[i,:], axis =0)
  52.             #[M, 1] @ [1, M] -> [M, M]
  53.             w = tf.matmul(tf.transpose(theta), theta)
  54.             #[batch, M, M] * [M, M] -> [batch, M, M]
  55.             temp = tf.matmul(p, w)
  56.             #[batch, M, M] -> [batch, 1]
  57.             temp = tf.expand_dims(tf.reduce_mean(temp, axis = [1, 2]), axis = 1)            
  58.             product.append(temp)
  59.         #product:[batch, D1]
  60.         product = tf.concat(product, axis = 1)
  61.         #[batch, D1] -> [batch, 2D1]
  62.         out = tf.concat([linear, product],axis = 1)
  63.         out = self.drop1(self.bn1(self.l1(out), training), training)
  64.         out = self.drop2(self.bn2(self.l2(out), training), training)
  65.         out = self.l3(out)
  66.         return out

  67.       
  68. model = PNN_Inner(D1 = 20)
  69. model.build((None,60, 30))
  70. model.summary()
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-2 18:29:05 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-27 12:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表