def __init__(self, n_history, n_action, on_gpu=False):
self.n_history = n_history
self.n_action = n_action
self.on_gpu = on_gpu
super(Q, self).__init__(
l1=F.Convolution2D(n_history, 32, ksize=8, stride=4, nobias=False, wscale=np.sqrt(2)),
l2=F.Convolution2D(32, 64, ksize=3, stride=2, nobias=False, wscale=np.sqrt(2)),
l3=F.Convolution2D(64, 64, ksize=3, stride=1, nobias=False, wscale=np.sqrt(2)),
l4=F.Linear(3136, 512, wscale=np.sqrt(2)),
out=F.Linear(512, self.n_action, initialW=np.zeros((n_action, 512), dtype=np.float32))
)
if on_gpu:
self.to_gpu()
python类Convolution2D()的实例源码
def testplot_p4(im=None, r=0):
if im is None:
im = np.zeros((5, 5), dtype='float32')
im[0:5, 1] = 1.
im[0, 1:4] = 1.
im[2, 1:3] = 1.
from groupy.gfunc.z2func_array import Z2FuncArray
from groupy.garray.C4_array import C4Array
def rotate_z2_func(im, r):
imf = Z2FuncArray(im)
rot = C4Array([r], 'int')
rot_imf = rot * imf
return rot_imf.v
im = rotate_z2_func(im, r)
filter1 = np.array([[-1., 0., 1.],
[-2., 0., 2.],
[-1., 0., 1.]]).astype(np.float32)
filter2 = rotate_z2_func(filter1, 1)
filter3 = rotate_z2_func(filter1, 2)
filter4 = rotate_z2_func(filter1, 3)
from chainer.functions import Convolution2D
from chainer import Variable
im = im.astype(np.float32)
pad = 2
imf1 = Convolution2D(in_channels=1, out_channels=1, ksize=3, bias=0., pad=pad, initialW=filter1)(
Variable(im[None, None])).data[0, 0]
imf2 = Convolution2D(in_channels=1, out_channels=1, ksize=3, bias=0., pad=pad, initialW=filter2)(
Variable(im[None, None])).data[0, 0]
imf3 = Convolution2D(in_channels=1, out_channels=1, ksize=3, bias=0., pad=pad, initialW=filter3)(
Variable(im[None, None])).data[0, 0]
imf4 = Convolution2D(in_channels=1, out_channels=1, ksize=3, bias=0., pad=pad, initialW=filter4)(
Variable(im[None, None])).data[0, 0]
return im, np.r_[[imf1, imf2, imf3, imf4]]
def __init__(self, num_blocks=18, nc32=6, nc16=11, nc8=23):
"""
:param num_blocks: the number of resnet blocks per stage. There are 3 stages, for feature map width 32, 16, 8.
Total number of layers is 6 * num_blocks + 2
:param nc32: the number of feature maps in the first stage (where feature maps are 32x32)
:param nc16: the number of feature maps in the second stage (where feature maps are 16x16)
:param nc8: the number of feature maps in the third stage (where feature maps are 8x8)
"""
ksize = 3
pad = 1
ws = sqrt(2.) # This makes the initialization equal to that of He et al.
super(P4MResNet, self).__init__()
# The first layer is always a convolution.
self.add_link(
P4MConvZ2(in_channels=3, out_channels=nc32, ksize=ksize, stride=1, pad=pad, wscale=ws)
)
# Add num_blocks ResBlocks (2 * num_blocks layers) for the size 32x32 feature maps
for i in range(num_blocks):
self.add_link(
ResBlock2D(
in_channels=nc32, out_channels=nc32, ksize=ksize,
fiber_map='id', conv_link=P4MConvP4M, stride=1, pad=pad, wscale=ws
)
)
# Add num_blocks ResBlocks (2 * num_blocks layers) for the size 16x16 feature maps
# The first convolution uses stride 2
for i in range(num_blocks):
stride = 1 if i > 0 else 2
fiber_map = 'id' if i > 0 else 'linear'
nc_in = nc16 if i > 0 else nc32
self.add_link(
ResBlock2D(
in_channels=nc_in, out_channels=nc16, ksize=ksize,
fiber_map=fiber_map, conv_link=P4MConvP4M, stride=stride, pad=pad, wscale=ws
)
)
# Add num_blocks ResBlocks (2 * num_blocks layers) for the size 8x8 feature maps
# The first convolution uses stride 2
for i in range(num_blocks):
stride = 1 if i > 0 else 2
fiber_map = 'id' if i > 0 else 'linear'
nc_in = nc8 if i > 0 else nc16
self.add_link(
ResBlock2D(
in_channels=nc_in, out_channels=nc8, ksize=ksize,
fiber_map=fiber_map, conv_link=P4MConvP4M, stride=stride, pad=pad, wscale=ws
)
)
# Add BN and final layer
# We do ReLU and average pooling between BN and final layer,
# but since these are stateless they don't require a Link.
self.add_link(F.BatchNormalization(size=nc8))
self.add_link(F.Convolution2D(in_channels=nc8 * 8, out_channels=10, ksize=1, stride=1, pad=0, wscale=ws))
def __init__(self):
ksize = 3
bn = True
act = F.relu
self.dr = 0.3
super(Z2CNN, self).__init__(
l1=ConvBNAct(
conv=F.Convolution2D(in_channels=1, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
l2=ConvBNAct(
conv=F.Convolution2D(in_channels=20, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
l3=ConvBNAct(
conv=F.Convolution2D(in_channels=20, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
l4=ConvBNAct(
conv=F.Convolution2D(in_channels=20, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
l5=ConvBNAct(
conv=F.Convolution2D(in_channels=20, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
l6=ConvBNAct(
conv=F.Convolution2D(in_channels=20, out_channels=20, ksize=ksize, stride=1, pad=0),
bn=bn,
act=act
),
top=F.Convolution2D(in_channels=20, out_channels=10, ksize=4, stride=1, pad=0),
)
def __init__(self, enable_controller=[0, 1, 3, 4]):
self.num_of_actions = len(enable_controller)
self.enable_controller = enable_controller # Default setting : "Breakout"
print "Initializing DDQN..."
# Initialization of Chainer 1.1.0 or older.
# print "CUDA init"
# cuda.init()
print "Model Building"
self.model = FunctionSet(
l1=F.Convolution2D(4, 32, ksize=8, stride=4, nobias=False, wscale=np.sqrt(2)),
l2=F.Convolution2D(32, 64, ksize=4, stride=2, nobias=False, wscale=np.sqrt(2)),
l3=F.Convolution2D(64, 64, ksize=3, stride=1, nobias=False, wscale=np.sqrt(2)),
l4=F.Linear(3136, 512, wscale=np.sqrt(2)),
q_value=F.Linear(512, self.num_of_actions,
initialW=np.zeros((self.num_of_actions, 512),
dtype=np.float32))
).to_gpu()
if args.resumemodel:
# load saved model
serializers.load_npz(args.resumemodel, self.model)
print "load model from resume.model"
self.model_target = copy.deepcopy(self.model)
print "Initizlizing Optimizer"
self.optimizer = optimizers.RMSpropGraves(lr=0.00025, alpha=0.95, momentum=0.95, eps=0.0001)
self.optimizer.setup(self.model.collect_parameters())
# History Data : D=[s, a, r, s_dash, end_episode_flag]
if args.resumeD1 and args.resumeD2:
# load saved D1 and D2
npz_tmp1 = np.load(args.resumeD1)
npz_tmp2 = np.load(args.resumeD2)
self.D = [npz_tmp1['D0'],
npz_tmp1['D1'],
npz_tmp1['D2'],
npz_tmp2['D3'],
npz_tmp2['D4']]
npz_tmp1.close()
npz_tmp2.close()
print "loaded stored D1 and D2"
else:
self.D = [np.zeros((self.data_size, 4, 84, 84), dtype=np.uint8),
np.zeros(self.data_size, dtype=np.uint8),
np.zeros((self.data_size, 1), dtype=np.int8),
np.zeros((self.data_size, 4, 84, 84), dtype=np.uint8),
np.zeros((self.data_size, 1), dtype=np.bool)]
print "initialize D data"