Chainer是由日本PreferredNetworks公司推出的一套深度学习框架,支持各领域深度学习研究和应用开发的高性能实现。内部提供了与Numpy类似的API,通过CuPy来调用GPUs的运算能力,支持通用和动态图模型,并提供了计算机视觉和分布式训练的强大功能包。
# copy from:https://docs.chainer.org/en/stable/examples/cnn.html
import chainer.links as L
import chainer.functions as F
class LeNet5(Chain): #继成Chain类
def __init__(self):
super(LeNet5, self).__init__()
with self.init_scope():
self.conv1 = L.Convolution2D(
in_channels=1, out_channels=6, ksize=5, stride=1)
self.conv2 = L.Convolution2D(
in_channels=6, out_channels=16, ksize=5, stride=1)
self.conv3 = L.Convolution2D(
in_channels=16, out_channels=120, ksize=4, stride=1)
self.fc4 = L.Linear(None, 84)
self.fc5 = L.Linear(84, 10) #定义各种操作函数
def forward(self, x): #定义序列模型,前向传播函数
h = F.sigmoid(self.conv1(x))
h = F.max_pooling_2d(h, 2, 2)
h = F.sigmoid(self.conv2(h))
h = F.max_pooling_2d(h, 2, 2)
h = F.sigmoid(self.conv3(h))
h = F.sigmoid(self.fc4(h))
if chainer.config.train:
return self.fc5(h)
return F.softmax(self.fc5(h))
##-----------也可以直接调用Chain的API来使用----------##
import chainer.links as L
from functools import partial
class LeNet5(Chain):
def __init__(self):
super(LeNet5, self).__init__()
net = [('conv1', L.Convolution2D(1, 6, 5, 1))]
net += [('_sigm1', F.sigmoid)]
net += [('_mpool1', partial(F.max_pooling_2d, ksize=2, stride=2))]
net += [('conv2', L.Convolution2D(6, 16, 5, 1))]
net += [('_sigm2', F.sigmoid)]
net += [('_mpool2', partial(F.max_pooling_2d, ksize=2, stride=2))]
net += [('conv3', L.Convolution2D(16, 120, 4, 1))]
net += [('_sigm3', F.sigmoid)]
net += [('_mpool3', partial(F.max_pooling_2d, ksize=2, stride=2))]
net += [('fc4', L.Linear(None, 84))]
net += [('_sigm4', F.sigmoid)]
net += [('fc5', L.Linear(84, 10))]
net += [('_sigm5', F.sigmoid)]
with self.init_scope():
for n in net:
if not n[0].startswith('_'):
setattr(self, n[0], n[1])
self.layers = net #直接定义前传的操作序列
## 注,定义模型也可以使用Sequential的形式:https://docs.chainer.org/en/stable/reference/generated/chainer.Sequential.html#chainer.Sequential
def forward(self, x): #前向传播函数
for n, f in self.layers:
if not n.startswith('_'):
x = getattr(self, n)(x)
else:
x = f(x)
if chainer.config.train:
return x
return F.softmax(x)
损失函数的定义方法
model = LeNet5()
# Input data and label
x = np.random.rand(32, 1, 28, 28).astype(np.float32)
t = np.random.randint(0, 10, size=(32,)).astype(np.int32)
# Forward computation
y = model(x) #预测结果
# Loss calculation
loss = F.softmax_cross_entropy(y, t) #交叉熵结果
参考网站 Docs:https://docs.chainer.org/en/stable/
Website:https://chainer.org/
论文地址:https://arxiv.org/pdf/1908.00213.pdf