Tensorflow Keras TF可学习划分/任意操作层
我正在搜索一个对输入执行元素分割的层,但是当然,必须学习该分割的参数,就像标准conv2D层的参数一样 我发现: 但我不认为这是我想要的,因为我希望分割参数是学习的,而不是分割两层Tensorflow Keras TF可学习划分/任意操作层,tensorflow,keras,Tensorflow,Keras,我正在搜索一个对输入执行元素分割的层,但是当然,必须学习该分割的参数,就像标准conv2D层的参数一样 我发现: 但我不认为这是我想要的,因为我希望分割参数是学习的,而不是分割两层 对于密集层,计算点积,这不是我想要的。我正在寻找元素级乘法/除法。自定义层的示例代码,该层使用训练期间学习的该除法的参数(权重)对输入执行元素级除法,如下所示: %tensorflow_version 2.x from tensorflow import keras from tensorflow.keras i
对于密集层,计算点积,这不是我想要的。我正在寻找元素级乘法/除法。自定义层的示例代码,该层使用训练期间学习的该除法的
参数(权重)
对输入执行元素级除法,如下所示:
%tensorflow_version 2.x
from tensorflow import keras
from tensorflow.keras import Input
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Layer
import numpy as np
class MyLayer(Layer):
def __init__(self, output_dims, **kwargs):
self.output_dims = output_dims
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=self.output_dims,
initializer='ones',
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this somewhere!
def call(self, x):
# Dividing Input with Weights
return tf.divide(x, self.kernel)
def compute_output_shape(self, input_shape):
return (self.output_dims)
mInput = np.array([[1,2,3,4]])
inShape = (4,)
net = Sequential()
outShape = (4,)
l1 = MyLayer(outShape, input_shape= inShape)
net.add(l1)
net.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accuracy'])
p = net.predict(x=mInput, batch_size=1)
print(p)
希望这有帮助。学习愉快 这实际上与无偏差的致密层相同。无需明确建模除法,因为除法相当于乘法(x/2:==x*0.5),这是神经网络层默认情况下发生的。不,我认为你犯了一个错误,默认情况下,它是点积:w^T x=w1x1+w2x2+…+wnxn我不想求和!我想要元素级的乘法/除法。。。你觉得怎么样?如果你有想法,我会很感兴趣。有人有想法吗?