Tom*_*nar 5 python numpy backpropagation neural-network
我创建了以下神经网络:
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)
Run Code Online (Sandbox Code Playgroud)
数据有10个类。当使用单个隐藏层时,准确率几乎为 40%。当使用 2 或 3 个隐藏层时,准确率大约是第一个 epoch 的 9-10%,并且仍然如此。训练集上的准确率也在这个范围内。我的实现是否存在可能导致这种情况的问题?
您询问了机器学习模型的准确性提升问题,这是 ML 时代一个非常广泛且模糊的问题,因为它在各种模型类型和数据类型之间存在差异
在您的情况下,模型是神经网络,它有几个依赖于准确性的因素。您试图根据激活函数、权重或隐藏层数量来优化准确性,这不是正确的方法。为了提高准确性,您还必须考虑其他因素,例如您的基本清单可以如下
现在您正试图在很少的因素的基础上实现最先进的准确性,我不知道您的数据集,因为您没有显示预处理代码,但我建议您仔细检查数据集可能是正确规范化数据集可以提高准确性,还可以检查数据集是否可以缩放,最重要的是,如果数据集中的某个类样本与其他样本相比过载或计数太大,那么它也会导致精度较差的矩阵。
有关更多详细信息,请检查此内容,其中包含数学证明和解释这些因素如何影响您的 ML 模型准确性
| 归档时间: |
|
| 查看次数: |
252 次 |
| 最近记录: |