使用tensorflow和numpy的奇怪数据类型错误

2024-09-28 01:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是否有原因:

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;]

Tags: runinpyimglocallinefilesprogram
1条回答
网友
1楼 · 发布于 2024-09-28 01:23:28

谢谢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的新手,我只想说这个网站是多么棒,你能从聪明善良的人那里得到你想要的所有答案。你知道吗

相关问题 更多 >

    热门问题