深度學習實驗:Softmax實現手寫數字識別
import numpy as np
from layers import FCLayer
from dataloader import build_dataloader
from network import Network
from optimizer import SGD
from loss import SoftmaxCrossEntropyLoss
from visualize import plot_loss_and_acc
class Solver(object):
def __init__(self, cfg):
self.cfg = cfg
# build dataloader
train_loader, val_loader, test_loader = self.build_loader(cfg)
self.train_loader = train_loader
self.val_loader = val_loader
self.test_loader = test_loader
# build model
self.model = self.build_model(cfg)
# build optimizer
self.optimizer = self.build_optimizer(self.model, cfg)
# build evaluation criterion
self.criterion = SoftmaxCrossEntropyLoss()
@staticmethod
def build_loader(cfg):
train_loader = build_dataloader(
cfg['data_root'], cfg['max_epoch'], cfg['batch_size'], shuffle=True, mode='train')
val_loader = build_dataloader(
cfg['data_root'], 1, cfg['batch_size'], shuffle=False, mode='val')
test_loader = build_dataloader(
cfg['data_root'], 1, cfg['batch_size'], shuffle=False, mode='test')
return train_loader, val_loader, test_loader
@staticmethod
def build_model(cfg):
model = Network()
model.add(FCLayer(784, 10))
return model
@staticmethod
def build_optimizer(model, cfg):
return SGD(model, cfg['learning_rate'], cfg['momentum'])
def train(self):
max_epoch = self.cfg['max_epoch']
epoch_train_loss, epoch_train_acc = [], []
for epoch in range(max_epoch):
iteration_train_loss, iteration_train_acc = [], []
for iteration, (images, labels) in enumerate(self.train_loader):
# forward pass
logits = self.model.forward(images)
loss, acc = self.criterion.forward(logits, labels)
# backward_pass
delta = self.criterion.backward()
self.model.backward(delta)
# updata the model weights
self.optimizer.step()
# restore loss and accuracy
iteration_train_loss.append(loss)
iteration_train_acc.append(acc)
# display iteration training info
if iteration % self.cfg['display_freq'] == 0:
print("Epoch [{}][{}]\t Batch [{}][{}]\t Training Loss {:.4f}\t Accuracy {:.4f}".format(
epoch, max_epoch, iteration, len(self.train_loader), loss, acc))
avg_train_loss, avg_train_acc = np.mean(iteration_train_loss), np.mean(iteration_train_acc)
epoch_train_loss.append(avg_train_loss)
epoch_train_acc.append(avg_train_acc)
# validate
avg_val_loss, avg_val_acc = self.validate()
# display epoch training info
print('\nEpoch [{}]\t Average training loss {:.4f}\t Average training accuracy {:.4f}'.format(
epoch, avg_train_loss, avg_train_acc))
# display epoch valiation info
print('Epoch [{}]\t Average validation loss {:.4f}\t Average validation accuracy {:.4f}\n'.format(
epoch, avg_val_loss, avg_val_acc))
return epoch_train_loss, epoch_train_acc
def validate(self):
logits_set, labels_set = [], []
for images, labels in self.val_loader:
logits = 外匯跟單gendan5.comself.model.forward(images)
logits_set.append(logits)
labels_set.append(labels)
logits = np.concatenate(logits_set)
labels = np.concatenate(labels_set)
loss, acc = self.criterion.forward(logits, labels)
return loss, acc
def test(self):
logits_set, labels_set = [], []
for images, labels in self.test_loader:
logits = self.model.forward(images)
logits_set.append(logits)
labels_set.append(labels)
logits = np.concatenate(logits_set)
labels = np.concatenate(labels_set)
loss, acc = self.criterion.forward(logits, labels)
return loss, acc
if __name__ == '__main__':
# You can modify the hyerparameters by yourself.
relu_cfg = {
'data_root': 'data',
'max_epoch': 10,
'batch_size': 100,
'learning_rate': 0.1,
'momentum': 0.9,
'display_freq': 50,
'activation_function': 'relu',
}
runner = Solver(relu_cfg)
relu_loss, relu_acc = runner.train()
test_loss, test_acc = runner.test()
print('Final test accuracy {:.4f}\n'.format(test_acc))
# You can modify the hyerparameters by yourself.
sigmoid_cfg = {
'data_root': 'data',
'max_epoch': 10,
'batch_size': 100,
'learning_rate': 0.1,
'momentum': 0.9,
'display_freq': 50,
'activation_function': 'sigmoid',
}
runner = Solver(sigmoid_cfg)
sigmoid_loss, sigmoid_acc = runner.train()
test_loss, test_acc = runner.test()
print('Final test accuracy {:.4f}\n'.format(test_acc))
plot_loss_and_acc({
"relu": [relu_loss, relu_acc],
"sigmoid": [sigmoid_loss, sigmoid_acc],
})
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2907883/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Get】用深度學習識別手寫數字深度學習
- 【TensorFlow篇】--Tensorflow框架實現SoftMax模型識別手寫數字集框架模型
- Pytorch 手寫數字識別 深度學習基礎分享PyTorch深度學習
- Tensorflow實現RNN(LSTM)手寫數字識別RNN
- 機器學習之神經網路識別手寫數字(純python實現)機器學習神經網路Python
- Pytorch搭建MyNet實現MNIST手寫數字識別PyTorch
- OpenCV + sklearnSVM 實現手寫數字分割和識別OpenCV
- 【機器學習】手寫數字識別機器學習
- 深度學習例項之基於mnist的手寫數字識別深度學習
- 用tensorflow2實現mnist手寫數字識別
- 在PaddlePaddle上實現MNIST手寫體數字識別
- CNN實現手寫數字識別並改變引數進行分析CNN
- mnist手寫數字識別——深度學習入門專案(tensorflow+keras+Sequential模型)深度學習Keras模型
- 《手寫數字識別》神經網路 學習筆記神經網路筆記
- KNN 演算法-實戰篇-如何識別手寫數字KNN演算法
- TensorFlow.NET機器學習入門【5】採用神經網路實現手寫數字識別(MNIST)機器學習神經網路
- keras框架下的深度學習(一)手寫體識別Keras框架深度學習
- 深度學習-行人重識別實戰(2020)深度學習
- TensorFlow 實戰Google深度學習框架(第2版)第6章之LeNet-5模型實現MNIST數字識別Go深度學習框架模型
- 手把手教你使用LabVIEW OpenCV DNN實現手寫數字識別(含原始碼)ViewOpenCVDNN原始碼
- tensorflow.js 手寫數字識別JS
- 小熊飛槳練習冊-01手寫數字識別
- 使用 Fantom 程式語言實現英文數字驗證碼識別
- 《動手學深度學習》TF2.0 實現深度學習TF2
- 利用OpenCV和深度學習來實現人類活動識別OpenCV深度學習
- 實戰四:手把手教你實現數字識別
- 【Keras篇】---Keras初始,兩種模型構造方法,利用keras實現手寫數字體識別Keras模型構造方法
- 深度學習——手動實現殘差網路ResNet 辛普森一家人物識別深度學習
- 實驗16-使用GAN生成手寫數字樣本
- 瀏覽器中的手寫數字識別瀏覽器
- Tensorflow2.0-mnist手寫數字識別示例
- 深度學習——性別識別深度學習
- Opencv學習筆記(3)---紙牌數字識別練習實踐專案OpenCV筆記
- 機器學習從入門到放棄:硬train一發手寫數字識別機器學習AI
- 5步實現深度學習OpenCV物件檢測:Faster-RCNN圖片識別深度學習OpenCV物件ASTCNN
- 卷積神經網路CNN實戰:MINST手寫數字識別——網路定義卷積神經網路CNN
- 機器學習實戰-SVM模型實現人臉識別機器學習模型
- 【opencv3】 svm實現手寫體與人臉識別OpenCV