python:《深度学习入门:基于Python的理论与实现》学习记录总结

Anaconda3-5.2.0-Windows-x86_64
Python 3.6.5

1
报错:

syntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

错误源代码:

import matplotlib.pyplot as plt
from matplotlib.image import imread
img=imread('C:\Users\DELL\Desktop\照片\11.jpg')
plt.imshow(img)
plt.show()

原因:在字符串中\是被当作转义字符来使用,无法正确读取图片路径。3种改正代码:

img=imread(r'C:\Users\DELL\Desktop\照片\11.jpg')
img=imread(r'C:/Users/DELL/Desktop/照片/11.jpg')
img=imread('C:\\Users\\DELL\\Desktop\\照片\\11.jpg')

2
报错:

ModuleNotFoundError: No module named 'matplotlib'

进入anaconda prompt然后conda list 下有matplotlib,但观察到spyder右下角console 中spyder 是tensorflow环境目录下的,该环境下并没有加载matplotlib库。解决:打开 anaconda navigator,在base(root)下启动spyder 顺利运行。

3

(1)
报错:SyntaxError: invalid syntax
即:SyntaxError:语法无效。错误提示指向第一行and.
原错误代码:

def and(x1,x2):
    w1,w2, theta=0.5,0.5,0.7
    tmp=x1*w1+x2*w2
    if tmp <= theta:
            return 0
    elif tmp > theta:
            return 1

在python中是区分大小写的,而and是python的逻辑运算符,可以使用and进行多个条件内容的判断。此处将函数名定义为and 是存在歧义的。解决:将def and(x1,x2):更改为def AND(x1,x2):
(2)
报错:

 if tmp <= theta:
  ^
IndentationError: unexpected indent

错误源码:

def AND(x1,x2):
    w1,w2, theta=0.5,0.5,0.7
    tmp=x1*w1+x2*w2
      if tmp <= theta:
            return 0
      elif tmp > theta:
            return 1

(3)
报错:

if tmp <= theta:
                ^
IndentationError: unindent does not match any outer indentation level

错误源码:

def AND(x1,x2):
    w1,w2, theta=0.5,0.5,0.7
    tmp=x1*w1+x2*w2
  if tmp <= theta:
            return 0
  elif tmp > theta:
            return 1

(2)与(3)的报错不同,而错误原因确实相同的,都是因为语句没有按要求对齐,很简单的改正方法,敲空格对齐:

def AND(x1,x2):
    w1,w2, theta=0.5,0.5,0.7
    tmp=x1*w1+x2*w2
    if tmp <= theta:
            return 0
    elif tmp > theta:
            return 1

4
报错:

ValueError: only 2 non-keyword arguments accepted

错误源码:

x=np.array(-5.0,5.0,0.1)

改正:

x=np.array([-5.0,5.0,0.1])

5
报错:

 Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

即CPU支持AVX2扩展,但是你安装的TensorFlow版本无法编译使用。
解决:添加代码

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'

其中:

os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1' # 默认,显示所有信息 
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error 
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' # 只显示 Error

6
报错:

2019󈚮󈚾 09:53:11.576275: W tensorflow/core/framework/allocator.cc:107] Allocation of 1003520000 exceeds 10% of system memory.
2019󈚮󈚾 09:53:15.356398: W tensorflow/core/framework/allocator.cc:107] Allocation of 250880000 exceeds 10% of system memory.
2019󈚮󈚾 09:53:16.054315: W tensorflow/core/framework/allocator.cc:107] Allocation of 501760000 exceeds 10% of system memory.

分配超过了系统内存的10%。通过警告信息,猜测可能是一次性将测试数据喂入神经网络要求分配的内存过大,可以减少一次喂入神经网络的数据量。
错误源码:

#开始训练模型,循环20000次,每次随机从训练集中抓取50幅图像
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i%100 == 0:
        #每100次输出一次日志
        train_accuracy = accuracy.eval(feed_dict={
            x:batch[0], y_:batch[1], keep_prob:1.0})
        print( "step %d, training accuracy %g" % (i, train_accuracy))

改正:

#开始训练模型,循环20000次,每次随机从训练集中抓取《5》幅图像
for i in range(20000):
    batch = mnist.train.next_batch(5)#为50时报错:Allocation of 250880000 exceeds 10% of system memory.

7.
报错:

 TypeError: object() takes no parameters 

源码:

class Restaurant():
    def _init_(self,name,cuisine_type):
        self.name=name
        self.cuisine_type=cuisine_type
    def describe(self):
        print('the restaurant mamed:'+self.name)
        print('the cuisine type is '+self.cuisine_type)
    def open_r(self):
        print(self.name+' is openning')
        
restaurant=Restaurant('hxc','cool')
restaurant.describe()
restaurant.open_r()

改正:

 _init_应该是两个_ , 即__init__

8.
报错:

TypeError: __init__() missing 1 required positional argument: 'privileges'`

源码:

class User():
    def __init__(self,firstname,lastname):
        self.firstname=firstname
        self.lastname=lastname
    def describe(self):
        print('The user name is '+self.firstname.title()+' '+self.lastname.title())
    def greet(self):
        print('Hello and welcome:'+self.firstname.title()+' '+self.lastname.title())
class Privileges():
    def __init__(self,privileges):
        self.privileges=['can add post','can delete post','can ban user']
    def show_privileges(self):
        print('This is a admin and has privileges:')
        print(self.privileges) 
class Admin(User):
    def __init__(self,firstname,lastname):
        super().__init__(firstname,lastname)
        self.privileges=Privileges()#使用类Privileges   
u=Admin('wang','juncheng')
u.privileges.show_privileges()

改正:
这个类其实是需要一个参数’privileges’的;此处没有指定默认值,所以后续调用的时候需要再给这个参数。

# 解决办法1:在privileges定义的时候去掉第二个参数
class Privileges():
    def __init__(self):
        self.privileges=['can add post','can delete post','can ban user']
# 解决办法2:在后续调用时,给一个初始空值('')
class Admin(User):
    def __init__(self,firstname,lastname):
        super().__init__(firstname,lastname)
        self.privilege=Privileges('')
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值