简体   繁体   English

keras 输入形状:输入与层不兼容

[英]keras input shape: Input incompatible with the layer

I've looked at a few similar questions but I still don't understand how to solve my problem.我看过一些类似的问题,但我仍然不明白如何解决我的问题。

I am trying to build a CNN that estimates how many particles hit a detector, based on what's essentially an oscilloscope trace of the energy released in the detector over time.我正在尝试构建一个 CNN,它根据探测器随时间释放的能量的示波器轨迹来估计有多少粒子撞击探测器。

I have 100,000 events of 1024 time samples, which I split 80/20 as train/test, like so:我有 1024 个时间样本的 100,000 个事件,我将 80/20 拆分为训练/测试,如下所示:

from sklearn.model_selection import train_test_split
train_to_test_ratio=0.8 #proportion of the dataset to include in the train split

X_train,X_test,Y_train,Y_test=train_test_split(NormSignals,labels,train_size=train_to_test_ratio)

no_outputs = 14 # maximum number of particles expected

# force the labels to have 14 binary digits, one for each of the possible outputs 
Y_train=tf.one_hot(Y_train,no_outputs)
Y_test=tf.one_hot(Y_test,no_outputs)

When I try to define the input shape for the network I do so like this (full CNN code below):当我尝试为网络定义输入形状时,我会这样做(下面的完整 CNN 代码):

# Define input to neural network (tensors of 1024 time samples x 1 amplitude per sample)
inputs = keras.Input(shape=(1024,1))

But it gives me the error: "Input 0 of layer Conv_1 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 1024, 1]"但它给了我错误:“Conv_1 层的输入 0 与该层不兼容:预期 ndim=4,发现 ndim=3。收到完整形状:[None, 1024, 1]”

I thought the input shape was as simple as the shape of the data arrays being passed to the network.我认为输入形状与传递给网络的数据 arrays 的形状一样简单。 Can someone please explain what the correct shape of my data should be?有人可以解释我的数据的正确形状应该是什么吗?

Thank you very much in advance!非常感谢您!

Full CNN:完整的CNN:

from tensorflow import keras

# Following the architecture of the CNN from the image recognition lab (14/5/2020):
# Simple CNN:

class noiseLayer(keras.layers.Layer):

    def __init__(self,mean):
        super(noiseLayer, self).__init__()
        self.mean = mean

    def call(self, input):
        mean = self.mean
        return input + (np.random.poisson(mean))/mean

# Add data augmentation to produce a random flip of the data (the ECal is symmetrical)
# and add poissonian noise to all of the crystals - using large N and dividing by N normalises 
# the noise to be approximately continuous between 0 and 1

data_augmentation = keras.Sequential([
  noiseLayer(mean = 1000)
], name='DataAugm')

# Define input to neural network (tensors of 1024 time samples x 1 amplitude per sample)
inputs = keras.Input(shape=(1024,1))

#x=inputs
x = data_augmentation(inputs)

# primo blocco Convoluzionale

x = keras.layers.Conv2D(16, kernel_size=(3,3), name='Conv_1')(x)
x = keras.layers.LeakyReLU(0.1)(x)      
x = keras.layers.MaxPool2D((2,2), name='MaxPool_1')(x)

# secondo blocco Convoluzionale
x = keras.layers.Conv2D(16, kernel_size=(3,3), name='Conv_2')(x)
x = keras.layers.LeakyReLU(0.1)(x)
x = keras.layers.MaxPool2D((2,2), name='MaxPool_2')(x)

# terzo blocco convoluzionale 
x = keras.layers.Conv2D(32, kernel_size=(3,3), name='Conv_3')(x)
x = keras.layers.LeakyReLU(0.1)(x)
x = keras.layers.MaxPool2D((2,2), name='MaxPool_3')(x)

# Flatten output tensor of the last convolutional layer so it can be used as  
# input to the dense layers

x = keras.layers.Flatten(name='Flatten')(x)

# dense network: 2 dense hidden layer with 256 neurons, with ReLU activation

# Classifier
x = keras.layers.Dense(64, name='Dense_1')(x)
x = keras.layers.ReLU(name='ReLU_dense_1')(x)
#x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(64, name='Dense_2')(x)
x = keras.layers.ReLU(name='ReLU_dense_2')(x)

outputs = keras.layers.Dense(no_outputs, activation='softmax', name='Output')(x)

# Model definition
model = keras.Model(inputs=inputs, outputs=outputs, name='VGGlike_CNN')

# Print model summary
model.summary()

# Show model structure
keras.utils.plot_model(model, show_shapes=True)

The problem was that I was using 2D layers to try to solve a 1D problem.问题是我正在使用 2D 图层来尝试解决 1D 问题。

Changing all the 2D layers to 1D now compiles without errors:现在将所有 2D 图层更改为 1D 编译不会出错:


x = keras.layers.Conv1D(16, kernel_size=(3), name='Conv_1')(x)
x = keras.layers.LeakyReLU(0.1)(x)      
x = keras.layers.MaxPool1D((2), name='MaxPool_1')(x)

# secondo blocco Convoluzionale
x = keras.layers.Conv1D(16, kernel_size=(3), name='Conv_2')(x)
x = keras.layers.LeakyReLU(0.1)(x)
x = keras.layers.MaxPool1D((2), name='MaxPool_2')(x)

# terzo blocco convoluzionale 
x = keras.layers.Conv1D(32, kernel_size=(3), name='Conv_3')(x)
x = keras.layers.LeakyReLU(0.1)(x)
x = keras.layers.MaxPool1D((2), name='MaxPool_3')(x)

# Flatten output tensor of the last convolutional layer so it can be used as  
# input to the dense layers

x = keras.layers.Flatten(name='Flatten')(x)

# dense network: 2 dense hidden layer with 256 neurons, with ReLU activation

# Classifier
x = keras.layers.Dense(64, name='Dense_1')(x)
x = keras.layers.ReLU(name='ReLU_dense_1')(x)
#x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(64, name='Dense_2')(x)
x = keras.layers.ReLU(name='ReLU_dense_2')(x)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras-CNN输入形状不兼容 - Keras - CNN input shape incompatible Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) Keras:层顺序的输入 0 与层不兼容 - Keras: Input 0 of layer sequential is incompatible with the layer 自动编码器/Keras/Tensorflow:密集层与层不兼容:输入形状的预期轴 -1 的值为 64 - Autoencoder/Keras/Tensorflow: Dense layer is incompatible with the layer: expected axis -1 of input shape to have value 64 输入密集与图层无效形状不兼容 - Input dense is incompatible with the layer invalid shape Tensorflow / Keras ValueError:层“模型”的输入 0 与层不兼容:预期形状=(无,224,224,3),发现形状=(32,224,3) - Tensorflow / Keras ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3) Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 Python Keras model 输入与层不兼容 - Python Keras model input incompatible with layer Keras中的Flatten()层具有可变的输入形状 - Flatten() Layer in Keras with variable input shape Keras 中嵌入层的输入形状出现问题 - Trouble with input shape of Embedding layer in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM