首页 > 其他分享 >《深度学习的数学》(涌井良幸、涌井贞美著) 神经网络计算pytorch示例一

《深度学习的数学》(涌井良幸、涌井贞美著) 神经网络计算pytorch示例一

时间:2023-11-01 21:31:45浏览次数:42  
标签:1.0 示例 涌井良幸 涌井贞美著 torch 0.0 print output data

涌井良幸、涌井贞美著的《深度学习的数学》这本书,浅显易懂。书中还用Excel示例神经网络的计算,真是不错。但光有Excel示例还是有点欠缺的,如果有代码演示就更好了。

百度了半天在网上没找到别人写的,只好自己撸一个(使用python + pytorch),供同样在学习神经网络的初学者参考。

(注,这是书中4-4节:体验神经网络的误差反向传播法,数据是64个3x4的0和1,用平方误差的总和作为代价函数, 用 Sigmoid 函数作为激活函数)

主程序demo44.py:

import torch
import torch.nn as nn
import torch.nn.init as init
import torch.optim as optimal
from torch import cosine_similarity

import demo44data as demo


# 定义一个简单的神经网络
class SimpleNet(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleNet, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        # self.activation = nn.ReLU()
        self.activation = nn.Sigmoid()
        self.fc2 = nn.Linear(hidden_size, output_size)
        init.normal_(self.fc1.weight, mean=0.0, std=0.5)  # 使用正态分布初始化权重矩阵,本例未用到
        init.normal_(self.fc2.weight, mean=0.0, std=0.5)  # 使用正态分布初始化权重矩阵,本例未用到
        init.normal_(self.fc1.bias, mean=0.0, std=0.5)  # 使用正态分布初始化偏置矩阵,本例未用到
        init.normal_(self.fc2.bias, mean=0.0, std=0.5)  # 使用正态分布初始化偏置矩阵,本例未用到

        self.fc1.weight.data = demo.get_param1()  # 若用正态分布,注释此行
        self.fc1.bias.data = demo.get_param2()  # 若用正态分布,注释此行
        self.fc2.weight.data = demo.get_param3()  # 若用正态分布,注释此行
        self.fc2.bias.data = demo.get_param4()  # 若用正态分布,注释此行

    def forward(self, x):
        x = self.fc1(x)
        print_x("z21=", x)
        x = self.activation(x)
        print_x("a21=", x)
        x = self.fc2(x)
        print_x("z31=", x)
        x = self.activation(x)
        print_x("a31=", x)
        return x


def print_x(name, x):
    if x.dim() > 1:
        print(name, end='')
        print("", x[0, :].data.numpy(), end='')
        for i in range(x.size()[0]):
            if i > 0:
                print("\t\t", x[i, :].data.numpy(), end='')
        print()


def print_params(params):
    for param in params:
        if param.dim() > 1:
            for i in range(param.size()[0]):
                print('\t[', end='')
                print(param[i, 0].data.numpy(), end='')
                for j in range(param.size()[1]):
                    if j > 0:
                        print('\t', param[i, j].data.numpy(), end='')
                print('] ', end='')
            print()
        else:
            print('\t[', end='')
            print(param[0].data.numpy(), end='')
            for i in range(param.size()[0]):
                if i > 0:
                    print('\t', param[i].data.numpy(), end='')
            print('] ')
    print()


# 书中用最小二乘法做为代价函数
# 若用torch提供的最小二乘法做为代价函数,计算后得再除以2
# 为了与书中数字一致,自己实现了一个
def mse_loss(x, y):
    z = x - y
    print("  c= ", end='')
    print(((z[0, 0] ** 2 + z[0, 1] ** 2) / 2).data.numpy(), end='')
    for i in range(z.size()[0]):
        if i>0:
            print("\t", ((z[i, 0] ** 2 + z[i, 1] ** 2) / 2).data.numpy(), end='')
    print()
    return (z[:, 0] ** 2 + z[:, 1] ** 2).sum() / 2


# 定义超参数
input_size = 12
hidden_size = 3
output_size = 2
learning_rate = 0.2
epochs = 1000

# 创建模型实例
model = SimpleNet(input_size, hidden_size, output_size)
# print_params(model.parameters())

# 定义损失函数和优化器
# criterion = nn.MSELoss(size_average=False) # torch提供的最小二乘法做为代价函数,criterion(outputs, labels)计算后得再除以2
# criterion = nn.CrossEntropyLoss() 这个应该更好,但为了跟书中数字一致,这里舍弃不用
criterion = mse_loss
# optimizer = optimal.Adam(model.parameters(), lr=learning_rate) # Adam优化算法,请自行百度
optimizer = optimal.SGD(model.parameters(), lr=learning_rate)  # 随机梯度下降法

# 准备数据集和标签
# 这里假设数据集是一个包含输入张量的数据集,标签是一个包含期望输出的张量
data = demo.get_data()
labels = demo.get_result()

# outputs = model(data)
# outputs = torch.sigmoid(output)
# outputs = 1 / (1 + torch.exp(output * -1))
# print("output=", output)
# print("outputs=", outputs)
# loss = criterion(outputs, labels)
# print("loss=", loss)

# 训练模型
for epoch in range(epochs):
    print("\nepoch=", epoch + 1)
    print_params(model.parameters())
    # 前向传播
    outputs = model(data)
    # print("outputs=", outputs)
    # outputs = torch.sigmoid(output)
    # outputs = 1 / (1 + torch.exp(output * -1))
    loss = criterion(outputs, labels)
    print("  C=", loss.data.numpy())

    # 反向传播和优化
    optimizer.zero_grad()
    loss.backward()  # 使用.backward()进行反向传播计算梯度值
    optimizer.step()  # 使用优化器更新网络参数

    # 输出训练进度信息
    # if (epoch + 1) % 10 == 0:
    if loss.item() < 0.25 or epoch + 1 == epochs:
        print("\n===========================")
        print("loss=", loss)
        print_params(model.parameters())
        # print("output=", output)
        # print("outputs=", outputs)
        print(f"Epoch [{epoch + 1}/{epochs}], Loss: {loss.item()}")
        break

print("\n===========================")
# 将模型设置为评估模式
model.eval()
output = model(demo.get_test0()).data
# output = torch.sigmoid(model(demo.get_test0())).data
print(torch.round(output, decimals=0), output)
output = model(demo.get_test1()).data
# output = torch.sigmoid(model(demo.get_test1())).data
print(torch.round(output, decimals=0), output)

print("\n=======  比对全部结果  ======")
result = demo.get_result()
data = demo.get_data()
i = 0
for d in data:
    output = model(d).data
    # output = torch.sigmoid(model(d)).data
    print(i + 1, "=", torch.round(output, decimals=0).numpy(), "\t(", output.numpy(), ")")
    result[i, :] = torch.round(output, decimals=0)
    i += 1
print("准确度:", (cosine_similarity(result, demo.get_result()) * 100).mean().data.numpy(), "%")

demo44data.py

import torch


def get_data():
    return torch.tensor([[
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 0.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        0.0, 1.0, 1.0], [
        0.0, 0.0, 0.0,
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 0.0, 0.0,
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 0.0, 0.0,
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 0.0, 0.0,
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 0.0], [
        0.0, 0.0, 0.0,
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        0.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0,
        0.0, 0.0, 0.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0,
        0.0, 0.0, 0.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0,
        0.0, 0.0, 0.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 0.0,
        0.0, 0.0, 0.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        0.0, 1.0, 1.0,
        0.0, 0.0, 0.0], [
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 0.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0], [
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 1.0,
        0.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 0.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0], [
        0.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 1.0,
        0.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 0.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 1.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        1.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 1.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 1.0,
        0.0, 1.0, 1.0], [
        1.0, 1.0, 0.0,
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        1.0, 1.0, 0.0,
        1.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 0.0, 0.0], [
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0], [
        0.0, 1.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 0.0,
        1.0, 1.0, 0.0], [
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        1.0, 1.0, 0.0], [
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 0.0], [
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 1.0, 0.0], [
        0.0, 1.0, 0.0,
        1.0, 0.0, 0.0,
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0]])


def get_result():
    return torch.tensor([[
        1.0, 0.0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        1, 0], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1], [
        0, 1]
    ])


def get_result2():
    return torch.tensor(
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])


def get_test0():
    return torch.tensor([
        1.0, 1.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 0.0, 1.0,
        1.0, 1.0, 1.0])


def get_test1():
    return torch.tensor([
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 1.0, 0.0])


def get_param1():
    return torch.tensor([[
        0.490, 0.348, 0.073, 0.837, -0.071, -3.617, -0.536, -0.023, -1.717, -1.456, -0.556, 0.852], [
        0.442, -0.537, 1.008, 1.072, -0.733, 0.823, -0.453, -0.014, -0.027, -0.427, 1.876, -2.305], [
        0.654, -1.389, 1.246, 0.057, -0.183, -0.743, -0.461, 0.331, 0.449, -1.296, 1.569, -0.471]
    ])


def get_param2():
    return torch.tensor([-0.185, 0.526, -1.169])


def get_param3():
    return torch.tensor([[
        0.388, 0.803, 0.029], [
        0.025, -0.790, 1.553]
    ])


def get_param4():
    return torch.tensor([-1.438, -1.379])


标签:1.0,示例,涌井良幸,涌井贞美著,torch,0.0,print,output,data
From: https://blog.51cto.com/oldycat/8133220

相关文章

  • Flyweight 享元模式简介与 C# 示例【结构型6】【设计模式来了_11】
    〇、简介1、什么是享元模式?一句话解释:  将相似或同类的对象共享同一个对象,将这些对象暂存在列表中,使用时直接取出,避免每次使用时都要新建浪费资源。享元模式的目的是减少对象的创建,通过共享对象来提高系统的性能。享元设计模式将对象的实例分为两种:内部共享对象和外部共享对......
  • 使用phpQuery库采集平安健康代码示例
    大家好,今天给大家分享的内容是使用phpQuery库采集平安健康相关视频,内容非常简单,篇幅也很短,但是确实很实用,一起学习一下吧。```php<?php//引入phpQuery库require_once'phpQuery/phpQuery.php';//创建一个phpQuery对象$jq=phpQuery::newDocument();//使用配置p......
  • 创建一个Web服务器并保持其运行,可以使用Python的Flask库。以下是一个基本的示例: ```p
    创建一个Web服务器并保持其运行,可以使用Python的Flask库。以下是一个基本的示例:```pythonfromflaskimportFlask,requestimportosapp=Flask(__name__)@app.route('/webhook',methods=['POST'])defwebhook():  data=request.get_json()  #在这里添加你的......
  • Android自动化测试框架:UiAutomator和UiAutomator2的区别与示例代码
    UiAutomator和UiAutomator2是两种常用的Android自动化测试框架,它们都是由Google开发的。然而,它们之间存在一些关键的区别:API级别:UiAutomator框架在Android4.3(API级别18)中引入,而UiAutomator2在Android5.0(API级别21)中引入。测试能力:UiAutomator只能测试Android系统应用......
  • Linux时间校准、时间同步(ntpdate及C代码NTP客户端代码校准示例)
    背景机器每次机启后时间就会出现异常,因为机器无法访问外网,只能访问局域网的ntp服务,所以需要保证局域网内部有ntp服务,如何安装ntp服务,参考Ubuntu20.04Ntp服务安装及验证。网络时间协议NetworkTimeProtocol(NTP)是一种确保时钟保持准确的方法。如果可以访问互联网,只需安装ntp......
  • huatuo热更环境安装与示例项目
    上一节我们分析了huatuo的原理与优势,相信大家迫不及待想体验一下huatuo热更新,本节我们来安装huatuo的开发环境,然后运行示例项目,来体验下huatuo做热更新。huatuo 热更开发环境的安装使用huatuo之前,我们先准备好开发环境,Unity版本,官方的huatuo版本采用的是unity2020.3.33f1的......
  • hugepage 示例代码
    #include<fcntl.h>#include<sys/mman.h>#include<errno.h>#include<stdio.h>#defineMAP_LENGTH(500*1024*1024)//10MBintmain(){intfd;void*addr;//1.创建一个Hugetlb文件系统的文件fd1=open("/dev/hugepag......
  • 量化交易之One Piece篇 - spdlog - 示例demo
    #include<memory>#include<onepiece/datacore/DataCore.h>#include<spdlog/spdlog.h>#include<spdlog/sinks/basic_file_sink.h>#include<memory>usingnamespacestd;intmain(intargc,constchar*argv[]){//testsp......
  • KFC框架:小游戏/H5 首包、分包、加载优化方案与项目示例
    CocosCreator小游戏/H5首包、分包、加载优化方案与项目示例今天是疯狂星期四,给大家晒一下最近的成果。麒麟子上周末将《Jare大冒险》升级到了CocosCreator3.8,并更新到了CocosStore。在做了更精细的分包管理、资源加载拆分,以及利用分析工具剔除了不必要的资源加载后,最终......
  • nginx-变量与常见配置示例
    1、变量$request_method  请求方法$host  来自请求行的主机名,来自主机请求头字段的主机名,或匹配请求的服务器名。$uri  请求url的path部分,自动进行url解码。可能造成响应拆分漏洞$remote_port  客户端端口$request  完整的请求行 2、常见配置示例 ......