S形函数在神经网络中的作用

时间:2018-04-23 08:59:53

标签: python machine-learning neural-network

我试图理解sigmoid函数的导数在神经网络中的作用。 enter image description here

首先我绘制sigmoid函数,并使用python从定义中导出所有点。这个衍生物的作用究竟是什么? enter image description here

internal class Program
{
    public static void Main()
    {
        const string dataToEncrypt = "hello";
        var encryptedWithPublicKey = RSAEncryptionService.EncryptWithPublicKey(dataToEncrypt);
        var decryptWithPrivateKey = RSAEncryptionService.DecryptWithPrivateKey(encryptedWithPublicKey);
    }
}

public static class RSAEncryptionService
{
    private const string PublicKeyBase64 =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLDfiAR2Z1bHyYT5haopn/KDgKQ3x1wKrVcEx+qBHJxvahh1v1bzQ2flNB/TQMIzy5xiBA5/7vJb8H8RWw5anY6Xr6sRCCSpft6jZ60c1TSxOpCP5OFsG7WJLZPdECk2PBNLQ/SoB7Iw4Ku8WRioYxqWLlY4nXYlaGUnd/2XhpQwIDAQAB";

    private const string PrivateKeyBase64 =
        "MIICXAIBAAKBgQDKVSB9hQVPFH9DCrUckcXLLNeLIXyMs8KXwBR42RG9LCFXcKx1u0W0CbLx5X3p+nCojfj/NU9aOMPXW8Vv4DqzHr5E5FeMXv31tBjxTEIFz+gia1tbnsaGklZSoBykQC3PfnYUDNg9pNl4m3hZsO6F1hgeMPLS6ZWu+JkP0p0YwQIDAQABAoGADjnuDuVLekP2rsAR+/BD+AUvIpk4KKx7AjB/QAADP0nbDkrd1WlVfVMxQdDhCZKSWUeiOk+FRNK48muBdlxNxCpAgr7Qh8SF0w286eRzmo2VlQjz9XurTpO2uIkhf/NP3TTkuQwHRkjrDfJWCaFObHRBJoOFO9Q516cZs7zagAECQQD5xmR7KXz1rw1XA7RnV4OSlufYSG6Kn/71MjHXtmlA8RvODhUjcbfEXCvCZQPEPkyjUt5lsLtCP5HwleCvvVmJAkEAz2ALHXFABP9gKmsJQ0cCLGu//T2t/P7Zci4dPElIECLlM9rG3rdIiGpAjGjkoK91fnWKM36SrwV0FwxcGvjPeQJAZ7bekFmo3qeChYgvR94J7zukJ6Xj6MII7y2ZjrXavLGhVw1375DPusR/Eyg4ySsJTaQiXt1k1FxrMBg8BR096QJAS6XKiDP+2ZPbHb4sA9nMZi3sPFIavRaudeNykMJewlwoZlEhiK8ti23PHClLdX5sCvSPtJcwpR4FKj88/i3TaQJBAOwlsbPKdPLuxtFuue/XlUfVsJd7hpe+ma5qNJuwNTuFZHjbH373b3IAuNlAA1KLQs53Fh4bbdOnu97gRjgvT9c=";

    public static string EncryptWithPublicKey(string data)
    {
        var publicKeyBytes = Convert.FromBase64String(PublicKeyBase64);
        var asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);
        var rsaKeyParameters = (RsaKeyParameters) asymmetricKeyParameter;
        var rsaParameters = new RSAParameters
        {
            Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(),
            Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned()
        };

        var toEncryptBytes = Encoding.UTF8.GetBytes(data);
        using (var rsa = new RSACryptoServiceProvider(2048))
        {
            try
            {
                rsa.ImportParameters(rsaParameters);
                var encryptedData = rsa.Encrypt(toEncryptBytes, true);
                return Convert.ToBase64String(encryptedData);
            }
            finally
            {
                rsa.PersistKeyInCsp = false;
            }
        }
    }

    public static string DecryptWithPrivateKey(string base64Encrypted)
    {
        var privateKeyBytes = Convert.FromBase64String(PrivateKeyBase64);
        var asymmetricKeyParameter = PrivateKeyFactory.CreateKey(privateKeyBytes); // fails here :(
        var rsaKeyParameters = (RsaKeyParameters) asymmetricKeyParameter;
        var rsaParameters = new RSAParameters
        {
            Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(),
            Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned()
        };

        using (var rsa = new RSACryptoServiceProvider(2048))
        {
            try
            {
                rsa.ImportParameters(rsaParameters);
                var resultBytes = Convert.FromBase64String(base64Encrypted);
                var decryptedBytes = rsa.Decrypt(resultBytes, false);
                var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
                return decryptedData;
            }
            finally
            {
                rsa.PersistKeyInCsp = false;
            }
        }
    }
}

但我发现了这个:

enter image description here

来源:http://www.ai.mit.edu/courses/6.892/lecture8-html/sld015.htm

当我绘制这个衍生物的结果时,我得到了

enter image description here

完全是另一个情节。为什么?这些衍生物有什么区别?

第二个剧情

import numpy as np
import matplotlib.pyplot as plt

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

def derivative(x, step):
    return (sigmoid(x+step) - sigmoid(x)) / step

x = np.linspace(-10, 10, 1000)

y1 = sigmoid(x)
y2 = derivative(x, 0.0000000000001)

plt.plot(x, y1, label='sigmoid')
plt.plot(x, y2, label='derivative')
plt.legend(loc='upper left')
plt.show()

2 个答案:

答案 0 :(得分:1)

看来你的情节不正确。这应该给你正确的情节。

import numpy as np
import matplotlib.pyplot as plt

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

def derivative(x, step):
    return (sigmoid(x+step) - sigmoid(x)) / step

def other_derivative(x):
    y = [(1/(1 + np.exp(-i))) * (1 - (1 /(1 + np.exp(-i)))) for i in x]
    return y

x = np.linspace(-10, 10, 1000)

y1 = sigmoid(x)
y2 = derivative(x, 0.0000000000001)
y3 = other_derivative(x)

plt.plot(x, y1, label='sigmoid')
plt.plot(x, y2, label='derivative')
plt.plot(x, y3, label='other derivative')
plt.legend(loc='upper left')
plt.show()

enter image description here

答案 1 :(得分:1)

sigmoid函数导数的公式由s(x) * (1 - s(x))给出,其中s是sigmoid函数。

S形函数的优点是它的导数非常容易计算 - 它与原始函数有关。

def __sigmoid_derivative(x):
    return sigmoid(x) * (1 - sigmoid(x))

所以你有

enter image description here

你引用的另一种选择,即

def __sigmoid_derivative(x):
    return x * (1 - x)

假设x已经是sigmoid函数的输出,因此不会在第二次重新计算。