从.h5转换为.tflite时,具有默认keras loss_categorical_crossentropy的未知损失函数

时间:2019-03-11 08:54:32

标签: python tensorflow keras rstudio loss-function

我正在尝试将经过训练的模型转换为.h5文件,并将其保存为.tflite文件。我正在使用rstudio / keras库,因为我在R中工作。但是没有用于R语言的转换器,我切换到Python来转换文件。 当我尝试运行以下脚本时

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_keras_model_file("test.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

我正在使用keras提供的默认损失函数

model %>% compile(
  loss = loss_categorical_crossentropy,
  optimizer = optimizer_adadelta(),
  metrics = c('accuracy')

我收到以下错误

Traceback (most recent call last):
  File "/tmp/RtmpWIBDmu/chunk-code-849197e0a8f.txt", line 3, in <module>
    converter = tf.lite.TFLiteConverter.from_keras_model_file("test.h5")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/lite/python/lite.py", line 370, in from_keras_model_file
    keras_model = _keras.models.load_model(model_file)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/saving.py", line 266, in load_model
    sample_weight_mode=sample_weight_mode)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/checkpointable/base.py", line 442, in _method_wrapper
    method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/training.py", line 282, in compile
    loss_function = training_utils.get_loss_function(loss)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/training_utils.py", line 873, in get_loss_function
    return losses.get(loss)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/losses.py", line 594, in get
    return deserialize(identifier)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/losses.py", line 585, in deserialize
    printable_module_name='loss function')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/utils/generic_utils.py", line 212, in deserialize_keras_object
    function_name)
ValueError: Unknown loss function:loss_categorical_crossentropy

0 个答案:

没有答案