python 在Keras中重塑卷积的输入数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在Keras中重塑卷积的输入数据相关的知识,希望对你有一定的参考价值。

# In Keras the Convolution layer requirest an additional dimension which will be used for the various filter.
# When we have eg. 2D dataset the shape is (data_points, rows, cols).
# But Convolution2D requires shape (data_points, rows, cols, 1).
# Otherwise it fails with eg. "Exception: Input 0 is incompatible with layer convolution2d_5: expected ndim=4, found ndim=3"
#
# Originally I reshaped the data beforehand but it only complicates things.
#
# An easier and more elegant solution is to add a Reshape layer at the input
# of the network!
#
# Docs: https://keras.io/layers/core/#reshape

from keras.models import Sequential, Model
from keras.layers import Input
from keras.layers.core import Activation, Reshape
from keras.layers.convolutional import Convolution2D

# eg. 100x100 px images
input_shape = (100, 100)

def create_model_sequential(input_shape):
  """For the classic sequential API..."""
  model = Sequential()

  # add one more dimension for convolution
  model.add(Reshape(input_shape + (1, ), input_shape=input_shape))

  model.add(Convolution2D(32, 3, 3))
  model.add(Activation('relu'))
  # ...

  return model

def create_model_functional(input_shape):
  """For the new functional API..."""
  inputs = Input(input_shape)

  # add one more dimension for convolution
  x = Reshape(input_shape + (1, ), input_shape=input_shape)(inputs)

  x = Convolution2D(32, 3, 3)(x)
  x = Activation('relu')(x)
  # ...

  return Model(inputs, x)

以上是关于python 在Keras中重塑卷积的输入数据的主要内容,如果未能解决你的问题,请参考以下文章

如何重塑我的输入以将其输入一维卷积层以进行序列分类?

Keras Reshape似乎没有运行

如何将 numpy 数组重塑为 3 维以输入到卷积层?

keras中一维卷积网络的输入维度

如何使用 Keras 将一维输入提供给卷积神经网络(CNN)?

如何重塑文本数据以适合 keras 中的 LSTM 模型