我是Tensorflow的新手.我正在尝试在1D卷积层之后添加最大池池:
import tensorflow as tf
import math
sess = tf.InteractiveSession()
length=458
# These will be inputs
## Input pixels, image with one channel (gray)
x = tf.placeholder("float", [None, length])
# Note that -1 is for reshaping
x_im = tf.reshape(x, [-1,length,1])
## Known labels
# None works during variable creation to be
# unspecified size
y_ = tf.placeholder("float", [None,2])
# Conv layer 1
num_filters1 = 2
winx1 = 3
W1 = tf.Variable(tf.truncated_normal(
[winx1, 1 , num_filters1],
stddev=1./math.sqrt(winx1)))
b1 = tf.Variable(tf.constant(0.1,
shape=[num_filters1])) …Run Code Online (Sandbox Code Playgroud) python machine-learning conv-neural-network tensorflow max-pooling
如何在 PyTorch 中执行求和池。具体来说,如果我们有输入(N, C, W_in, H_in)并想要(N, C, W_out, H_out)使用特定的输出kernel_size,stride就像nn.Maxpool2d?
我想定义我的自定义池化层,而不是像 MaxPooling 层那样返回最大值,它会输出 k 个最大值和 k 个最小值。
我使用 Tensorflow 作为后端。我需要对输出向量进行排序。
我正在考虑这样做:
from keras.layers.pooling import _Pooling1D
class MinMaxPooling1D(_Pooling1D):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MinMaxPooling1D, self).__init__(**kwargs)
def _pooling_function(self, inputs, **kwargs):
sorted_ = tf.contrib.framework.sort(inputs, axis = -1)
print(sorted_)
return np.concatenate((sorted_[:,:,:self.output_dim/2], sorted_[:,:,-self.output_dim/2:]))
Run Code Online (Sandbox Code Playgroud)
但后来我得到:
Tensor("min_max_pooling1d_1/sort/Neg_1:0", shape=(?, 1, 1, ?), dtype=float32)
ValueError: zero-dimensional arrays cannot be concatenated
Run Code Online (Sandbox Code Playgroud)
MinMaxPooling1D 层应用于 (None, 1, 10) 形状输出。
我当时正在考虑在 MinMaxPooling1D 之前添加一个 Flatten 层,但随后有一个维度问题:
ValueError: Input 0 is incompatible with layer min_max_pooling1d_5: expected ndim=3, found ndim=2
Run Code Online (Sandbox Code Playgroud) 我试图用Pytorch建立一个cnn,并且难以进行最大化.我采取了斯坦福大学持有的cs231n.正如我记得的那样,maxpooling可以用作维度推导步骤,例如,我有这个(1,20,高度,宽度)输入ot max_pool2d(假设我的batch_size是1).如果我使用(1,1)内核,我想得到这样的输出:(1,1,高度,宽度),这意味着内核应该在通道维度上滑动.但是,在检查了pytorch文档之后,它说内核在高度和宽度上滑动.感谢Pytorch论坛上的@ImgPrcSng告诉我使用max_pool3d,结果表明效果很好.但是在conv2d层的输出和max_pool3d层的输入之间仍然存在重塑操作.所以很难被聚合到一个nn.Sequential,所以我想知道还有另一种方法吗?