簡體   English   中英

keras 輸入形狀:輸入與層不兼容

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

我看過一些類似的問題,但我仍然不明白如何解決我的問題。

我正在嘗試構建一個 CNN,它根據探測器隨時間釋放的能量的示波器軌跡來估計有多少粒子撞擊探測器。

我有 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)

當我嘗試為網絡定義輸入形狀時,我會這樣做(下面的完整 CNN 代碼):

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

但它給了我錯誤:“Conv_1 層的輸入 0 與該層不兼容:預期 ndim=4,發現 ndim=3。收到完整形狀:[None, 1024, 1]”

我認為輸入形狀與傳遞給網絡的數據 arrays 的形狀一樣簡單。 有人可以解釋我的數據的正確形狀應該是什么嗎?

非常感謝您!

完整的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)

問題是我正在使用 2D 圖層來嘗試解決 1D 問題。

現在將所有 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM