1
Layer (type)                 Output Shape              Param #   
=================================================================
input_13 (InputLayer)        (None, 5511, 101)         0         
_________________________________________________________________
conv1d_13 (Conv1D)           (None, 1375, 196)         297136    
_________________________________________________________________
batch_normalization_27 (Batc (None, 1375, 196)         784       
_________________________________________________________________
activation_13 (Activation)   (None, 1375, 196)         0         
_________________________________________________________________
dropout_34 (Dropout)         (None, 1375, 196)         0         
_________________________________________________________________
gru_18 (GRU)                 (None, 1375, 128)         124800    
_________________________________________________________________
dropout_35 (Dropout)         (None, 1375, 128)         0         
_________________________________________________________________
batch_normalization_28 (Batc (None, 1375, 128)         512       
_________________________________________________________________
gru_19 (GRU)                 (None, 1375, 128)         98688     
_________________________________________________________________
dropout_36 (Dropout)         (None, 1375, 128)         0         
_________________________________________________________________
batch_normalization_29 (Batc (None, 1375, 128)         512       
_________________________________________________________________
dropout_37 (Dropout)         (None, 1375, 128)         0         
_________________________________________________________________
time_distributed_11 (TimeDis (None, 1375, 1)           129       
=================================================================

Total params: 522,561
Trainable params: 521,657
Non-trainable params: 904

ValueError: Error when checking target: expected time_distributed_3 to have shape (1375, 1) but got array with shape (5511, 101)

我将 .npy 文件作为 cnn 层的输入。数组大小为 (5, 5511, 101) 输入数组有问题吗?如何克服该值错误。我正在使用 keras(jupyter notebook)。我找不到任何解决方案。任何帮助将不胜感激。

代码片段@ErselEr ...这是我用来构建模型的代码

def model(input_shape):

    X_input = Input(shape = input_shape)
    y = Input(shape = input_shape)
    ### START CODE HERE ###

    # Step 1: CONV layer (≈4 lines)
    X = Conv1D(196, kernel_size=15, strides=4)(X_input)
    X = BatchNormalization()(X)                                 # Batch normalization
    X = Activation('relu')(X)   # ReLu activation
    X = 
    X = Dropout(0.8)(X) # dropout (use 0.8)

    # Step 2: First GRU Layer (≈4 lines)
    X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)
    X = Dropout(0.8)(X)                                 # dropout (use 0.8)
    X = BatchNormalization()(X)  # Batch normalization



    # Step 3: Second GRU Layer (≈4 lines)
    X = GRU(units = 128, return_sequences = True)(X)   # GRU (use 128 units and return the sequences)
    X = Dropout(0.8)(X)                                 # dropout (use 0.8)
    X = BatchNormalization()(X)                                  # Batch normalization
       # dropout (use 0.8)



    # Step 4: Time-distributed dense layer (≈1 line)
    X = TimeDistributed(Dense(1,activation = "sigmoid"))(X) # time distributed  (sigmoid)


    ### END CODE HERE ###

    model = Model(inputs=X_input, outputs=X)

    return model
4

1 回答 1

0

我认为您的代码应该可以正常工作。我用随机创建的数据编造了一个例子。不要关心准确性和损失,因为我创建了这个例子来表明可以编译和训练模型而不会出错。

导入所需的包:

from keras.layers import Input, Conv1D, Activation, BatchNormalization, TimeDistributed, Dense, Dropout, GRU
from keras.models import Model
from keras.optimizers import Adam
import numpy as np

然后使用模型函数(我在步骤1中只删除了一行代码):

def model(input_shape):

    X_input = Input(shape = input_shape)
    y = Input(shape = input_shape)
    ### START CODE HERE ###

    # Step 1: CONV layer (≈4 lines)
    X = Conv1D(196, kernel_size=15, strides=4)(X_input)
    X = BatchNormalization()(X)                                 # Batch normalization
    X = Activation('relu')(X)   # ReLu activation
    X = Dropout(0.8)(X) # dropout (use 0.8)

    # Step 2: First GRU Layer (≈4 lines)
    X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)
    X = Dropout(0.8)(X)                                 # dropout (use 0.8)
    X = BatchNormalization()(X)  # Batch normalization



    # Step 3: Second GRU Layer (≈4 lines)
    X = GRU(units = 128, return_sequences = True)(X)   # GRU (use 128 units and return the sequences)
    X = Dropout(0.8)(X)                                 # dropout (use 0.8)
    X = BatchNormalization()(X)                                  # Batch normalization
       # dropout (use 0.8)



    # Step 4: Time-distributed dense layer (≈1 line)
    X = TimeDistributed(Dense(1,activation = "sigmoid"))(X) # time distributed  (sigmoid)


    ### END CODE HERE ###

    model = Model(inputs=X_input, outputs=X)

    return model

创建模型:

input_shape = (5511, 101)
m = model(input_shape)
m.summary()

输出:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_27 (InputLayer)        (None, 5511, 101)         0         
_________________________________________________________________
conv1d_12 (Conv1D)           (None, 1375, 196)         297136    
_________________________________________________________________
batch_normalization_14 (Batc (None, 1375, 196)         784       
_________________________________________________________________
activation_6 (Activation)    (None, 1375, 196)         0         
_________________________________________________________________
dropout_13 (Dropout)         (None, 1375, 196)         0         
_________________________________________________________________
gru_9 (GRU)                  (None, 1375, 128)         124800    
_________________________________________________________________
dropout_14 (Dropout)         (None, 1375, 128)         0         
_________________________________________________________________
batch_normalization_15 (Batc (None, 1375, 128)         512       
_________________________________________________________________
gru_10 (GRU)                 (None, 1375, 128)         98688     
_________________________________________________________________
dropout_15 (Dropout)         (None, 1375, 128)         0         
_________________________________________________________________
batch_normalization_16 (Batc (None, 1375, 128)         512       
_________________________________________________________________
time_distributed_5 (TimeDist (None, 1375, 1)           129       
=================================================================
Total params: 522,561
Trainable params: 521,657
Non-trainable params: 904

使用所需参数编译模型:

# initiate Adam optimizer
opt = Adam(lr=0.0001)

# Let's train the model using Adam
m.compile(loss='binary_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

创建包含 100 个实例的训练(虚构)数据来测试模型:

x_train = np.random.rand(100, 5511, 101)
y_train = np.random.rand(100, 1375, 1)

最后将数据拟合到模型进行训练:

results = m.fit(
 x_train, y_train,
 epochs= 2,
 batch_size = 10,
 validation_data = (x_train, y_train)
)

输出:

Train on 100 samples, validate on 100 samples
Epoch 1/2
100/100 [==============================] - 16s 157ms/step - loss: 0.9138 - acc: 0.0000e+00 - val_loss: 0.7009 - val_acc: 0.0000e+00
Epoch 2/2
100/100 [==============================] - 13s 130ms/step - loss: 0.9135 - acc: 0.0000e+00 - val_loss: 0.7006 - val_acc: 0.0000e+00

正如我之前所说,我在没有任何关于你训练目标的信息的情况下编造了binary_crossentropy这个例子,因此不要关心损失等。我只是盲目地使用,因为我的目的只是为了表明模型正在工作。

希望能帮助到你 :)

于 2018-12-19T12:31:48.960 回答