【问题标题】:deep lerning number guesser with python(tensorflow)使用python(tensorflow)进行深度学习数字猜测器
【发布时间】:2021-06-16 13:45:19
【问题描述】:

我用python(在jupyter notebook,我用了十个)做了一个深度学习数字猜测器,当我运行代码时,它应该打开一个空窗口(用pygame),我可以画数字来训练深度学习,但我得到了 AttributeErrors 这是我的代码:

import tensorflow as tf
import pygame
import matplotlib.pyplot as plt
import numpy as np
from tkinter import *
from tkinter import messagebox

class pixel(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = (255,2555,255)
        self.neighbors = []
    def draw(self, surface):
        pygame.draw.rect(surface, self.color, (self.x, self.y, self.x + self.width, self.y + self.height))
    
    def getNeighbors(self, g):
        j = self.x // 20
        i = self.y // 20
        rows = 28
        cols = 28
        
        if i < cols -1:
            self.neighbors.append(g.pixels[i + 1][j])
        if i > 0:
            self.neighbors.append(g.pixels[i - 1][j])
        if j < rows -1:
            self.neighbors.append(g.pixels[i][j + 1])
        if j > 0:
            self.neighbors.append(g.pixels[i - 1][j + 1])
        
        if j > 0 and i > 0:
            self.neighbors.append(g.pixels[i - 1][j - 1])
        if j + 1 < rows and i > -1 and i - 1 > 0:
            self.neighbors.append(g.pixels[i - 1][j + 1])
        if j - 1 < rows and i < cols - 1 and j - 1 > 0:
            self.neighbors.append(g.pixels[i + 1][j - 1])
        if j < rows - 1 and i < cols - 1:
            self.neighbors.append(g.pixels[i + 1][j + 1])

class grid(object):
    pixels = []
    
    def __init__(self, row, col, width, height):
        self.rows = row
        self.cols = col
        self.len = row * col
        self.width = width
        self.height = height
        self.generatePixels
        pass
    
    def draw(self, surface):
        for row in self.pixels:
            for col in row:
                col.draw(surface)
    
    def generatePixels(self):
        x_gap = self.width // self.cols
        y_gap = self.height // self.rows
        self.pixels = []
        for r in range(self.rows):
            self.pixels.append([])
            for c in range(self.cols):
                self.pixels[r].append(pixel(x_gap * c, y_gap * r, y_gap * r, x_gap, y_gap))
                
        for r in range(self.rows):
            for c in range(self.cols):
                self.pixels[r][c].getNeighbors(self)

    def clicked(self, pos):
        try:
            t = pos[0]
            w = pos[1]
            g1 = int(t) // self.pixels[0][0].width
            g1 = int(t) // self.pixels[0][0].width
            g2 = int(w) // self.pixels[0][0].height
            
            return self.pixels[g2][g1]
        except:
            pass
    def convert_binary(self):
        li = self.pixels
        
        newMatrix = [[] for x in range(len(li))]
        
        for i in range(len(li)):
            for j in range(len(li[i])):
                if li[i][j].color == (255,225,255):
                    newMatrix[i].append(0)
                else:
                    newMatrix[i].append(1)
        mnist = tf.keras.datasets.mnist
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        x_test = tf.keras.utils.normalize(x_test, axis=1)
        for row in range(28):
            for x in range(28):
                x_test[0][row][x] = newMatrix[row][x]
                
        return x_test[:1]
    
def guess(li):
    model = tf.keras.models.load_model('m.model')
    
    predictions = model.predict(li)
    print(predictions[0])
    t = (np.argmax(predictions[0]))
    print("I predict this number is a:", t)
    window = Tk()
    window = withdraw()
    messagebox.showinfow("prediction", "I predict this number is a:" + str(t))
    #plt.imshow(li[0], cmap=plt.cm.binary)
    #pit.show()
    
def main():
    run = True

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                li = g.convert_binary()
                guess(li)
                g.generatePixels()
            if pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()
                clicked = g.clicked(pos)
                clicked.color = (0,0,0)
                for n in clicked.neighbors:
                    n.color = (0,0,0)
                    
            if pygame.mouse.get_pressed()[2]:
                try:
                    pos = pygame.mouse.get_pos()
                    clicked = g.clicked(pos)
                    clicked.color = (255,255,255)
                except:
                    pass
                
        g.draw(win)
        pygame.display.update()

pygame.init()
width = height = 560
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Number Guesser")
g = grid(28, 28, width, height)
main()

这是输出:

pygame 2.0.1 (SDL 2.0.14, Python 3.8.8)
Hello from the pygame community. https://www.pygame.org/contribute.html

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-a4fa9a8a2b5c> in <module>
    149 pygame.display.set_caption("Number Guesser")
    150 g = grid(28, 28, width, height)
--> 151 main()

<ipython-input-1-a4fa9a8a2b5c> in main()
    129                 pos = pygame.mouse.get_pos()
    130                 clicked = g.clicked(pos)
--> 131                 clicked.color = (0,0,0)
    132                 for n in clicked.neighbors:
    133                     n.color = (0,0,0)

AttributeError: 'NoneType' object has no attribute 'color'

【问题讨论】:

  • clicked = g.clicked(pos) 正在重新调整 None
  • 好吧,无论g.clicked() 返回什么都是None。也许您应该在尝试访问 .color 之前检查一下?
  • 你在.clicked() 中只有一个try: except: - 永远,永远 使用try: except:,没有选择异常类型。

标签: python tensorflow matplotlib jupyter-notebook pygame


【解决方案1】:
  1. g.clicked() 显然返回 None 以便您在访问返回值时得到 NoneType 对象错误。
  2. 如果我们查看clicked() 方法,我们可以看到有一个try: ... except: pass 构造。这意味着任何和所有可能发生的错误都会被抛出窗口,并且由于没有显式返回的函数返回None,我们可以假设这就是发生的事情。
    • 要点 1:永远,永远 像那样使用 try: except:。让异常发生并在其他地方处理即可。
  3. 在该函数中可能存在我们不知道的一些异常的原因可能是例如
    • 解压参数时的IndexError
    • IndexError 访问 self.pixels
    • ZeroDivisionError
    • NameErrorAttributeError 如果我们打错字了
    • 还有其他的,如果我们知道的话,见鬼
  4. 暂时先看看是什么初始化了self.pixels...看起来generatePixels就是这个东西。
  5. 什么叫generatePixels?嗯......没什么,因为在grid__init__ 中对它的“调用”缺少调用括号:self.generatePixels 应该是self.generatePixels()
    • 要点 2:使用 IDE 或 linter 来警告您类似的事情。

所以,追溯:

  • self.pixels 永远不会被初始化,因为 generatePixels 没有被调用。
  • 访问self.pixels 的异常被裸except: 吞噬。
  • 您会从clicked() 函数中获得None
  • 您尝试访问 None 并收到异常。

虽然代码中还有其他明显的问题,但您可能需要先开始

  • 修复对generatePixels()的调用
  • 解开try: except:s

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2017-09-24
    相关资源
    最近更新 更多