Python如何在神经网络中绘制错误

时间:2017-11-02 20:17:05

标签: python matplotlib machine-learning

我正在从http://iamtrask.github.io/2015/07/27/python-network-part2/博客学习神经网络。我想使用matplotlib绘制错误,如图所示。

enter image description here

我该怎么做?我尝试将数据存储在列表中,但我的解决方案却无法正常工作。 来自trask博客:

  

让我们尝试绘制错误平面的样子   上面的网络/数据集。那么,我们如何计算给定集合的误差   重量?第31,32和35行向我们展示了这一点。如果我们采取这种逻辑和   绘制整体错误(表示网络错误的单个标量)   在整个数据集上)为每一组可能的权重(从-10   对于x和y而言为10,它看起来像这样。

import numpy as np
import matplotlib as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

# 2 layer neural network

def sigmoid(x):
    output = 1 / (1+np.exp(-x))
    return output


def sigmoid_output_to_derivative(output):
    return output*(1-output)


X = np.array([
    [0,1],
    [0,1],
    [1,0],
    [1,0]
])

y = np.array([[0, 0, 1, 1]]).T

np.random.seed(1)

synapse_0 = 2*np.random.random((2, 1)) - 1

data = list()

for iter in xrange(1000):

    layer_0 = X
    layer_1 = sigmoid(np.dot(layer_0, synapse_0))

    layer_1_error = layer_1 - y

    layer_1_delta = layer_1_error * sigmoid_output_to_derivative(layer_1)
    synapse_0_deriative = np.dot(layer_0.T, layer_1_delta)

    synapse_0 -= synapse_0_deriative

    data.append(np.array([layer_0, layer_1, layer_1_error]))

    print "Error: {}".format(layer_1_error)

fig = plt.figure()
ax = fig.gca(projection='3d')

# x,y,z,c = data

print data


# surf = ax.plot_surface(x,y,z, cmap=cm.coolwarm,
#                        linewidth=0, antialiased=False)

编辑:

我尝试:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random

def sigmoid(x):
    output = 1 / (1+np.exp(-x))
    return output


def sigmoid_output_to_derivative(output):
    return output*(1-output)

X = np.array([
    [0,1],
    [0,1],
    [1,0],
    [1,0]
])

y = np.array([[0, 0, 1, 1]]).T

np.random.seed(1)

synapse_0 = 2*np.random.random((2, 1)) - 1

layer_1_error = ""

errors_sum = np.array([])

for iter in xrange(12):

    layer_0 = X
    layer_1 = sigmoid(np.dot(layer_0, synapse_0))

    layer_1_error = layer_1 - y

    layer_1_delta = layer_1_error * sigmoid_output_to_derivative(layer_1)
    synapse_0_deriative = np.dot(layer_0.T, layer_1_delta)

    synapse_0 -= synapse_0_deriative

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_surface(range(-10, 10), range(-10, 10), layer_1_error,  linewidth=0, antialiased=False)
plt.show()

结果:

enter image description here

我不知道如何收集for循环中的所有layer_1_error

1 个答案:

答案 0 :(得分:1)

根据突触权重绘制误差曲面所需的是改变权重并评估每个组合的平均误差。在这里,您可以找到代码草图:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np

def sigmoid(x):
    output = 1.0 / (1.0 + np.exp(-x))
    return output

def sigmoid_output_to_derivative(output):
    return output*(1-output)

X = np.array([
    [0,1],
    [0,1],
    [1,0],
    [1,0]
])
y = np.array([[0, 0, 1, 1]]).T

synapse_0 = np.empty((2,1))

# the error aggregation starts here
x_range = np.linspace(-10, 10, 20, dtype=np.float)
y_range = np.linspace(-10, 10, 20, dtype=np.float)
errors = []
for _x in x_range:
    synapse_0[0] = _x
    for _y in y_range:
        synapse_0[1] = _y

        # apply the model to the input
        layer_0 = X
        layer_1 = sigmoid(np.dot(layer_0, synapse_0))

        # evaluate the error using the RMSE
        error = np.mean(np.sqrt((layer_1 - y) ** 2))
        errors.append(error)

# in order to plot we need to transform x,y and z in 2D array 
error_surface = np.reshape(np.array(errors), (x_range.shape[0], y_range.shape[0]))
_X, _Y = np.meshgrid(x_range, y_range, indexing='ij')

# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(_X, _Y, error_surface, cmap=cm.YlOrBr_r, edgecolor='gray', linewidth=0.004, antialiased=False)
plt.show()

结果图如下: enter image description here