在 keras 中使用 reshape 去除尺寸?

J.D*_*own 5 dimensions reshape python-2.7 keras

是否可以使用 Reshape 或任何其他功能删除尺寸。

我有以下网络。

import keras
from keras.layers.merge import Concatenate
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import Dropout
from keras.layers.core import Dense, Activation, Lambda, Reshape,Flatten
from keras.layers import Conv2D, MaxPooling2D, Reshape, ZeroPadding2D
import numpy as np


#Number_of_splits = ((input_width-win_dim)+1)/stride_dim
splits = ((40-5)+1)/1
print splits


train_data_1 = np.random.randint(100,size=(100,splits,45,5,3))
test_data_1 = np.random.randint(100,size=(10,splits,45,5,3))
labels_train_data =np.random.randint(145,size=(100,15))
labels_test_data =np.random.randint(145,size=(10,15))


list_of_input = [Input(shape = (45,5,3)) for i in range(splits)]
list_of_conv_output = []
list_of_max_out = []
for i in range(splits):
    list_of_conv_output.append(Conv2D(filters = 145 , kernel_size = (15,3))(list_of_input[i])) #output dim: 36x(31,3,145)
    list_of_max_out.append((MaxPooling2D(pool_size=(2,2))(list_of_conv_output[i]))) #output dim: 36x(15,1,145)


merge = keras.layers.concatenate(list_of_max_out) #Output dim: (15,1,5220)
#reshape = Reshape((merge.shape[0],merge.shape[3]))(merge) # expected output dim: (15,145)


dense1 = Dense(units = 1000, activation = 'relu',    name = "dense_1")(merge)
dense2 = Dense(units = 1000, activation = 'relu',    name = "dense_2")(dense1)
dense3 = Dense(units = 145 , activation = 'softmax', name = "dense_3")(dense2)






model = Model(inputs = list_of_input , outputs = dense3)
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam")


print model.summary()


raw_input("SDasd")
hist_current = model.fit(x = [train_input[i] for i in range(100)],
                    y = labels_train_data,
                    shuffle=False,
                    validation_data=([test_input[i] for i in range(10)], labels_test_data),
                    validation_split=0.1,
                    epochs=150000,
                    batch_size = 15,
                    verbose=1)
Run Code Online (Sandbox Code Playgroud)

maxpooling 层创建了一个维度为 (15,1,36) 的输出,我想删除中间轴,因此输出维度最终为 (15,36)。

如果可能的话,我想避免指定外部维度,或者因为我已经尝试使用前一层维度来重塑它。

#reshape = Reshape((merge.shape[0],merge.shape[3]))(merge) # expected output dim: (15,145)
Run Code Online (Sandbox Code Playgroud)

我需要整个网络的输出维度为 (15,145),其中中间维度导致了一些问题。

我如何删除中间尺寸?

jer*_*ile 7

我想删除所有等于 1 的维度,但不指定特定大小,Reshape以便在更改卷积中的输入大小或内核数时我的代码不会中断。这适用于 tensorflow 后端的功能性 keras API。

from keras.layers.core import Reshape

old_layer = Conv2D(#actualArguments) (older_layer)
#old_layer yields, e.g., a (None, 15,1,36) size tensor, where None is the batch size

newdim = tuple([x for x in old_layer.shape.as_list() if x != 1 and x is not None])
#newdim is now (15, 36). Reshape does not take batch size as an input dimension.
reshape_layer = Reshape(newdim) (old_layer)
Run Code Online (Sandbox Code Playgroud)

  • @Maciej Jureczko 正如我在回答中所说,它允许删除具有未知维度大小的 1 大小维度,而不必从前一层找出输出张量的大小,这会随着您更改模型的参数和输入大小。先前的答案意味着未来对模型的调整更加困难。 (2认同)

J.D*_*own 3

reshape = Reshape((15,145))(merge) # expected output dim: (15,145)
Run Code Online (Sandbox Code Playgroud)