【发布时间】:2019-01-16 05:15:24
【问题描述】:
我正在 Keras 的功能 API(使用 TensorFlow 后端)中训练具有多个输出层的文本情感分类模型。该模型将由 Keras 预处理 API 的 hashing_trick() 函数生成的散列值的 Numpy 数组作为输入,并根据 Keras 规范使用二进制 one-hot 标签的 Numpy 数组的 list 作为其目标用于训练具有多个输出的模型(请参阅此处的 fit() 文档:https://keras.io/models/model/)。
这是模型,没有大部分预处理步骤:
textual_features = hashing_utility(filtered_words) # Numpy array of hashed values(training data)
label_list = [] # Will eventually contain a list of Numpy arrays of binary one-hot labels
for index in range(one_hot_labels.shape[0]):
label_list.append(one_hot_labels[index])
weighted_loss_value = (1/(len(filtered_words))) # Equal weight on each of the output layers' losses
weighted_loss_values = []
for index in range (one_hot_labels.shape[0]):
weighted_loss_values.append(weighted_loss_value)
text_input = Input(shape = (1,))
intermediate_layer = Dense(64, activation = 'relu')(text_input)
hidden_bottleneck_layer = Dense(32, activation = 'relu')(intermediate_layer)
keras.regularizers.l2(0.1)
output_layers = []
for index in range(len(filtered_words)):
output_layers.append(Dense(2, activation = 'sigmoid')(hidden_bottleneck_layer))
model = Model(inputs = text_input, outputs = output_layers)
model.compile(optimizer = 'RMSprop', loss = 'binary_crossentropy', metrics = ['accuracy'], loss_weights = weighted_loss_values)
model.fit(textual_features, label_list, epochs = 50)
这是该模型产生的错误跟踪训练的要点:
ValueError: 检查目标时出错:预期dense_3 的形状为(2,),但得到的数组的形状为(1,)
【问题讨论】:
-
检查
Input(shape = (None,))提供了什么。 -
产生以下错误跟踪:TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
-
由于标签尺寸与输出层的预期不匹配而导致此错误。可以贴一下 label_list[0].shape 的形状吗?
-
label_list[0].shape 是:(2,)。
标签: python tensorflow machine-learning keras deep-learning