【问题标题】:Weird data type error using tensorflow and numpy使用 tensorflow 和 numpy 的奇怪数据类型错误
【发布时间】:2018-11-18 20:07:44
【问题描述】:

我不明白下面两个代码之间的区别:

这样做是否有原因:

def evaluer_dessin ():
    global result,img
    listeimage=[]
    for y in matrice:
        for x in y:
            listeimage.append(x)
    img = np.array([listeimage])

    afficher_resultat()  

    try:
        Cadre.delete(result)
    except:
        pass
    if evaluerimg:
        result=Cadre.create_text(Largeur/2,Hauteur/8,text="c'est un 
"+str((prediction[0]+1)%10),font=('', '50'),fill="red")
    root.after(300,evaluer_dessin)

def afficher_resultat():
    global prediction
    prediction = session.run(y_pred_cls,feed_dict={ x : img})

但是这里的代码没有:

def evaluer_dessin ():
    global result
    listeimage=[]
    for y in matrice:
        for x in y:
            listeimage.append(x)
    img = np.array([listeimage])

    # this is the line that doesn't work, it says that it cant convert an
    # int into a tensor
    prediction = session.run(y_pred_cls,feed_dict={ x : img})

    try:
        Cadre.delete(result)
    except:
        pass
    if evaluerimg:
        result=Cadre.create_text(Largeur/2,Hauteur/8,text="c'est un 
"+str((prediction[0]+1)%10),font=('', '50'),fill="red")
    root.after(300,evaluer_dessin)

上面说“img”是一个int但它的类型显然是一个数组,最奇怪的是当我使用单独的函数来计算预测时,它运行良好!

这是第二个程序显示的错误:

>Traceback (most recent call last):
>  File "C:\Users\nicol\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1092, in _run
>    subfeed, allow_tensor=True, allow_operation=False)
>  File "C:\Users\nicol\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
>    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
>  File "C:\Users\nicol\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3579, in >_as_graph_element_locked
>    types_str))
>TypeError: Can not convert a int into a Tensor.
>
>During handling of the above exception, another exception occurred:
>
>Traceback (most recent call last):
>  File "C:\Users\nicol\Desktop\CNN_exe\programmeCNNexe.py", line 170, in <module>
>    evaluer_dessin()
>  File "C:\Users\nicol\Desktop\CNN_exe\programmeCNNexe.py", line 123, in >evaluer_dessin
>    prediction = session.run(y_pred_cls,feed_dict={ x : img})
>  File "C:\Users\nicol\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
>    run_metadata_ptr)
>  File "C:\Users\nicol\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1095, in _run
>    'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
>TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into >a Tensor.
>[Finished in 2.1s with exit code 1]
>[shell_cmd: python -u "C:\Users\nicol\Desktop\CNN_exe\programmeCNNexe.py"]
>[dir: C:\Users\nicol\Desktop\CNN_exe]
>[path: C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS >Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\Syst>em32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management >Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine >Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\System32\OpenSSH\;C:\Users\nicol\AppData>\Local\Programs\Python\Python35\Scripts\;C:\Users\nicol\AppData\Local\Programs\>Python\Python35\;C:\Users\nicol\AppData\Local\Programs\Python\Python37\Scripts\>;C:\Users\nicol\AppData\Local\Programs\Python\Python37\;C:\Users\nicol\AppData\>Local\Microsoft\WindowsApps;]

【问题讨论】:

  • 有趣的问题 - 您介意在问题中包含完整的错误消息(包括堆栈跟踪,如果有的话)吗? (只需按“编辑”即可添加)
  • 问题不在于img,而在于x。在afficher_resultat 中似乎是一个全局变量,而在evaluer_dessin 中它来自全局变量matrice(来自循环),因此很难判断它何时或为什么应该工作,但无论如何看起来它不是图中的张量,而是一个普通的整数值。

标签: python tensorflow tensor


【解决方案1】:

感谢 jdehesa 帮助我解决问题。问题实际上来自变量 x 而不是 img。 我所要做的就是为 for 循环中的变量指定另一个名称:

def evaluer_dessin ():
    global listeimage,result,img
    listeimage=[]
    for y in matrice:
        for z in y: # I just change the name from "x" to "z" and the problem was solved
            listeimage.append(z)
    img = np.array([listeimage])

    prediction = session.run(y_pred_cls,feed_dict={ x : img})

    try:
        Cadre.delete(result)
    except:
        pass
    if evaluerimg:
        result=Cadre.create_text(Largeur/2,Hauteur/8,text="c'est un "+str((prediction[0]+1)%10),font=('', '50'),fill="red")
    root.after(300,evaluer_dessin)

我是 stackoverflow 的新手,我只想说这个网站有多棒,你可以从聪明而善良的人那里得到你想要的所有答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2021-10-18
    • 2013-12-16
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多