keras:对于小批量中的每个样本,具有不同滤波器的一维卷积

zhf061

通常,我们使用一组相同的滤波器对微型批处理的样本进行一维卷积运算。但是现在我想对微型批次中的每个样本使用不同的过滤器。我有什么办法可以在喀拉拉邦做到这一点,特别是如果不知道迷你批次的大小?

具体来说,我的输入数据的形状为(batch_size, maxlen, input_dim),并且生成了一组形式为的过滤器(batch_size, output_dim, kernel_size, input_dim)有什么想法可以将输入与过滤器集进行卷积吗?

丹尼尔·莫勒

这是非常棘手的问题,我们将获得帮助K.depthwise_conv2d(唯一一个单独处理通道的卷积),在该过程中,我们会将样本转换为通道,每个通道具有所需的输出,然后重塑为预期的形状

因此,想法是:

1-变换输入形状(适当重新排序)

#from `(batch_size, maxlen, input_dim)` 
#to `(1, maxlen, input_dim, batch_size)`   
x = K.expand_dims(x, axis=0)
x = K.permute_dimensions(x, (0,2,3,1))

2-具有形状的滤镜 (kernel_size, input_dim, batch_size, output_dim)

#transform your kernels:
filters = K.permute_dimensions(filters, (2, 3, 0, 1))

3转换结果(正确排序):

#from `(1, result_len, 1, batch_size * output_dim)` 
#to `(batch_size, result_len, output_dim)`   
results = K.reshape(results, (output_length, -1, output_dim) #-1 for batch size
results = K.permute_dimensions(results, (1,0,2))

这是一个示例代码:

from keras.layers import *
import keras.backend as K
from keras.models import Model

#dimensions
length = 11 #maxlen
features = 3 #input_dim
filtersize = 2 #kernel_size
out_dim = 5 #output_dim
samples = 7 #batch_size

#keep track of output length for reshaping
outlen = length - filtersize + 1

#creating dummy filters with the desired shape
npfilters = np.arange(features*filtersize*out_dim*samples)
npfilters = npfilters.astype(np.float64)
npfilters = npfilters.reshape((filtersize, features, samples, out_dim))
kerasfilters = K.variable(npfilters)

#function that performs the convolution
def sample_wise_conv1d(x):

    #reshape and reorder inputs properly
    x= K.expand_dims(x, axis=0)
    x = K.permute_dimensions(x, (0,2,3,1))
    print('in shape', K.int_shape(x))

    #perform the convolution
    print("filter shape", K.int_shape(kerasfilters))
    results =  K.depthwise_conv2d(x, kerasfilters)
    print('out shape', K.int_shape(results))

    #reshape and reorder the results properly
    results = K.reshape(results, (outlen, samples, out_dim))
    results = K.permute_dimensions(results, (1,0,2))
    print('final shape', K.int_shape(results))

    return results


#creating a model that performs the operation
inputs = Input((length, features))
outputs = Lambda(sample_wise_conv1d)(inputs)

model = Model(inputs, outputs)

#predicting from the model
inputdata = np.arange(samples*length*features).reshape((samples, length, features))
results = model.predict(inputdata)

print(results.shape)

测试代码

#creating a single conv1D model for each sample
for i in range(samples):

    #get the respective input sample and filter
    x = inputdata[i:i+1]
    filts = npfilters[:,:,i,:]
    print(filts.shape)

    #make a model with conv1d
    in1D = Input((length, features))
    out1D = Lambda(lambda x: K.conv1d(x, K.variable(filts)))(in1D)
    model1D = Model(in1D, out1D)

    #compare this model's predictions with the respective prediction from the function
    pred1D = model1D.predict(x)
    pred2D = results[i]

    print(pred1D == pred2D)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Tensorflow:对于小批量中的每个样本使用不同的滤波器进行卷积

具有多个滤波器的卷积层的输出形状

RL 中的小批量

在深度学习中,两个 3*3 卷积滤波器和一个 5*5 卷积滤波器有什么区别?

在深度学习中在卷积网络中使用同一滤波器的倍数有什么优势

在Python中使用有限滤波器和Dirac增量求和进行快速一维卷积

了解卷积滤波器

如何在Python中将3维数组(在本例中为滤波器组)与2维图像(单色)进行卷积?

Tensorflow中是否有卷积函数来应用Sobel滤波器?

卷积网络中的最佳滤波器数量

减少卷积神经网络中的滤波器大小

如何在 tensorflow 中训练卷积滤波器

C中的Gabor滤波器卷积矩阵

在 Keras 中使用连接层进行小批量学习?

在小批量内更改学习率 - keras

在Keras中实现多输入模型,每个模型具有不同的样本大小(每个批次具有不同的大小)

Tensorflow小批量培训

如何在 Tensorflow 中为一维信号设置步幅、滤波器大小?

一维阵列像素的高斯滤波器

卡尔曼滤波器(一维):几种方法?

Node.js 卡尔曼滤波器一维

卷积图像滤波器的实现公式

卷积滤波器的参数数量

使用pyro的深度生成模型中的小批量概念

随机梯度下降和Q学习中的小批量

如何从熊猫数据框中制作小批量数据?

如何在python中实现小批量梯度下降?

为什么卷积滤波器在卷积神经网络中翻转?

如何理解Keras 1D卷积输入形状和滤波器