我创建了以下神经网络:
def init_weights(m, n=1):
"""
initialize a matrix/vector of weights with xavier initialization
:param m: out dim
:param n: in dim
:return: matrix/vector of random weights
"""
limit = (6 / (n * m)) ** 0.5
weights = np.random.uniform(-limit, limit, size=(m, n))
if n == 1:
weights = weights.reshape((-1,))
return weights
def softmax(v):
exp = np.exp(v)
return exp / np.tile(exp.sum(1), (v.shape[1], 1)).T
def relu(x):
return np.maximum(x, 0)
def sign(x):
return (x > 0).astype(int)
class Model:
"""
A class for neural network model
"""
def __init__(self, sizes, lr):
self.lr = lr
self.weights = []
self.biases = []
self.memory = []
for i in range(len(sizes) - 1):
self.weights.append(init_weights(sizes[i + 1], sizes[i]))
self.biases.append(init_weights(sizes[i + 1]))
def forward(self, X):
self.memory = [X]
X = np.dot(self.weights[0], X.T).T + self.biases[0]
for W, b in zip(self.weights[1:], self.biases[1:]):
X = relu(X)
self.memory.append(X)
X = np.dot(W, X.T).T + b
return softmax(X)
def backward(self, y, y_pred):
# calculate the errors for each layer
y = np.eye(y_pred.shape[1])[y]
errors = [y_pred - y]
for i in range(len(self.weights) - 1, 0, -1):
new_err = sign(self.memory[i]) * \
np.dot(errors[0], self.weights[i])
errors.insert(0, new_err)
# update weights
for i in range(len(self.weights)):
self.weights[i] -= self.lr *\
np.dot(self.memory[i].T, errors[i]).T
self.biases[i] -= self.lr * errors[i].sum(0)数据有10个类。当使用单个隐藏层时,准确率接近40%。当使用2或3个隐藏层时,精度从第一个纪元开始约为9-10%,并保持不变。训练集上的精度也在这个范围内。我的实现中有没有可能导致这种情况的问题?
发布于 2021-05-21 16:03:37
我会试着用简单的语言来解释。你使用的是一个无界线性误差函数。当你增加隐藏层时,这不会有任何好处,因为任何线性函数的组合仍然是线性的,现在你有更多的权重来优化相同数量的数据。让问题变得更糟的是,你已经重现了容易消失的梯度现象。尝试将线性误差函数替换为非线性函数,如交叉熵或负对数损失,并将relu替换为泄漏relu。因为你还没有分享你正在使用的数据,所以我不会评论你是否应该使用多个隐藏层。首先,更多的隐藏层并不能保证精确度的提高。
发布于 2021-05-22 16:26:11
您询问了机器学习模型的准确性改进,这在ML时代是一个非常广泛和模糊的问题,因为它在不同的模型类型和数据类型之间有所不同
在您的例子中,模型是神经网络,它具有几个依赖于准确性的因素。你试图根据激活函数、权重或隐藏层的数量来优化精度,这不是正确的方法。为了提高准确性,你还必须考虑其他因素,例如,你的基本核对表可以如下
使用初始权重增加班级的隐藏层和变更激活Functions
Class Biasness
现在你正试图在很少的因素的基础上达到最先进的准确性,我不知道你的数据集,因为你还没有显示预处理代码,但我建议你仔细检查数据集可能是通过正确地归一化数据集来提高准确性,也检查你的数据集是否可以缩放,最重要的是,如果你的数据集中的一个类样本超载或与其他样本相比计数太大,那么它也会导致较差的精度矩阵。
有关更多详细信息,请查看this,其中包含数学证明和这些内容如何影响ML模型精度的说明
https://stackoverflow.com/questions/67521507
复制相似问题