[英]Output of converted CoreML model different than Keras
I'm training a LSTM next character / word predictor in Keras, and want to include it in an iOS project. 我正在Keras中训练LSTM下一个字符/单词预测器,并希望将其包含在iOS项目中。 When I convert it to CoreML, the output shape and values doesn't match my original Keras model. 当我将其转换为CoreML时,输出形状和值与我原来的Keras模型不匹配。
To summarize my question: 总结一下我的问题:
The model I train has the following layout: 我训练的模型有以下布局:
model = Sequential()
model.add(LSTM(128, input_shape=(SEQUENCE_LENGTH, len(chars))))
model.add(Dense(len(chars), activation = 'softmax'))
model.add(Activation('softmax'))
Where a sequence is a list of characters of length 40 ( sequence_length
) and chars
a list of possible characters. 其中序列是长度为40( sequence_length
)的chars
列表,并且列出可能的字符列表。 In this case, 31. So, the output shape of the model is (None,31)
在这种情况下,31。因此,模型的输出形状是(None,31)
If i try to convert the model using 如果我尝试使用转换模型
coreml_model = coremltools.converters.keras.convert(
'keras_model.h5',
input_names=['sentence'],
output_names=['chars'],
class_labels = chars)
I get the following error: 我收到以下错误:
NSLocalizedDescription = "The size of the output layer 'characters' in the neural network does not match the number of classes in the classifier.";
I guess this makes sense, since the output shape has a None-dimension. 我想这是有道理的,因为输出形状具有无维度。
If I don't supply the class_labels
argument, it converts the model just fine. 如果我不提供class_labels
参数,它会很好地转换模型。 However, when running result = coreml_model.predict()
, I now get an output matrix of (40,31)
instead of a single list of 31 character probabilites. 但是,当运行result = coreml_model.predict()
,我现在得到(40,31)
的输出矩阵,而不是31个字符概率的单个列表。
None of the entries in the results matches the values from the Keras model. 结果中的所有条目都不匹配Keras模型中的值。 The only the first entry has unique values for each character - all later entries have the exact same values. 唯一的第一个条目对每个字符都有唯一的值 - 所有后面的条目都具有完全相同的值。
The CoreML model output layer has the following metadata: CoreML模型输出层具有以下元数据:
output {
name: "characters"
shortDescription: "Next predicted character"
type {
multiArrayType {
shape: 31
dataType: DOUBLE
}
}
}
Thank you very much for helping! 非常感谢您的帮助!
The error was in CoreML's incompatibility with multi-dimensional inputs. 错误在于CoreML与多维输入的不兼容性。 I found this blog , which guided me in the right direction. 我找到了这个博客 ,它引导我朝着正确的方向前进。
So to fix it, I had to flatten the input by adding a Reshape layer, and resize the input training data to a single vector. 因此,要修复它,我必须通过添加Reshape图层来平整输入,并将输入训练数据的大小调整为单个矢量。 The new model looks like this: 新模型如下所示:
# Input is now a single vector of length 1240
input_shape = (SEQUENCE_LENGTH*len(chars))
model = Sequential()
# The reshape layer makes sure that I don't have to change anything inside the layers.
model.add(Reshape((SEQUENCE_LENGTH, len(chars)), input_shape=(input_shape,)))
model.add(LSTM(128, input_shape=(SEQUENCE_LENGTH, len(chars))))
model.add(Dense(len(chars)))
model.add(Activation('softmax'))
All input vectors has to be resized the same way: 所有输入向量必须以相同的方式调整大小:
x = x.reshape(x.shape[0], input_shape)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.