Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TensorFlow估计器和大量预训练模型:跨调用重用?_Tensorflow_Tensorflow Hub - Fatal编程技术网

TensorFlow估计器和大量预训练模型:跨调用重用?

TensorFlow估计器和大量预训练模型:跨调用重用?,tensorflow,tensorflow-hub,Tensorflow,Tensorflow Hub,我正在尝试使用TensorFlow中预先训练好的模型的转移学习-带有估计器API。模型的细节(层数、神经元等)将发生变化,并且不相关 get_feature_extractor()实例化TensorFlow集线器模块。最终的结果是,对.train_和_evaluate(),.predict()等的每次调用都会破坏会话和图形,从头开始,重新加载特性提取器。这需要几秒钟。是否有一种干净的方法可以在这些调用中持久化get_feature_extractor()的结果,并使其保持会话—至少对于.pred

我正在尝试使用TensorFlow中预先训练好的模型的转移学习-带有估计器API。模型的细节(层数、神经元等)将发生变化,并且不相关

get_feature_extractor()实例化TensorFlow集线器模块。最终的结果是,对.train_和_evaluate(),.predict()等的每次调用都会破坏会话和图形,从头开始,重新加载特性提取器。这需要几秒钟。是否有一种干净的方法可以在这些调用中持久化get_feature_extractor()的结果,并使其保持会话—至少对于.predict()?或者我必须使用较低级别的API来实现这一点

def model_fn(features, labels, mode):
    feature_extractor = get_feature_extractor()
    layer = feature_extractor(features)
    layer = tf.layers.batch_normalization(layer)
    layer = tf.layers.dense(inputs=layer, units=1280, activation=tf.nn.relu)
    layer = tf.layers.dense(inputs=layer, units=2048, activation=tf.nn.relu)
    layer = tf.layers.dense(inputs=layer, units=512, activation=tf.nn.relu)
    layer = tf.layers.dense(inputs=layer, units=2)
    if mode == tf.estimator.ModeKeys.PREDICT:
        estimator = tf.estimator.EstimatorSpec(mode, predictions=layer)     
    else:
        accuracy = tf.metrics.accuracy(labels=labels,
                               predictions=layer,
                               name='acc_op')
        metrics = {'accuracy': accuracy}
        loss = tf.losses.mean_squared_error(labels=labels, predictions=layer)
        optimizer = tf.train.AdamOptimizer()
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        estimator = tf.estimator.EstimatorSpec(
            mode=mode, loss=loss, train_op=train_op,
            eval_metric_ops=metrics)    
    return estimator

看起来像是针对估计器的一般问题/功能请求,避免从检查点重复初始化会话。对于TensorFlow Hub,没有特殊规定,对不起。查看Estimator.predict()的代码,我也没有看到太多用于交互的代码。但是,如果这是您的用例,那么当然有。在这种情况下,TensorFlow Hub的唯一特殊之处在于,您从中获得的模型往往非常大(至少与我自己培训过的任何模型相比)。如果我自己在ImageNet上进行培训,比如说《盗梦空间》,我也会遇到同样的问题。