Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def convnet_model(X, w, w2, w3, w4, w_o, p_drop_conv, p_drop_hidden):
l1a = nn.rectify(nn.conv2d(X, w, kernelshape=(3,3), pad=(1,1)))
l1 = nn.max_pool_2d(l1a, kernelshape=(2, 2), stride=(2,2))
l1 = nn.dropout(l1, p_drop_conv)
l2a = nn.rectify(nn.conv2d(l1, w2, kernelshape=(3,3), pad=(1,1)))
l2 = nn.max_pool_2d(l2a, kernelshape=(2, 2), stride=(2,2))
l2 = nn.dropout(l2, p_drop_conv)
l3a = nn.rectify(nn.conv2d(l2, w3, kernelshape=(3,3), pad=(1,1)))
l3b = nn.max_pool_2d(l3a, kernelshape=(2, 2), stride=(2,2))
batchsize,channels,rows,cols = l3b.shape
l3 = cgt.reshape(l3b, [batchsize, channels*rows*cols])
l3 = nn.dropout(l3, p_drop_conv)
l4 = nn.rectify(cgt.dot(l3, w4))
l4 = nn.dropout(l4, p_drop_hidden)
X = inputs[0]
param = layer.convolution_param
kh,kw = (param.kernel_size, param.kernel_size) if param.HasField("kernel_size")\
else (param.kernel_h, param.kernel_w)
nchanin = infer_shape(X)[0]
Wshape = (param.num_output, nchanin, kh, kw)
Wname = layer.param[0].name or layer.name+":W"
Wval = np.empty(Wshape, dtype=cgt.floatX)
W = name2node[Wname] = cgt.shared(Wval, name=Wname, fixed_shape_mask="all")
bshape = (1, param.num_output, 1, 1)
bname = layer.param[1].name or layer.name+":b"
bval = np.empty(bshape, dtype=cgt.floatX)
b = name2node[bname] = cgt.shared(bval, name=bname, fixed_shape_mask="all")
sh,sw = (param.stride, param.stride) if param.HasField("stride")\
else (param.stride_h, param.stride_w)
output = [cgt.broadcast("+",nn.conv2d(X, W, subsample=(sh,sw)), b, "xxxx,1x11")]
elif layer.type == "Pooling":
param = layer.pooling_param
X = inputs[0]
pool_type = {param.MAX : "max", param.AVE : "mean"}[param.pool]
height_in,width_in = infer_shape(X)[2:4]
kernel = (param.kernel_size, param.kernel_size) if param.HasField("kernel_size")\
else (param.kernel_h, param.kernel_w)
stride = (param.stride, param.stride) if param.HasField("stride")\
else (param.stride_h, param.stride_w)
pad = (param.pad, param.pad) if param.HasField("pad")\
else (param.pad_h, param.pad_w)
output = [nn.pool(pool_type, X, stride, kernel, pad)]
elif layer.type == "InnerProduct":
X = inputs[0]
if X.ndim == 4:
X = cgt.reshape(X, [X.shape[0], X.shape[1]*X.shape[2]*X.shape[3]] )
# encoding
Xlevels = {}
for level in range(levels[-1]+1):
if level == 0:
Xlevel = X
else:
if level == 1:
xlevelm1_c_dim = x_c_dim
xlevel_c_dim = x1_c_dim
else:
xlevelm1_c_dim = xlevel_c_dim
xlevel_c_dim = 2 * xlevel_c_dim
Xlevel_1 = nn.rectify(nn.SpatialConvolution(xlevelm1_c_dim, xlevel_c_dim, kernelshape=(3,3), pad=(1,1), stride=(1,1), name='conv%d_1'%level,
weight_init=nn.IIDGaussian(std=0.01))(Xlevels[level-1]))
Xlevel_2 = nn.rectify(nn.SpatialConvolution(xlevel_c_dim, xlevel_c_dim, kernelshape=(3,3), pad=(1,1), stride=(1,1), name='conv%d_2'%level,
weight_init=nn.IIDGaussian(std=0.01))(Xlevel_1))
Xlevel = nn.max_pool_2d(Xlevel_2, kernelshape=(2,2), pad=(0,0), stride=(2,2))
Xlevels[level] = Xlevel
# bilinear
Xlevels_next_pred_0 = {}
Ylevels = OrderedDict()
Ylevels_diff_pred = OrderedDict()
for level in levels:
Xlevel = Xlevels[level]
Xlevel_diff_pred = Bilinear(input_shapes, b=None, axis=2, name='bilinear%d'%level)(Xlevel, U)
Xlevels_next_pred_0[level] = Xlevel + Xlevel_diff_pred
Ylevels[level] = Xlevel.reshape((Xlevel.shape[0], cgt.mul_multi(Xlevel.shape[1:])))
Ylevels_diff_pred[level] = Xlevel_diff_pred.reshape((Xlevel_diff_pred.shape[0], cgt.mul_multi(Xlevel_diff_pred.shape[1:])))
# decoding
def make_updater_fc_parallel():
X = cgt.matrix("X", fixed_shape=(None,28*28))
y = cgt.vector("y",dtype='i8')
stepsize = cgt.scalar("stepsize")
loss = build_fc_return_loss(X,y)
params = nn.get_parameters(loss)
m = nn.Module([X,y], [loss])
split_loss = 0
for start in xrange(0, batch_size, batch_size//4):
sli = slice(start, start+batch_size//4)
split_loss += m([X[sli], y[sli]])[0]
split_loss /= 4
gparams = cgt.grad(split_loss, params)
updates2 = [(p, p-stepsize*gp) for (p, gp) in zip(params, gparams)]
return cgt.function([X,y, stepsize], split_loss, updates=updates2)
l1 = nn.dropout(l1, p_drop_conv)
l2a = nn.rectify(nn.conv2d(l1, w2, kernelshape=(3,3), pad=(1,1)))
l2 = nn.max_pool_2d(l2a, kernelshape=(2, 2), stride=(2,2))
l2 = nn.dropout(l2, p_drop_conv)
l3a = nn.rectify(nn.conv2d(l2, w3, kernelshape=(3,3), pad=(1,1)))
l3b = nn.max_pool_2d(l3a, kernelshape=(2, 2), stride=(2,2))
batchsize,channels,rows,cols = l3b.shape
l3 = cgt.reshape(l3b, [batchsize, channels*rows*cols])
l3 = nn.dropout(l3, p_drop_conv)
l4 = nn.rectify(cgt.dot(l3, w4))
l4 = nn.dropout(l4, p_drop_hidden)
pyx = nn.softmax(cgt.dot(l4, w_o))
return pyx
from __future__ import division
from collections import OrderedDict
import numpy as np
import cgt
from cgt import nn
class SpatialDeconvolution(nn.SpatialConvolution):
def __init__(self, input_channels, output_channels,
kernelshape, pad, stride=(1,1), name=None,
weight_init=nn.Constant(0), bias_init=nn.Constant(0)):
self.original_input_channels = input_channels
self.original_output_channels = output_channels
self.original_kernelshape = tuple(map(int, kernelshape))
self.original_pad = tuple(map(int,pad))
self.original_stride = tuple(map(int,stride))
super(SpatialDeconvolution, self).__init__(input_channels, output_channels,
kernelshape, (kernelshape[0]-1, kernelshape[1]-1), stride=(1,1), name=name,
weight_init=weight_init, bias_init=bias_init)
def __call__(self, x):
if self.original_stride != 1:
_, _, width, height = cgt.infer_shape(x)
unstrided_width = width * self.original_stride[0]
def __init__(self, input_channels, output_channels, kernelshape, pad, stride=(1,1), name=None, weight_init=nn.Constant(0), bias_init=nn.Constant(0)):
# type conversion
self.input_channels = int(input_channels)
self.output_channels = int(output_channels)
self.kernelshape = tuple(map(int, kernelshape))
self.pad = tuple(map(int,pad))
self.stride = tuple(map(int,stride))
name = "unnamed" if name is None else name
self.weight = theano.shared(nn.init_array(weight_init, (self.output_channels, self.input_channels) + self.kernelshape),
name=name+".W")
self.bias = theano.shared(nn.init_array(bias_init, (1, self.output_channels, 1, 1)),
name=name+".b")
self.bias.type.broadcastable = (True,False,True,True)
parser = argparse.ArgumentParser()
parser.add_argument("--profile",action="store_true")
parser.add_argument("--unittest",action="store_true")
parser.add_argument("--epochs",type=int,default=10)
parser.add_argument("--devtype",choices=["cpu","gpu"],default="cpu")
args = parser.parse_args()
cgt.update_config(default_device=cgt.core.Device(devtype=args.devtype), backend="native")
batchsize = 64
Xshape = (batchsize, 3, 32, 32)
X = cgt.tensor4("X", fixed_shape = Xshape)
y = cgt.vector("y", fixed_shape = (batchsize,), dtype='i4')
conv1 = nn.SpatialConvolution(3, 32, kernelshape=(5,5), pad=(2,2),
weight_init=nn.IIDGaussian(std=1e-4))(X)
relu1 = nn.rectify(conv1)
pool1 = nn.max_pool_2d(relu1, kernelshape=(3,3), stride=(2,2))
conv2 = nn.SpatialConvolution(32, 32, kernelshape=(5,5), pad=(2,2),
weight_init=nn.IIDGaussian(std=0.01))(pool1)
relu2 = nn.rectify(conv2)
pool2 = nn.max_pool_2d(relu2, kernelshape=(3,3), stride=(2,2))
conv3 = nn.SpatialConvolution(32, 64, kernelshape=(5,5), pad=(2,2),
weight_init=nn.IIDGaussian(std=0.01))(pool2)
pool3 = nn.max_pool_2d(conv3, kernelshape=(3,3), stride=(2,2))
relu3 = nn.rectify(pool3)
d0,d1,d2,d3 = relu3.shape
flatlayer = relu3.reshape([d0,d1*d2*d3])
nfeats = cgt.infer_shape(flatlayer)[1]
ip1 = nn.Affine(nfeats, 10)(flatlayer)
logprobs = nn.logsoftmax(ip1)
loss = -logprobs[cgt.arange(batchsize), y].mean()