欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Tensorflow 同時(shí)載入多個(gè)模型的實(shí)例講解

 更新時(shí)間:2018年07月27日 14:41:50   作者:Icoding_F2014  
今天小編就為大家分享一篇Tensorflow 同時(shí)載入多個(gè)模型的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

有時(shí)我們希望在一個(gè)python的文件空間同時(shí)載入多個(gè)模型,例如 我們建立了10個(gè)CNN模型,然后我們又寫了一個(gè)預(yù)測(cè)類Predict,這個(gè)類會(huì)從已經(jīng)保存好的模型restore恢復(fù)相應(yīng)的圖結(jié)構(gòu)以及模型參數(shù)。然后我們會(huì)創(chuàng)建10個(gè)Predict的對(duì)象Instance,每個(gè)Instance負(fù)責(zé)一個(gè)模型的預(yù)測(cè)。

Predict的核心為:

class Predict:
 def __init__(self....):
  創(chuàng)建sess
  創(chuàng)建恢復(fù)器tf.train.Saver
  從恢復(fù)點(diǎn)恢復(fù)參數(shù):tf.train.Saver.restore(...)


 def predict(self,...):
  sess.run(output,feed_dict={輸入})

如果我們直接輪流生成10個(gè)不同的Predict 對(duì)象的話,我們發(fā)現(xiàn)tensorflow是會(huì)報(bào)類似于下面的錯(cuò)誤:

 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
 pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [256,512] rhs shape= [640,512]
   [[Node: save/Assign_14 = Assign[T=DT_FLOAT, _class=["loc:@fullcont/Variable"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](fullcont/Variable, save/RestoreV2_14)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "PREDICT_WITH_SPARK_DATAFLOW_WA.py", line 121, in <module>
 pre2=Predict(label=new_list[1])
 File "PREDICT_WITH_SPARK_DATAFLOW_WA.py", line 47, in __init__
 self.saver.restore(self.sess,self.ckpt.model_checkpoint_path)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1560, in restore
 {self.saver_def.filename_tensor_name: save_path})
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 895, in run
 run_metadata_ptr)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1124, in _run
 feed_dict_tensor, options, run_metadata)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
 options, run_metadata)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
 raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [256,512] rhs shape= [640,512]

關(guān)鍵就是:

Assign requires shapes of both tensors to match.意思是載入模型的時(shí)候 賦值失敗。主要是因?yàn)椴煌瑢?duì)象里面的不同sess使用了同一進(jìn)程空間下的相同的默認(rèn)圖graph。

正確的解決方法:

class Predict:
 def __init__(self....):
  self.graph=tf.Graph()#為每個(gè)類(實(shí)例)單獨(dú)創(chuàng)建一個(gè)graph
  with self.graph.as_default():
    self.saver=tf.train.import_meta_graph(...)#創(chuàng)建恢復(fù)器
    #注意!恢復(fù)器必須要在新創(chuàng)建的圖里面生成,否則會(huì)出錯(cuò)。
  self.sess=tf.Session(graph=self.graph)#創(chuàng)建新的sess
  with self.sess.as_default():
    with self.graph.as_default():
     self.saver.restore(self.sess,...)#從恢復(fù)點(diǎn)恢復(fù)參數(shù)

 def predict(self,...):
  sess.run(output,feed_dict={輸入})

以上這篇Tensorflow 同時(shí)載入多個(gè)模型的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論