检查目标时出错:预期time_distributed_3的形状为(1375,1),但数组的形状为(5511,101) - python

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笔记本)。我无法找到任何解决方案。任何帮助将不胜感激。

代码段@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

python大神给出的解决方案

我认为您的代码应该可以正常工作。我用随机创建的数据制作了一个示例。由于我创建了这个示例来说明可以编译和训练模型而不会出现错误,因此请不要担心准确性和损失。

导入所需的软件包:

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,因为我的目的只是为了证明模型正在运行。

希望能帮助到你 :)

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…

Python:检查新文件是否在文件夹中[重复] - python

This question already has answers here: How do I watch a file for changes? (23个答案) 3年前关闭。 我是python的新手,但是我尝试创建一个自动化过程,其中我的代码将侦听目录中的新文件条目。例如,某人可以手动将zip文件复制到一个特定的文件夹中,并且我希望我的代码能够在文件完全…