gpt4 book ai didi

python - 使用tensorflow和numpy时出现奇怪的数据类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:47 24 4
gpt4 key购买 nike

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

这是否有效:

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

最佳答案

感谢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 的新手,我只是想说这个网站有多棒,你可以从聪明而友善的人那里得到你想要的所有答案。

关于python - 使用tensorflow和numpy时出现奇怪的数据类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364952/

24 4 0
文章推荐: c# - 将 List 绑定(bind)到 c# wpf 中的组合框