通过在喀拉拉邦重塑来移除尺寸? [英] Removing dimension using reshape in keras?

查看:93
本文介绍了通过在喀拉拉邦重塑来移除尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to remove a dimension using Reshape or any other function.

我有以下网络。

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)

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

The maxpooling layer creates an output with dimension (15,1,36) which i would like to remove the middle axis, so the output dimension end up being (15,36)..

如果可能,我想避免指定外部尺寸,或者像我尝试过的那样使用先前的图层尺寸来重塑形状。

If possible would I like to avoid specifying the outer dimension, or as i've tried use the prior layer dimension to reshape it.

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

我需要整个网络的输出尺寸为(15,145),其中中间维度引起一些问题。

I need my output dimension for the entire network to be (15,145), in which the middle dimension is causing some problems.

如何删除中间尺寸?

推荐答案

reshape = Reshape((15,145))(merge) # expected output dim: (15,145)

这篇关于通过在喀拉拉邦重塑来移除尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆