我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用keras.layers.AveragePooling1D()。
def ResidualBlock1D_helper(layers, kernel_size, filters, final_stride=1): def f(_input): basic = _input for ln in range(layers): #basic = BatchNormalization()( basic ) # triggers known keras bug w/ TimeDistributed: https://github.com/fchollet/keras/issues/5221 basic = ELU()(basic) basic = Conv1D(filters, kernel_size, kernel_initializer='he_normal', kernel_regularizer=l2(1.e-4), padding='same')(basic) # note that this strides without averaging return AveragePooling1D(pool_size=1, strides=final_stride)(Add()([_input, basic])) return f
def test_delete_channels_averagepooling1d(channel_index): layer = AveragePooling1D(3) layer_test_helper_flatten_1d(layer, channel_index)
def test_average_pooling_1d(self): np.random.seed(1988) input_dim = 2 input_length = 10 filter_length = 3 nb_filters = 4 model = Sequential() model.add(Conv1D(nb_filters, kernel_size = filter_length, padding='same', input_shape=(input_length, input_dim))) model.add(AveragePooling1D(pool_size=2)) self._test_keras_model(model)
def test_conv1d_flatten(self): model = Sequential() model.add(AveragePooling1D(2,input_shape=(64,9))) model.add(Conv1D(16, 1, padding='same', activation='relu', use_bias=False)) model.add(MaxPooling1D(2)) model.add(Flatten()) model.add(Dense(units=7, activation='softmax', use_bias=False)) model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()]) self._test_keras_model(model)
def test_imdb_fasttext_first_2(self): max_features = 10 max_len = 6 embedding_dims = 4 pool_length = 2 model = Sequential() model.add(Embedding(max_features, embedding_dims, input_length=max_len)) # we add a AveragePooling1D, which will average the embeddings # of all words in the document model.add(AveragePooling1D(pool_size=pool_length)) self._test_keras_model(model, one_dim_seq_flags=[True])
def __init__(self,samples=1000): #??Model input_1=Input(shape=(20,200)) x=Convolution1D(400,3,border_mode='same',input_shape=(20,200))(input_1) #x=Dropout(0.5)(x) x=AveragePooling1D(20)(x) x=Dropout(0.25)(x) x=Flatten()(x) output_1=Dense(128,activation="tanh")(x) self.model_1=Model(input=input_1,output=output_1) self.model_1.compile(optimizer="sgd",loss="mse",metrics=['accuracy']) #????Model input_2=Input(shape=(1000,)) x=Dense(400,activation="tanh")(input_2) x=Dropout(0.25)(x) output_2=Dense(128,activation="tanh")(x) #output_2=Lambda(lambda x:x*(-1))(output_2) self.model_2=Model(input=input_2,output=output_2) self.model_2.compile(optimizer="sgd",loss="mse",metrics=['accuracy']) #?????????????? input_2_a=Input(shape=(1000,)) input_2_b=Input(shape=(1000,)) input_2_c=Input(shape=(1000,)) input_2_d=Input(shape=(1000,)) input_2_e=Input(shape=(1000,)) input_2_f=Input(shape=(1000,)) output_2_a=self.model_2(input_2_a) output_2_b=self.model_2(input_2_b) output_2_c=self.model_2(input_2_c) output_2_d=self.model_2(input_2_d) output_2_e=self.model_2(input_2_e) output_2_f=self.model_2(input_2_f) #??,?? output=merge(inputs=[output_1,output_2_a,output_2_b,output_2_c,output_2_d,output_2_e,output_2_f],mode=cosine_error,output_shape=(None,1)) #???? self.model=Model([input_1,input_2_a,input_2_b,input_2_c,input_2_d,input_2_e,input_2_f],output=output) self.model.compile(optimizer="sgd",loss='mse',metrics=['accuracy']) #???? #rand=np.random self.X_train_1=load_data(samples=samples) #question self.X_train_2_a=load_predicate_data(samples=samples) #??? #self.X_train_2_b=np.array([self.X_train_2_a[rand.randint(999)] for i in range(1000)]) #??? #self.X_train_2_c=np.array([self.X_train_2_a[rand.randint(999)] for i in range(1000)]) #???
def pooling(layer, layer_in, layerId): poolMap = { ('1D', 'MAX'): MaxPooling1D, ('2D', 'MAX'): MaxPooling2D, ('3D', 'MAX'): MaxPooling3D, ('1D', 'AVE'): AveragePooling1D, ('2D', 'AVE'): AveragePooling2D, ('3D', 'AVE'): AveragePooling3D, } out = {} layer_type = layer['params']['layer_type'] pool_type = layer['params']['pool'] padding = get_padding(layer) if (layer_type == '1D'): strides = layer['params']['stride_w'] kernel = layer['params']['kernel_w'] if (padding == 'custom'): p_w = layer['params']['pad_w'] out[layerId + 'Pad'] = ZeroPadding1D(padding=p_w)(*layer_in) padding = 'valid' layer_in = [out[layerId + 'Pad']] elif (layer_type == '2D'): strides = (layer['params']['stride_h'], layer['params']['stride_w']) kernel = (layer['params']['kernel_h'], layer['params']['kernel_w']) if (padding == 'custom'): p_h, p_w = layer['params']['pad_h'], layer['params']['pad_w'] out[layerId + 'Pad'] = ZeroPadding2D(padding=(p_h, p_w))(*layer_in) padding = 'valid' layer_in = [out[layerId + 'Pad']] else: strides = (layer['params']['stride_h'], layer['params']['stride_w'], layer['params']['stride_d']) kernel = (layer['params']['kernel_h'], layer['params']['kernel_w'], layer['params']['kernel_d']) if (padding == 'custom'): p_h, p_w, p_d = layer['params']['pad_h'], layer['params']['pad_w'],\ layer['params']['pad_d'] out[layerId + 'Pad'] = ZeroPadding3D(padding=(p_h, p_w, p_d))(*layer_in) padding = 'valid' layer_in = [out[layerId + 'Pad']] out[layerId] = poolMap[(layer_type, pool_type)](pool_size=kernel, strides=strides, padding=padding)( *layer_in) return out # ********** Locally-connected Layers **********