zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【北京大学】1 TensorFlow1.x中Python基础知识

2023-09-14 09:13:09 时间

知识来源-bilibili北京大学tensorflow

1 tutle模块

小游戏模块

import tutle #导入turtle模块
t = turtle.Pen() #用turtle模块中Pen类,实例化出一个叫做t的对象
t.forward(像素点)      # 让t向前走多少个像素点
t.backward(像素点)     # 让t向后走多少个像素点
t.left(像素点)      # 让t左转多少角度
t.right(像素点)         # 让t右转多少角度
t.reset(像素点)         # 让t复位

2 函数、模块、包

(1)函数

def hi_name(yourname)
    print "hello %s"%yourname #百分号表示占位

(2)模块
模块是一个函数的几何,先导入,再使用,用模块.函数名调用

import time
time.asctime()

(3)包
包是包含多个模块

from PIL import Image

3 类

(1)概念
类:物以类聚人以群分,是函数的几何,可实例化出对象的模具
实例化:对象 = 类()
对象:是类实例化出来的实体,对象实实在在存在的,完成具体的工作
面向对象:程序员反复修改优化类,类实例化出对象,对象调用类里面的函数具体的操作。

class 类名(父类名)pass# 占位符

(2)规定

  • 先用pass占位置,起架构;再用具体的函数替换pass完善类
  • 类里定义函数时,语法规定第一个参数必须是self
  • __init__函数,在新对象实例化时会自动运动,用于给新对象赋初值
  • 对象调用类里的函数,用对象.函数名
  • 对象调用类的变量,用对象.变量名
  • 类内定义函数时,如调用自身或父类的函数与变量时,必须用self.引导,应写为self.函数名或self.变量名
# pytest.py
class Animals()
    def breathe(self):
        print "breathing"
    def move(self):
        print "moving"
    def ear(self):
        print "eating food"
class Mammals(Animals):
    def breastfeed(self):
        print "feeding young"
class Cats(Mammals):
    def __init__(self,spots):
        self.spots = spots
    def catch_mouse(self):
        print "catch mouse"
    def left_foot_forward(self):
        print "left foot forward"
    def left_foot_backward(self):
        print "left foot backwad"
    def dance(self):
        self.left_foot_forward()
        self.left_foot_backward()
        self.left_foot_forward()
        self.left_foot_backward()
kitty = Cats(10)
print kitty.spots
kitty.dance()
kitty.breastfeed()
kitty.move()

4 文件的读写

import pickle
开: 文件变量 = open(“文件路径文件名”,“wb”)
存:pickle.dump(待写入的变量,文件变量)
关:文件变量.close()
举例:存储文件

# savedata.py
imprt pickle
game_data = {"postition":"N2 E3","pocket":["key","knife"],"money":160}
save_file = open("save.dat","wb") #打开文件
pickle.dump(game_data,save_file) # 存储文件
save_file.close()

举例:读取文件

# readdata.py
imprt pickle
load_file = open("save.dat","rb") #打开文件
load_game_data = pickle.load(load_file) # 存储文件
load_file.close()
print load_game_data

5 Bug总结

linux系统中保存写入带有中文的文件报错:SyntaxError:Non-ASCII character ‘\xe8’ in file x.py on line 1 ,but no encoding declared
分析:因为保存的文件编码格式不对,不兼容中文汉字,需要改成UTF-8编码格式
解决:在文件的第一行加上#coding:utf-8

相关笔记

以下所有源码以及更详细PDF笔记请在github下载
TensorFolwNotebook-from-Peking-University

  1. 【北京大学】1 TensorFlow1.x中Python基础知识
  2. 【北京大学】2 TensorFlow1.x的张量、计算图、会话
  3. 【北京大学】3 TensorFlow1.x的前向传播推导与实现
  4. 【北京大学】4 TensorFlow1.x的反向传播推导与实现
  5. 【北京大学】5 TensorFlow1.x的损失函数和交叉熵举例讲解及实现
  6. 【北京大学】6 TensorFlow1.x的学习率、滑动平均和正则化实例及实现
  7. 【北京大学】7 TensorFlow1.x的神经网络模块设计思想举例及实现
  8. 【北京大学】8 TensorFlow1.x的Mnist数据集实例实现
  9. 【北京大学】9 TensorFlow1.x的实现自定义Mnist数据集
  10. 【北京大学】10 TensorFlow1.x的卷积神经网络(CNN)相关基础知识
  11. 【北京大学】11 TensorFlow1.x的卷积神经网络模型Lenet5实现
  12. 【北京大学】12 TensorFlow1.x的卷积神经网络模型VGGNet实现
  13. 【北京大学】13 TensorFlow1.x的项目实战之手写英文体识别OCR技术
  14. 【北京大学】14 TensorFlow1.x的二值神经网络实现MNIST数据集手写数字识别
  15. 【北京大学】15 TensorFlow1.x的项目实战之人脸表情识别
  16. 【北京大学】16 TensorFlow1.x的项目实战之图像风格融合与快速迁移