更换数据源,使用mnist训练softmax模型,用代码回答,不用tensorflow
时间: 2024-06-10 14:05:20 浏览: 24
好的,我理解了您的要求,我可以回答您的问题。下面是使用mnist数据集训练softmax模型的代码示例:
```
import numpy as np
import urllib.request
import gzip
# load data from online source
def load_data():
url = 'http://yann.lecun.com/exdb/mnist/'
files = ['train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz',
't10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz']
data = []
for file_name in files:
print('Downloading:', url+file_name)
urllib.request.urlretrieve(url+file_name, file_name)
print('Download complete')
with gzip.open(file_name, 'rb') as f:
file_content = f.read()
if file_name.endswith('ubyte.gz'):
data.append(np.frombuffer(file_content, dtype=np.uint8, offset=16).reshape(-1, 784))
elif file_name.endswith('labels-idx1-ubyte.gz'):
data.append(np.frombuffer(file_content, dtype=np.uint8, offset=8))
return tuple(data)
# one-hot encode labels
def one_hot(labels):
n_labels = len(labels)
n_classes = len(np.unique(labels))
onehot_labels = np.zeros((n_labels, n_classes))
onehot_labels[np.arange(n_labels), labels] = 1
return onehot_labels
# softmax function
def softmax(y):
exp_y = np.exp(y - np.max(y))
return exp_y / np.sum(exp_y, axis=1, keepdims=True)
# cross-entropy loss
def cross_entropy_loss(y_pred, y_true):
n_samples = y_pred.shape[0]
loss = -np.sum(y_true * np.log(y_pred+1e-12)) / n_samples
return loss
# train softmax model
def train_softmax(X_train, y_train, alpha=0.1, epochs=500):
n_samples, n_features = X_train.shape
n_classes = y_train.shape[1]
W = np.random.randn(n_features, n_classes) * 0.01
b = np.zeros((1, n_classes))
for i in range(epochs):
y_pred = softmax(np.dot(X_train, W) + b)
loss = cross_entropy_loss(y_pred, y_train)
if i % 100 == 0:
print('Epoch %d loss: %.4f' % (i, loss))
dW = (1 / n_samples) * np.dot(X_train.T, (y_pred - y_train))
db = (1 / n_samples) * np.sum(y_pred - y_train, axis=0, keepdims=True)
W -= alpha * dW
b -= alpha * db
return W, b
# test softmax model
def test_softmax(X_test, y_test, W, b):
y_pred = softmax(np.dot(X_test, W) + b)
accuracy = np.mean(np.argmax(y_pred, axis=1) == np.argmax(y_test, axis=1))
return accuracy
# load data
X_train, y_train, X_test, y_test = load_data()
X_train = X_train / 255.0
X_test = X_test / 255.0
y_train = one_hot(y_train)
y_test = one_hot(y_test)
# train model
W, b = train_softmax(X_train, y_train)
# test model
accuracy = test_softmax(X_test, y_test, W, b)
print('Accuracy:', accuracy)
```
这是一个使用Numpy实现的softmax分类器,对mnist数据集进行训练与测试。其中`load_data`函数用于从在线源加载mnist数据集,`one_hot`函数用于对标签进行One-Hot编码,`softmax`函数计算softmax输出,`cross_entropy_loss`函数计算交叉熵损失,`train_softmax`函数用于训练softmax模型,`test_softmax`函数用于测试softmax模型。在代码中使用的学习率`alpha`为0.1,训练轮数`epochs`为500。运行以上代码可以得到模型的测试准确率,这个准确率可能并不是很高,因为这是一个比较简单的模型。但是,它可以作为使用mnist数据集训练softmax模型的例子,供您参考。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)