【问题标题】:Why does my tanh activation function perform so badly?为什么我的 tanh 激活函数表现如此糟糕?
【发布时间】:2016-06-18 14:58:26
【问题描述】:

我有两个 Perceptron 算法,除了激活函数之外都相同。一个使用单步函数1 if u >= 0 else -1,另一个使用tanh 函数np.tanh(u)

我预计 tanh 的表现会优于 step,但相比之下它实际上表现得非常糟糕。我在这里做错了什么还是有原因它在问题集上表现不佳?

import numpy as np
import matplotlib.pyplot as plt

# generate 20 two-dimensional training data
# data must be linearly separable

# C1: u = (0,0) / E = [1 0; 0 1]; C2: u = (4,0), E = [1 0; 0 1] where u, E represent centre & covariance matrix of the
# Gaussian distribution respectively


def step(u):
    return 1 if u >= 0 else -1


def sigmoid(u):
    return np.tanh(u)

c1mean = [0, 0]
c2mean = [4, 0]
c1cov = [[1, 0], [0, 1]]
c2cov = [[1, 0], [0, 1]]
x = np.ones((40, 3))
w = np.zeros(3)     # [0, 0, 0]
w2 = np.zeros(3)    # second set of weights to see how another classifier compares
t = []  # target array

# +1 for the first 20 then -1
for i in range(0, 40):
    if i < 20:
        t.append(1)
    else:
        t.append(-1)

x1, y1 = np.random.multivariate_normal(c1mean, c1cov, 20).T
x2, y2 = np.random.multivariate_normal(c2mean, c2cov, 20).T

# concatenate x1 & x2 within the first dimension of x and the same for y1 & y2 in the second dimension
for i in range(len(x)):
    if i >= 20:
        x[i, 0] = x2[(i-20)]
        x[i, 1] = y2[(i-20)]
    else:
        x[i, 0] = x1[i]
        x[i, 1] = y1[i]

errors = []
errors2 = []
lr = 0.0001
n = 10

for i in range(n):
    count = 0
    for row in x:
        dot = np.dot(w, row)
        response = step(dot)
        errors.append(t[count] - response)
        w += lr * (row * (t[count] - response))
        count += 1

for i in range(n):
    count = 0
    for row in x:
        dot = np.dot(w2, row)
        response = sigmoid(dot)
        errors2.append(t[count] - response)
        w2 += lr * (row * (t[count] - response))
        count += 1

print(errors[-1], errors2[-1])

# distribution
plt.figure(1)
plt.plot((-(w[2]/w[0]), 0), (0, -(w[2]/w[1])))
plt.plot(x1, y1, 'x')
plt.plot(x2, y2, 'ro')
plt.axis('equal')
plt.title('Heaviside')

# training error
plt.figure(2)
plt.ylabel('error')
plt.xlabel('iterations')
plt.plot(errors)
plt.title('Heaviside Error')

plt.figure(3)
plt.plot((-(w2[2]/w2[0]), 0), (0, -(w2[2]/w2[1])))
plt.plot(x1, y1, 'x')
plt.plot(x2, y2, 'ro')
plt.axis('equal')
plt.title('Sigmoidal')

plt.figure(4)
plt.ylabel('error')
plt.xlabel('iterations')
plt.plot(errors2)
plt.title('Sigmoidal Error')

plt.show()

编辑:即使从我展示的误差图中,tanh 函数也显示出一些收敛性,因此可以合理地假设仅增加迭代次数或降低学习率就可以减少其误差。但是我想我真的在问,考虑到 step 函数的显着更好的性能,在什么问题集上使用 tanh 和感知器是可行的?

【问题讨论】:

  • 当您将lr 更改为 0.1 或 1 时,结果看起来几乎相同,因此您的学习率太小了。
  • 或者,您也可以增加n
  • 鉴于您的示例数据似乎是线性可分的,为什么您希望 sigmoidal 激活函数比阶跃函数做得更好?
  • @ali_m 有没有时候我应该使用 tanh,然后考虑到当数据是非线性可分时,感知的表现非常糟糕?

标签: python python-3.x numpy matplotlib machine-learning


【解决方案1】:

正如 cmets 中已经提到的,您的学习率太小,因此需要大量迭代才能收敛。因此,为了获得可比较的输出,您可以增加n 和/或lr

如果将lr 增加到例如0.1(也可以使用 1)和 n 到 10000,结果看起来几乎相同(见下图)和线

print(errors[-1], errors2[-1])

返回

(0, -8.4289020207961585e-11)

如果再次运行,这些值可能会有所不同,因为没有为随机数设置种子。

这是我为上述值得到的图:

【讨论】:

  • 我很好奇什么时候 tanh 的性能优于简单的步进函数?
  • 不确定我是否可以对此给出一般性答案。这样一个笼统的问题可能更适合cross validated。我的回答只是关于您发布并展示了如何修复它的这个特定示例。祝你好运! :)
猜你喜欢
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 2017-02-10
  • 2012-11-16
  • 1970-01-01
相关资源
最近更新 更多