Python ValueError:检查目标时出错:预期密集_14有2维,但得到了具有形状的数组(144、11、1756)
我正在运行一个CNN,检查图像,但不分类。事实上,输出层是一个密集层,其参数为1d中标签中图像的大小 如下面代码所示,我使用的是model.fit_generator()而不是model.fit,当开始训练模型时,出现以下错误: “ValueError:检查目标时出错:预期密集_14有2个 维度,但获得了具有形状的数组(144、11、1756)” 这里附上了模型的摘要:Python ValueError:检查目标时出错:预期密集_14有2维,但得到了具有形状的数组(144、11、1756),python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我正在运行一个CNN,检查图像,但不分类。事实上,输出层是一个密集层,其参数为1d中标签中图像的大小 如下面代码所示,我使用的是model.fit_generator()而不是model.fit,当开始训练模型时,出现以下错误: “ValueError:检查目标时出错:预期密集_14有2个 维度,但获得了具有形状的数组(144、11、1756)” 这里附上了模型的摘要: def generator(data_arr, batch_size = 12): num = len(data_arr)
def generator(data_arr, batch_size = 12):
num = len(data_arr)
# Loop forever so the generator never terminates
while True:
#shuffle(data_arr)
i = 0
for offset in range(0, num, batch_size):
batch_samples = data_arr[offset:offset+batch_size]
samples = []
labels = []
for i in range(0, offset+batch_size, 1):
for batch_sample in batch_samples:
samples.append(data_arr[i][0])
labels.append(data_arr[i][1])
X_ = np.array(samples)
Y_ = np.array(labels)
i+= batch_size
X_ = X_[:, :, :, newaxis]
yield (X_, Y_)
# compile and train the model using the generator function
train_generator = generator(training_data, batch_size = 12)
validation_generator = generator(val_data, batch_size = 12)
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
model = Sequential()
model.add(Conv2D(8, (3, 3), input_shape = (55,1756,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(16, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Activation('softmax'))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature
vectors
model.add(Dense(19316))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'],
options = run_opts)
model.summary()
batch_size = 12
nb_epoch = 6
model.fit_generator(train_generator,
steps_per_epoch = len(training_data) ,
epochs = nb_epoch,
validation_data = validation_generator,
validation_steps = len(val_data))
图层(类型)输出形状参数#
conv2d_45(conv2d)(无,531754,8)80
激活57(激活)(无,53,1754,8)0
最大池2D_43(最大池(无,17584,8)0
conv2d_46(conv2d)(无、15、582、16)1168
激活(激活)(无、15、582、16)0
最大池2D_44(最大池(无、5194、16)0
conv2d_47(conv2d)(无、3、192、32)4640
激活59(激活)(无,3,192,32)0
最大池2D池45(最大池(无、1、64、32)0
激活(激活)(无,1,64,32)0
展平15(展平)(无,2048)0
稠密的(稠密的)(无,19316)39578484 总参数:39584372 可培训参数:39584372 不可训练参数:0
有什么建议吗? 提前谢谢 代码:
def generator(data_arr, batch_size = 12):
num = len(data_arr)
# Loop forever so the generator never terminates
while True:
#shuffle(data_arr)
i = 0
for offset in range(0, num, batch_size):
batch_samples = data_arr[offset:offset+batch_size]
samples = []
labels = []
for i in range(0, offset+batch_size, 1):
for batch_sample in batch_samples:
samples.append(data_arr[i][0])
labels.append(data_arr[i][1])
X_ = np.array(samples)
Y_ = np.array(labels)
i+= batch_size
X_ = X_[:, :, :, newaxis]
yield (X_, Y_)
# compile and train the model using the generator function
train_generator = generator(training_data, batch_size = 12)
validation_generator = generator(val_data, batch_size = 12)
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
model = Sequential()
model.add(Conv2D(8, (3, 3), input_shape = (55,1756,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(16, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Activation('softmax'))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature
vectors
model.add(Dense(19316))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'],
options = run_opts)
model.summary()
batch_size = 12
nb_epoch = 6
model.fit_generator(train_generator,
steps_per_epoch = len(training_data) ,
epochs = nb_epoch,
validation_data = validation_generator,
validation_steps = len(val_data))