ValueError: 权重无法广播到值。值的秩为3,权重的秩为1。值的形状为(?, 7, 7),权重的形状为(?,)。

我正在使用预训练的Densenet网络进行皮肤病变检测和识别工作。然而,我遇到了一个问题,提示说权重无法广播到值。

```%tensorflow_version 1.ximport tensorflow as tffrom tensorflow.keras.applications import densenetfrom tensorflow.keras import layersfrom tensorflow.keras.models import Sequential,Model,load_model from tensorflow.keras.layers import Convolution2Dfrom tensorflow.keras.layers import MaxPooling2Dfrom tensorflow.keras.layers import Flatten,Dense,Activation,Dropoutfrom tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau,ModelCheckpoint,Callbackfrom tensorflow.keras.preprocessing.image import ImageDataGeneratorfrom tensorflow.keras.models import Model image_width,image_height=224,224 #分配高度和宽度 --- Densenet在224*224尺寸上训练 training_samples = 33068 #训练样本数testing_samples = 1103 #验证样本数epochs = 100batch_size = 10 #批次大小设为10 n_classes = 7 #类别数为7。我们有7种皮肤病类别training_folder = '/content/gdrive/My Drive/Deep Learning Projects/skincancer/skin_cancer_classified/training_directory/'testing_folder = '/content/gdrive/My Drive/Deep Learning Projects/skincancer/skin_cancer_classified/testing_directory/'datagen = ImageDataGenerator(    preprocessing_function= \    tf.keras.applications.densenet.preprocess_input)#使用与densenet架构原始rgb图像相同的预处理技术training_batches = datagen.flow_from_directory(training_folder,                                               target_size=(image_height,image_width),                                               batch_size = batch_size)testing_batches = datagen.flow_from_directory(testing_folder,                                              target_size=(image_height,image_width),                                              batch_size = batch_size)network = tf.keras.applications.densenet.DenseNet121()######通过移除网络的最后5层来创建模型架构engineered_network = network.layers[-5].outputengineered_network = Dropout(0.25)(engineered_network)predictions = Dense(7,activation='softmax')(engineered_network) final_model = Model(inputs = network.input, outputs = predictions)for layer in final_model.layers[:-30]:  layer.trainable = Falsefinal_model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=["accuracy"])class_weights = {    0: 1.0,    1: 1.0,    2: 1.0,    3: 1.0,    4: 3.2, #使黑色素瘤更敏感    5: 1.0,    6: 1.0,}network_training = final_model.fit_generator(training_batches,                                             steps_per_epoch=36,                                             class_weight=class_weights,                                             validation_data = testing_batches,                                             validation_steps = 110,                                             epochs=epochs,                                             verbose = 1)```

我不断收到这个错误:

#############################################Epoch 1/100---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-16-6a6e3a644f6a> in <module>()      5                                              validation_steps = 110,      6                                              epochs=epochs,----> 7                                              verbose = 1)11 frames/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/weights_broadcast_ops.py in assert_broadcastable(weights, values)    101             " values.shape=%s. weights.shape=%s." % (    102                 _ASSERT_BROADCASTABLE_ERROR_PREFIX, values_rank_static,--> 103                 weights_rank_static, values.shape, weights.shape))    104       weights_shape_static = tensor_util.constant_value(weights_shape)    105       values_shape_static = tensor_util.constant_value(values_shape)ValueError: 权重无法广播到值。值的秩为3,权重的秩为1。值的形状为(?, 7, 7),权重的形状为(?,)。

我之前没有使用过预训练网络,所以这是我第一次遇到这种情况。任何帮助都将不胜感激。谢谢


回答:

我认为您可能试图直接在卷积层上添加一个全连接层。尝试将预训练模型的输出展平:

######通过移除网络的最后5层来创建模型架构engineered_network = network.layers[-5].outputengineered_network = Dropout(0.25)(engineered_network)engineered_network = Flatten()(engineered_network)predictions = Dense(7,activation='softmax')(engineered_network) 

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注