栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【DL学习笔记01】深度学习入门——基于Python的理论与实现(ch01&02:Python入门&perceptron)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【DL学习笔记01】深度学习入门——基于Python的理论与实现(ch01&02:Python入门&perceptron)

 

目录

NumPy

Matplotlib

感知机

NumPy
# 导入NumPy
import numpy as np

# np.array()接受Python列表作为参数,生成NumPy数组(numpy.ndarray)
x = np.array([1.0, 2.0, 3.0])
print(x)
print(type(x))

# 算法运算
# 注:元素个数相同时,可以对各个元素进行算数运算。如果元素个数不同,程序就会报错
x = np.array([1.0, 2.0, 3.0])
y = np.array([2.0, 4.0, 6.0])
print(x + y)
print(type(x + y))
print(x - y)
print(type(x - y))
print(x * y)
print(type(x * y))
print(x / y)
print(type(x / y))
# 广播
print(x / 2.0)
print(type(x / 2.0))

# 生成N维数组——矩阵
A = np.array([[3, 0], [0, 6]])
print(A)
print(A.shape)  # 查看矩阵A的形状
print(A.dtype)  # 查看矩阵元素的数据类型

# 矩阵的运算
B = np.array([[3, 0], [0, 6]])
print(A + B)
print(A * B)
print(A * 10)

# 广播
# NumPy中,形状不同的数组之间也可以进行运算
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20])
print(A * B)

# 访问元素
# 注:元素的索引从0开始
X = np.array([[51, 55], [14, 19], [0, 4]])
print(X)
print(X[0])  # 第0行
print(X[0][1])  # (0,1)的元素
# for语句访问各个元素。
for row in X:
    print(row)
# NumPy还可以使用数组访问各个元素
X = X.flatten()  # 将X转换为一维数组
print(X)
print(X[np.array([0, 2, 4])])  # 获取索引为0、 2、 4的元素
print(X > 15)  # 从X中抽出大于15的元素,得到一个布尔型的数组
print(X[X > 15])  # 从X中抽出大于15的元素,取出True对应的元素

Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread

print("#####绘制简单图形#####")
# 生成数据
x = np.arange(0, 6, 0.1)
y = np.sin(x)
# 绘制图形
plt.plot(x, y)
plt.show()
print("#####pyplot的功能#####")
# 生成数据
y1 = np.sin(x)
y2 = np.cos(x)
# 绘制图形
plt.plot(x, y1, label="sin")
plt.plot(x, y2, line, label="cos")  # 用虚线绘制
plt.xlabel("x")  # x轴标签
plt.ylabel("y")  # y轴标签
plt.title('sin&cos')  # 标题
plt.legend()
plt.show()
print("#####显示图像#####")
img = imread('2670536155_c170f49cd0.jpg')
plt.imshow(img)
plt.show()

感知机
# 感知机接收多个输入信号,输出一个信号
# 感知机的信号只有0/1两种取值
import numpy as np

print("###simple realize###")

# 与门
def AND1(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

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = AND1(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))

# 使用权重和偏置实现与门
def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = AND(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))

def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.2
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

print("###多层感知机###")

def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
    return y

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = XOR(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987174.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号