实现简单的sgd算法应用
时间: 2024-01-24 19:04:26 浏览: 60
以下是一个简单的SGD算法应用,假设我们有一个二分类问题,需要通过SGD算法训练一个逻辑回归模型来对样本进行分类。
首先,我们可以使用sklearn库生成随机样本数据:
```python
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)
```
接下来,我们可以使用train_test_split函数将数据划分为训练集和测试集:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
然后,我们可以定义逻辑回归模型的损失函数和梯度函数,并使用SGD算法进行模型训练:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def loss_fn(w, b, X, y):
y_pred = sigmoid(np.dot(X, w) + b)
loss = -(np.mean(y * np.log(y_pred) + (1 - y) * np.log(1 - y_pred)))
return loss
def grad_fn(w, b, X, y):
y_pred = sigmoid(np.dot(X, w) + b)
grad_w = np.dot(X.T, (y_pred - y)) / len(y)
grad_b = np.mean(y_pred - y)
return grad_w, grad_b
def sgd(X, y, lr=0.01, batch_size=32, epochs=100):
n, m = X.shape
w = np.zeros(m)
b = 0.0
for epoch in range(epochs):
indices = np.random.permutation(n)
for i in range(0, n, batch_size):
X_batch = X[indices[i:i+batch_size]]
y_batch = y[indices[i:i+batch_size]]
grad_w, grad_b = grad_fn(w, b, X_batch, y_batch)
w -= lr * grad_w
b -= lr * grad_b
loss = loss_fn(w, b, X, y)
print('Epoch %d, Loss %.4f' % (epoch+1, loss))
return w, b
```
最后,我们可以使用训练好的模型对测试集进行预测,并计算预测准确率:
```python
w, b = sgd(X_train, y_train, lr=0.01, batch_size=32, epochs=100)
y_pred = np.round(sigmoid(np.dot(X_test, w) + b))
accuracy = np.mean(y_pred == y_test)
print('Accuracy:', accuracy)
```
通过适当调节学习率、批量大小和迭代次数等参数,可以得到较好的分类效果。
阅读全文