如何使用Hyperopt最小化TensorFlow Keras冻结图(.pb)大小?

问题描述 投票:0回答:1

我正在尝试将通过TensorFlow Keras(.pb)创建的冻结图形加载到内存受限的微控制器上。通过Hyperopt(...),我正在优化训练超参数,但是我想将结果模型的大小作为搜索空间的一部分。目前,我包括使用加权损失作为hyperopt的输入,如下所示:

def optimizer(args):
...
  training_history = nn.train_v00(....)
  file_bytes = int(os.path.getsize(frozen_graph_filepath)) # returns size in bytes

  final_loss = training_history.history['loss'][-1]
  weighted_loss = final_loss*(file_bytes/(100*1024)) # Want model smaller than 100KB

  if OPTIMIZATION_TARGET == 'loss':
    return_struct = {
      'status': STATUS_OK,
      'loss': weighted_loss,
      'epochs': epochs,
      'metrics': {
        'accuracy': final_acc
       }
    }

    return return_struct

space = {
  'learning_rate': hp.loguniform('learning_rate', np.log(0.00001), np.log(0.05)),
  'dropout': hp.uniform('dropout', 0, 0.5),
  'batch_size': hp.quniform('batch_size', 32, 128, 2),
  'input_seq_len': hp.quniform('seq_len', 32, N_ADC_SAMPLES, 2),
  'cnn_n_filters': hp.quniform('cnn_n_filters', 1, 10, 1),
  'cnn_kernel_size': hp.quniform('cnn_kernel_size', 1, 10, 1),
  'cnn_pool_size': hp.quniform('cnn_pool_size', 1, 10, 1)
}
t = Trials()
best = fmin(optimizer, space, algo=tpe.suggest, max_evals=MAX_EVALS, trials=t)

根据到目前为止的发现,还没有一种方法可以通过训练直接反向传播模型的大小,但是有更好的方法吗?

感谢您的考虑!

tensorflow keras hyperopt
1个回答
0
投票

如果我对您的理解正确,这是关于如何设计损失函数的问题,而不是直接关于hyperopt的问题。正确?如果是这样,那么如果您确实确实有一个100kb的极限值,那么我就不会扩大您的训练损失(因为这可能会导致出现奇怪的伪像,例如性能不佳,但非常小的模型占据了首位)。

如果模型大小小于100kb,否则返回天文数字,您如何报告常规训练损失?只是一个想法。

© www.soinside.com 2019 - 2024. All rights reserved.