Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_convnet_return_loss(X, y):
np.random.seed(0)
conv1 = nn.rectify(
nn.SpatialConvolution(1, 32, kernelshape=(3,3), pad=(0,0),
weight_init=nn.IIDGaussian(std=.1))(X))
pool1 = nn.max_pool_2d(conv1, kernelshape=(3,3), stride=(2,2))
conv2 = nn.rectify(
nn.SpatialConvolution(32, 32, kernelshape=(3,3), pad=(0,0),
weight_init=nn.IIDGaussian(std=.1))(pool1))
pool2 = nn.max_pool_2d(conv2, kernelshape=(3,3), stride=(2,2))
d0,d1,d2,d3 = pool2.shape
flatlayer = pool2.reshape([d0,d1*d2*d3])
nfeats = cgt.infer_shape(flatlayer)[1]
logprobs = nn.logsoftmax(nn.Affine(nfeats, 10)(flatlayer))
loss = -logprobs[cgt.arange(X.shape[0]), y].mean()
return loss
def make_updater_fc_theano():
X = TT.matrix("X")
y = TT.ivector("y")
np.random.seed(0)
stepsize = TT.scalar("stepsize")
layer1 = AffineTheano(28*28, 256, weight_init=nn.IIDGaussian(std=.1))
h1 = nn.rectify(layer1(X))
layer2 = AffineTheano(256, 256, weight_init=nn.IIDGaussian(std=.1))
h2 = nn.rectify(layer2(h1))
logprobs = logsoftmax_theano(AffineTheano(256, 10, weight_init=nn.IIDGaussian(std=.1))(h2))
neglogliks = -logprobs[TT.arange(X.shape[0]), y]
loss = neglogliks.mean()
params = [layer1.weight, layer1.bias, layer2.weight, layer2.bias]
gparams = TT.grad(loss, params)
updates = [(p, p-stepsize*gp) for (p, gp) in zip(params, gparams)]
return theano.function([X,y, stepsize], loss, updates=updates, allow_input_downcast=True)
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)
pyx = nn.softmax(cgt.dot(l4, w_o))
return pyx
def build_convnet_return_loss(X, y):
np.random.seed(0)
conv1 = nn.rectify(
nn.SpatialConvolution(1, 32, kernelshape=(3,3), pad=(0,0),
weight_init=nn.IIDGaussian(std=.1))(X))
pool1 = nn.max_pool_2d(conv1, kernelshape=(3,3), stride=(2,2))
conv2 = nn.rectify(
nn.SpatialConvolution(32, 32, kernelshape=(3,3), pad=(0,0),
weight_init=nn.IIDGaussian(std=.1))(pool1))
pool2 = nn.max_pool_2d(conv2, kernelshape=(3,3), stride=(2,2))
d0,d1,d2,d3 = pool2.shape
flatlayer = pool2.reshape([d0,d1*d2*d3])
nfeats = cgt.infer_shape(flatlayer)[1]
logprobs = nn.logsoftmax(nn.Affine(nfeats, 10)(flatlayer))
loss = -logprobs[cgt.arange(X.shape[0]), y].mean()
return loss
def dense_model(X, w_h, w_h2, w_o, p_drop_input, p_drop_hidden):
X = nn.dropout(X, p_drop_input)
h = nn.rectify(cgt.dot(X, w_h))
h = nn.dropout(h, p_drop_hidden)
h2 = nn.rectify(cgt.dot(h, w_h2))
h2 = nn.dropout(h2, p_drop_hidden)
py_x = nn.softmax(cgt.dot(h2, w_o))
return py_x