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
答案 0 :(得分: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
,因为我的目的只是为了证明该模型正在运行。
希望它会有所帮助:)