#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 20:30:39 2018
@author: nanzheng
"""
import numpy as np
import matplotlib.pyplot as plt
from cnn_operations import cnn_operations as cnn_opr
class cnn():
"""
卷积神经网络。
"""
def __init__(self):
# 网络的层数
self.n_layers = 0
# list,网络中的各层
self.layers = []
# array,网络的输出
self.output = None
# 网络的代价
self.loss = None
# 权值的学习率
self.learning_rate_weight = 0.1
# 偏置的学习率
self.learning_rate_bias = 0.1
def config(self, args):
"""
配置网络。
Parameters
----------
args: tuple,args[i][0]为第i层的类型,{"input", "convoluting", "pooling",
"full_connecting", "output"},args[i][1]为tuple,第i层的配置参数
"""
self.n_layers = len(args)
prior_layer = None
for i in range(self.n_layers):
# 配置网络的各层
new_layer = cnn_layer(args[i][0])
if i > 0:
prior_layer = self.layers[-1]
# 当前层设为上一层的next_layer
self.layers[-1].next_layer = new_layer
new_layer.config(args[i][1], self.learning_rate_weight,
self.learning_rate_bias, prior_layer)
self.layers.append(new_layer)
return None
def _feed_forward(self, x):
"""
前向传播。
Parameters
----------
x: 3-d array,一个batch的输入图像,
每个通道的尺寸为x.shape[0] * x.shape[1],
x.shape[2]为当前batch中图像的个数 * 每幅图像的通道数
"""
# 输入层前向传播
self.layers[0].feed_forward(x)
# 其它各层前向传播
for i in range(1, self.n_layers):
self.layers[i].feed_forward(x)
# self.layers[-1].n_nodes * size_batch array,网络的输出
self.output = np.ndarray.flatten( \
np.array(self.layers[-1].output)).reshape( \
self.layers[-1].n_nodes, -1)
return None
def _back_propagate(self, y):
"""
反向传播。
Parameters
----------
y: array,输入样本对应的类别标签
"""
# 输出层反向传播
self.layers[-1].back_propagate(y)
# 其它各层反向传播
for i in range(self.n_layers - 2, 0, -1):
self.layers[i].back_propagate()
return None
def fit(self, X, Y, size_batch=1, n_epochs=1):
"""
训练。
下降方式为随机梯度下降。
Parameters
----------
X: 3-d array,训练集,
X[:, :, i: i + self.layers[0].n_nodes]为一个训练样本(图片),
self.layers[0].n_nodes即为每幅图片的通道数
Y: array,训练集对应的类别标签
size_batch: 一个batch中训练样本的个数
n_epochs: 迭代次数
"""
self.size_batch = size_batch
# 训练样本个数 * 每幅图片的通道数
len_X = X.shape[-1]
len_Y = Y.shape[0]
# 每个epoch中batch的个数
n_batches = int(np.ceil(len_X / self.layers[0].n_nodes / size_batch))
loss = np.empty(n_epochs * n_batches)
for i_epoch in range(n_epochs):
print("Epoch: ", end="")
print(i_epoch)
for i_batch in range(n_batches):
print("\tBatch: ", end="")
print(i_batch, end="\t")
y_offset = i_batch * size_batch
x_offset = y_offset * self.layers[0].n_nodes
# 将类别标签转换为向量
y = np.zeros([self.layers[-1].n_nodes, size_batch])
for i in range(size_batch):
if i > len_Y - y_offset - 1:
y = y[:, :, : i]
break
y[Y[y_offset + i], i] = 1
self._feed_forward(X[:, :, x_offset: x_offset + size_batch * \
self.layers[0].n_nodes])
loss[i_epoch * n_batches + i_batch] = \
cnn_opr.calc_loss(y.T, self.output.T)
print("loss = ", end="")
print(loss[i_epoch * n_batches + i_batch])
self._back_propagate(y)
self.loss = loss
plt.figure()
plt.plot(loss, "r-")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.grid()
plt.show()
return None
def test(self, X, Y):
"""
验证。
Parameters
----------
X: 3-d array,验证集,
X[:, :, i: i + self.layers[0].n_nodes]为一个验证样本(图片),
self.layers[0].n_nodes即为每幅图片的通道数
Y: array,训练集对应的类别标签
Returns
-------
correct_rate: 验证集的分类正确率
"""
n_correct = 0
for i in range(0, X.shape[-1], self.layers[0].n_nodes):
print("Test case: ", end="")
print(i)
y_predict = self.predict(X[:, :, i: i + self.layers[0].n_nodes])
if y_predict == Y[i]:
n_correct += 1
correct_rate = n_correct / X.shape[-1]
return correct_rate
def predict(self, x):
"""
预测。
Parameters
----------
x: 2-d或3-d array,输入样本(图像)
Returns
-------
y_predict: 预测出的输入样本(图像)的类别
"""
self._feed_forward(x.reshape(x.shape[0], x.shape[1], -1))
# 根据网络输出层的类型,判定输入图像的类别
if self.layers[-1].type_output is "softmax":
y_predict = np.argmax(self.output[:, 0])
elif self.layers[-1].type_output is "rbf":
# TODO:
pass
return y_predict
class cnn_layer():
"""
卷积神经网络中的一层。
"""
def __init__(self, type_layer):
"""
Parameters
----------
type_layer: 当前层的类型,{"input", "convoluting", "pooling",
"full_connecting", "output"}
"""
# 当前层的类型
self.type = type_layer
# 当前层中神经元的个数
self.n_nodes = 0
# list,当前层中各神经元
self.nodes = []
# 当前层的上一层
self.prior_layer = None
# 当前层的下一层
self.next_layer = None
# list,当前层的输出
self.output = []
# 权值的学习率
self.learning_rate_weight = 0.0
# 偏置的学习率
self.learning_rate_bias = 0.0
if self.type is "input":
# array,输入图像(每个通道)的尺寸
self.size_input = None
elif self.type is "convoluting":
# 2-d array,当前层与上一层各神经元的连接矩阵
self.connecting_matrix = None
# array,卷积核尺寸
self.size_conv_kernel = None
python卷积神经网络实现
1星 需积分: 44 86 浏览量
2019-05-22
10:56:59
上传
评论 10
收藏 31KB ZIP 举报
qq_36784544
- 粉丝: 81
- 资源: 224
最新资源
- 电力场景设备漏油检测数据集VOC+YOLO格式338张1类别.7z
- 基于yolov8+pyqt5实现精美界面支持图片视频和摄像检测源码.zip
- 用C语言为母亲节献上一份特别的祝福.zip
- LCD1602液晶显示屏的深入探索与实用指南.zip
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈