美文网首页
神经网络03

神经网络03

作者: 平头哥2 | 来源:发表于2020-09-17 15:50 被阅读0次

损失函数

import numpy as np

# 均方误差
def mean_squared_error(y, t):
    return 0.5 * np.sum((y - t)**2)

y = [0.1, 0.05, 0.6,0.0,0.05,0.1,0.0,0.1,0.0,0.0]
t = [0,0,1,0,0,0,0,0,0,0]
print(mean_squared_error(np.array(y),np.array(t))) # 0.09750000000000003
y2 = [0.1, 0.05, 0.1,0.0,0.05,0.1,0.0,0.6,0.0,0.0]   
print(mean_squared_error(np.array(y2),np.array(t))) # 0.5975


# 交叉熵误差函数
def cross_entropy_error(y, t):
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

print(cross_entropy_error(np.array(y),np.array(t))) # 0.510825457099338
print(cross_entropy_error(np.array(y2),np.array(t))) # 2.302584092994546

三维图像

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Make data.
X = np.arange(-3, 3, 0.25)
Y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(X, Y)
# R = np.sqrt(X**2 + Y**2)
# Z = np.sin(R)
Z = X**2 + Y**2

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-0.01, 25.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

梯度


## mini-batch 训练

import sys, os

sys.path.append(os.getcwd())

from mnist import load_mnist
import numpy as np

(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)

train_size = x_train.shape[0]  # (60000, 784) 的第一个元素 60000
batch_size = 100

batch_mask = np.random.choice(train_size, batch_size)

x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]


def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)

    batch_size2 = y.shape[0]
    return -np.sum(t * np.log(y + 1e-7)) / batch_size2


def numeric_diff(f, x):
    h = 1e-4  # 0.0001
    return (f(x + h) - f(x - h)) / 2 * h

# 求梯度
def numeric_gradient(f, x):
    h = 1e-4  # 0.0001
    grad = np.zeros_like(x)  # 生成 和x形状相同的数组

    for idx in range(x.size):
        tmp_val = x[idx]

        # 计算f(x+h)
        x[idx] = tmp_val + h
        fxh1 = f(x)

        # 计算f(x-h)
        x[idx] = tmp_val - h
        fxh2 = f(x)

        grad[idx] = (fxh1 - fxh2) / (2 * h)
        x[idx] = tmp_val  # 还原值

    return grad


def function_2(x):
    return x[0] ** 2 + x[1] ** 2


print(numeric_gradient(function_2, np.array([3.0, 4.0]))) # [6. 8.]

print(numeric_gradient(function_2, np.array([3.0, 0.0]))) #  [6. 0.]

print(numeric_gradient(function_2, np.array([0.0, 2.0]))) # [0. 4.]

相关文章

  • 独家连载 | 超详细!带你走进单层感知器与线性神经网络

    第03章-单层感知器与线性神经网络 3.1生物神经网络 人工神经网络ANN的设计实际上是从生物体的神经网络结构获得...

  • 已写博客目录

    BP神经网络和SVM实现时间序列预测(2019-03-23) 配电网潮流计算(2019-03-23) 用CNN做电...

  • 神经网络03

    损失函数 三维图像 梯度

  • 七月-语音识别实战 百度网盘分享

    01.补充课 02.语音识别技术之前世: GMM + HMM 03.语音识别技术之今生:神经网络 04.第一课 语...

  • Chapter 03 神经网络

    略去一些有关神经网络和激活函数的相关概念,本人在读这本书的时候已有一定的基础。 简单的阶跃函数的实现 显然上述函数...

  • 七月 语音识别实战 百度网盘分享

    01.补充课02.语音识别技术之前世: GMM + HMM03.语音识别技术之今生:神经网络04.第一课 语音识别...

  • 神经网络(一)

    神经网络 1. 神经网络基础 2. 浅层神经网络分析 3. 卷积神经网络 4. 卷积神经网络MNIST数字图片识别...

  • 神经网络(二)

    神经网络 1. 神经网络基础 2. MNIST数据集浅层神经网络分析 3. 卷积神经网络 4. 卷积神经网络MNI...

  • 深层神经网络简单介绍(1)

    1/神经网络构架: 前向神经网络,典型代表CNN;循环神经网络:典型代表RNN;对称神经网络:典型代表DBN 2/...

  • 卷积神经网络 CNN(1)

    今天给大家介绍一下什么是卷积神经网络,以及神经网络是与普通的神经网络有什么不同。卷积神经网络是为了简化普通神经网络...

网友评论

      本文标题:神经网络03

      本文链接:https://www.haomeiwen.com/subject/xdnlyktx.html