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

在Flask使用TensorFlow的幾個(gè)常見(jiàn)錯(cuò)誤及解決

 更新時(shí)間:2024年01月12日 09:11:13   作者:渣渣的夏天  
這篇文章主要介紹了在Flask使用TensorFlow的幾個(gè)常見(jiàn)錯(cuò)誤及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在Flask使用TensorFlow的幾個(gè)常見(jiàn)錯(cuò)誤

常見(jiàn)錯(cuò)誤一

1. ValueError: Tensor Tensor(“dense_1/Sigmoid:0”, shape=(?, 1), dtype=float32) is not an element of this graph.

在Flask中使用tensorflow的model,一在界面中調(diào)用 model.predict() 就報(bào)下面這個(gè)錯(cuò)誤,不過(guò)在單獨(dú)的 .py 文件中使用卻不報(bào)錯(cuò)。

ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.

這個(gè)bug真的很是糾心,網(wǎng)上一般說(shuō)是添加如下代碼

import tensorflow as tf
graph = tf.get_default_graph()
model = models.load_model(…………)

# 使用處添加:
global graph
global model
with graph.as_default():
    model.predict()
    # 執(zhí)行預(yù)測(cè)函數(shù)

但是我當(dāng)時(shí)測(cè)試時(shí)又報(bào)了另一個(gè)bug,但是這個(gè)bug也不好解決,試了很多方法也沒(méi)解決,當(dāng)然最終還是可以解決的,具體解決方式參考第三點(diǎn)。

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.
     [[{{node dense_1/BiasAdd/ReadVariableOp}}]]

后來(lái)經(jīng)過(guò)N遍測(cè)試后找到了以下兩種解決方式,僅供參考:

解決方法一:

在調(diào)用前加載model和graph,但是這樣會(huì)導(dǎo)致程序每次調(diào)用都需要重新加載model,然后運(yùn)行速度就會(huì)很慢,不過(guò)這種修改方式是最簡(jiǎn)單的。

    graph = tf.get_default_graph()
    model = models.load_model('./static/my_model2.h5')
    with graph.as_default():
        result = model.predict(tokens_pad)

解決方法二:

在創(chuàng)建model后,先使用一遍 model.predict(),參數(shù)的大小和真實(shí)大小一致,這個(gè)是真正解決之道,同時(shí)不影響使用速率。

# 使用前:
model = models.load_model('./static/my_model2.h5')
# a 矩陣大小和 tokens_pad 一致
a = np.ones((1, 220))
model.predict(a)

# 使用時(shí):
global model
result = model.predict(tokens_pad)

但是在使用后又遇到了 The Session graph is empty…… 的錯(cuò)誤即第二點(diǎn),不過(guò)估摸著這個(gè)是個(gè)例,應(yīng)該是程序問(wèn)題。

常見(jiàn)錯(cuò)誤二

2. RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

在相關(guān)代碼使用前添加graph即可。

    graph = tf.get_default_graph()
    with graph.as_default():
        # 相關(guān)代碼
        # 本次測(cè)試中是需要把調(diào)用包含model.predict()方法的方法的代碼放到這里

常見(jiàn)錯(cuò)誤三

3. tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.[[{{node dense_1/BiasAdd/ReadVariableOp}}]]

這個(gè)錯(cuò)誤呢,也是TensorFlow和Flask結(jié)合使用時(shí)的常見(jiàn)錯(cuò)誤

解決方式

如下:

from tensorflow.python.keras.backend import set_session
# 程序開(kāi)始時(shí)聲明
sess = tf.Session()
graph = tf.get_default_graph()

# 在model加載前添加set_session
set_session(sess)
model = models.load_model(…………)

# 每次使用有關(guān)TensorFlow的請(qǐng)求時(shí)
# in each request (i.e. in each thread):
global sess
global graph
with graph.as_default():
    set_session(sess)
    model.predict(...)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python+Matplotlib繪制3D圖像的示例詳解

    Python+Matplotlib繪制3D圖像的示例詳解

    這篇文章主要為大家介紹了如何使用python matplotlib繪制繪制出一系列酷炫的3D圖像,例如:3D散點(diǎn)圖,3D曲線圖等,感興趣的可以了解一下
    2022-04-04
  • Python2與Python3關(guān)于字符串編碼處理的差別總結(jié)

    Python2與Python3關(guān)于字符串編碼處理的差別總結(jié)

    這篇文章主要給大家介紹了Python2與Python3關(guān)于字符串編碼處理差別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python?import?引用上上上級(jí)包的三種方法

    python?import?引用上上上級(jí)包的三種方法

    這篇文章主要介紹了python?import?引用上上上級(jí)包的三種方法包的三種方法,需要的朋友可以參考下
    2023-02-02
  • Windows上使用virtualenv搭建Python+Flask開(kāi)發(fā)環(huán)境

    Windows上使用virtualenv搭建Python+Flask開(kāi)發(fā)環(huán)境

    在自己本機(jī)的開(kāi)發(fā)環(huán)境下,我們完全可以使用virtualenv來(lái)hold住多個(gè)Python環(huán)境,這樣就可以留出一個(gè)專門服役于Flask框架,哈哈,這里我們就來(lái)看看如何在Windows系統(tǒng)上使用virtualenv搭建Python+Flask開(kāi)發(fā)環(huán)境
    2016-06-06
  • Python中turtle繪圖模塊的詳細(xì)講解

    Python中turtle繪圖模塊的詳細(xì)講解

    Turtle庫(kù)是Python語(yǔ)言中一個(gè)很流行的繪制圖像的函數(shù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Python中turtle繪圖模塊的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • 調(diào)用其他python腳本文件里面的類和方法過(guò)程解析

    調(diào)用其他python腳本文件里面的類和方法過(guò)程解析

    這篇文章主要介紹了調(diào)用其他python腳本文件里面的類和方法過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python使用?OpenCV?進(jìn)行圖像投影變換

    Python使用?OpenCV?進(jìn)行圖像投影變換

    這篇文章主要介紹了Python使用?OpenCV?進(jìn)行圖像投影變換,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • python解析xml文件實(shí)例分析

    python解析xml文件實(shí)例分析

    這篇文章主要介紹了python解析xml文件的方法,實(shí)例分析了Python針對(duì)XML文件節(jié)點(diǎn)及字段的獲取技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • python getopt 參數(shù)處理小示例

    python getopt 參數(shù)處理小示例

    getopt是python中專門用來(lái)處理參數(shù)的一個(gè)模塊,十分好用,下面提供一個(gè)小示例
    2009-06-06
  • Python實(shí)現(xiàn)查找數(shù)組中任意第k大的數(shù)字算法示例

    Python實(shí)現(xiàn)查找數(shù)組中任意第k大的數(shù)字算法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)查找數(shù)組中任意第k大的數(shù)字算法,涉及Python針對(duì)數(shù)組的排序、查找等相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01

最新評(píng)論