Python实现PNN【tensorflow2.0】
本帖最后由 糖逗 于 2021-1-2 18:28 编辑说明:待验证,有问题的地方欢迎批评指正
#Inner Product-based Neural Network
import tensorflow as tf
from tensorflow.keras import layers, optimizers, Sequential
from tensorflow import keras
class PNN_Inner(keras.Model):
def __init__(self, D1):
super(PNN_Inner, self).__init__()
self.D1 = D1
def build(self, input_shape):
self.N = input_shape
self.M = input_shape[-1]
#
self.Wz = self.add_weight(shape = (self.N, self.M, self.D1),
trainable = True)
#
self.theta = self.add_weight(shape = (self.D1, self.N),
trainable = True)
# ->
self.l1 = layers.Dense(24, activation = tf.nn.leaky_relu)
self.bn1 = layers.BatchNormalization(axis = 1)
self.drop1 = layers.Dropout(0.5)
self.l2 = layers.Dense(12, activation = tf.nn.leaky_relu)
self.bn2 = layers.BatchNormalization(axis = 1)
self.drop2 = layers.Dropout(0.5)
self.l3 = layers.Dense(1, activation = tf.nn.sigmoid)
super(PNN_Inner, self).build(input_shape)
def call(self, x, training = None):
#x:
#w:
linear = []
for i in range(self.D1):
# ->
w = self.Wz[:, :, i]
# * ->
temp = tf.multiply(x, w)
# ->
temp = tf.expand_dims(tf.reduce_mean(temp, axis = ), axis = 1)
linear.append(temp)
#linear:
linear = tf.concat(linear, axis = 1)
product = []
# * ->
p = tf.multiply(x, x)
for i in range(self.D1):
# ->
theta = tf.expand_dims(self.theta, axis =0)
# @ ->
w = tf.matmul(tf.transpose(theta), theta)
# ->
f = tf.transpose(p, perm = )
# * ->
temp = tf.matmul(f, w)
# ->
temp = tf.expand_dims(tf.reduce_mean(temp, axis = ), axis = 1)
product.append(temp)
#product:
product = tf.concat(product, axis = 1)
# ->
out = tf.concat(,axis = 1)
out = self.drop1(self.bn1(self.l1(out), training), training)
out = self.drop2(self.bn2(self.l2(out), training), training)
out = self.l3(out)
return out
model = PNN_Inner(D1 = 20)
model.build((None,60, 30))
model.summary()
#Outer Product-based Neural Network
import tensorflow as tf
from tensorflow.keras import layers, optimizers, Sequential
from tensorflow import keras
class PNN_Inner(keras.Model):
def __init__(self, D1):
super(PNN_Inner, self).__init__()
self.D1 = D1
def build(self, input_shape):
self.N = input_shape
self.M = input_shape[-1]
#
self.Wz = self.add_weight(shape = (self.N, self.M, self.D1),
trainable = True)
#
self.theta = self.add_weight(shape = (self.D1, self.M),
trainable = True)
# ->
self.l1 = layers.Dense(24, activation = tf.nn.leaky_relu)
self.bn1 = layers.BatchNormalization(axis = 1)
self.drop1 = layers.Dropout(0.5)
self.l2 = layers.Dense(12, activation = tf.nn.leaky_relu)
self.bn2 = layers.BatchNormalization(axis = 1)
self.drop2 = layers.Dropout(0.5)
self.l3 = layers.Dense(1, activation = tf.nn.sigmoid)
super(PNN_Inner, self).build(input_shape)
def call(self, x, training = None):
#x:
#w:
linear = []
for i in range(self.D1):
# ->
w = self.Wz[:, :, i]
# * ->
temp = tf.multiply(x, w)
# ->
temp = tf.expand_dims(tf.reduce_mean(temp, axis = ), axis = 1)
linear.append(temp)
#linear:
linear = tf.concat(linear, axis = 1)
product = []
# ->
fi = tf.reduce_mean(x, axis = 1)
# !* ->
p = tf.einsum('ai,aj->aij', fi, fi)
for i in range(self.D1):
# ->
theta = tf.expand_dims(self.theta, axis =0)
# @ ->
w = tf.matmul(tf.transpose(theta), theta)
# * ->
temp = tf.matmul(p, w)
# ->
temp = tf.expand_dims(tf.reduce_mean(temp, axis = ), axis = 1)
product.append(temp)
#product:
product = tf.concat(product, axis = 1)
# ->
out = tf.concat(,axis = 1)
out = self.drop1(self.bn1(self.l1(out), training), training)
out = self.drop2(self.bn2(self.l2(out), training), training)
out = self.l3(out)
return out
model = PNN_Inner(D1 = 20)
model.build((None,60, 30))
model.summary() 论文链接:https://arxiv.org/pdf/1611.00144v1.pdf
页:
[1]