Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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")
yname = layer.top[0]
output = [cgt.broadcast("+",X.dot(W), b, "xx,1x") ]
elif layer.type == "ReLU":
output = [nn.rectify(inputs[0])]
elif layer.type == "Softmax":
output = [nn.softmax(inputs[0])]
elif layer.type == "LRN":
# XXX needs params
param = layer.lrn_param
output = [nn.lrn(inputs[0], param.alpha,param.beta, param.local_size)]
elif layer.type == "Concat":
param = layer.concat_param
output = [cgt.concatenate(inputs, param.concat_dim) ]
elif layer.type == "Dropout":
output = [nn.dropout(inputs[0])]
elif layer.type == "SoftmaxWithLoss":
output = [nn.loglik_softmax(inputs[0], inputs[1])]
elif layer.type == "Accuracy":
output = [nn.zero_one_loss(inputs[0], inputs[1])]
else:
cgt.error("unrecognized layer type %s"%layer.type)
assert output is not None
# assert isinstance(output, cgt.Node)
for i in xrange(len(layer.top)): name2node[layer.top[i]] = output[i]
print "stored", layer.top[0]
if layer.type != "Data":
print "shape",layer.type, infer_shape(name2node[layer.bottom[0]]), infer_shape(name2node[layer.top[0]])
def make_loss_and_grad(net):
X_b = inps[0] #cgt.matrix(dtype=cgt.floatX)
y_onehot = cgt.matrix(dtype='i4')
outputs = [logprobs]
loss = nn.crossent(outputs[0], y_onehot) / b_size
#gradloss = cgt.grad(loss, params)
gradloss = cgt.grad(loss, param_list)
# XXX use flatcat function
grad = cgt.concatenate([x.flatten() for x in gradloss])
#grad = gradloss
return cgt.make_function([X_b, y_onehot], [loss, grad, logprobs])
def flatcat(xs):
return cgt.concatenate([x.flatten() for x in xs])
def circ_conv_1d(wg_bhn, s_bh3, axis=2):
"VERY inefficient way to implement circular convolution for the special case of filter size 3"
assert axis == 2
n = cgt.size(wg_bhn,2)
wback = cgt.concatenate([wg_bhn[:,:,n-1:n], wg_bhn[:,:,:n-1]], axis=2)
w = wg_bhn
wfwd = cgt.concatenate([wg_bhn[:,:,1:n], wg_bhn[:,:,0:1]], axis=2)
return cgt.broadcast("*", s_bh3[:,:,0:1] , wback, "xx1,xxx")\
+ cgt.broadcast("*", s_bh3[:,:,1:2] , w, "xx1,xxx")\
+ cgt.broadcast("*", s_bh3[:,:,2:3] , wfwd, "xx1,xxx")
def circ_conv_1d(wg_bhn, s_bh3, axis=2):
"VERY inefficient way to implement circular convolution for the special case of filter size 3"
assert axis == 2
n = cgt.size(wg_bhn,2)
wback = cgt.concatenate([wg_bhn[:,:,n-1:n], wg_bhn[:,:,:n-1]], axis=2)
w = wg_bhn
wfwd = cgt.concatenate([wg_bhn[:,:,1:n], wg_bhn[:,:,0:1]], axis=2)
return cgt.broadcast("*", s_bh3[:,:,0:1] , wback, "xx1,xxx")\
+ cgt.broadcast("*", s_bh3[:,:,1:2] , w, "xx1,xxx")\
+ cgt.broadcast("*", s_bh3[:,:,2:3] , wfwd, "xx1,xxx")
def flatcat(xs):
return cgt.concatenate([x.flatten() for x in xs])
def make_ff_controller(opt):
b, h, m, p, k = opt.b, opt.h, opt.m, opt.p, opt.k
H = 2*h
in_size = k + h*m
out_size = H*m + H + H + H*3 + H + h*m + h*m + p
# Previous reads
r_bhm = cgt.tensor3("r", fixed_shape = (b,h,m))
# External inputs
X_bk = cgt.matrix("x", fixed_shape = (b,k))
r_b_hm = r_bhm.reshape([r_bhm.shape[0], r_bhm.shape[1]*r_bhm.shape[2]])
# Input to controller
inp_bq = cgt.concatenate([X_bk, r_b_hm], axis=1)
hid_sizes = opt.ff_hid_sizes
activation = cgt.tanh
layer_out_sizes = [in_size] + hid_sizes + [out_size]
last_out = inp_bq
# feedforward part. we could simplify a bit by using nn.Affine
for i in xrange(len(layer_out_sizes)-1):
indim = layer_out_sizes[i]
outdim = layer_out_sizes[i+1]
W = cgt.shared(.02*nr.randn(indim, outdim), name="W%i"%i, fixed_shape_mask="all")
bias = cgt.shared(.02*nr.randn(1, outdim), name="b%i"%i, fixed_shape_mask="all")
last_out = cgt.broadcast("+",last_out.dot(W),bias,"xx,1x")
# Don't apply nonlinearity at the last layer
if i != len(layer_out_sizes)-2: last_out = activation(last_out)
Xlevel_next_pred_1 = nn.rectify(SpatialDeconvolution(xlevel_c_dim, xlevel_c_dim,
kernelshape=(3,3), pad=(1,1), stride=(1,1), name='deconv%d_2'%(level+1),
weight_init=nn.IIDGaussian(std=0.01))(Xlevel_next_pred_2))
nonlinearity = nn.rectify if level > 0 else cgt.tanh
Xlevel_next_pred = nonlinearity(SpatialDeconvolution(xlevel_c_dim, xlevelm1_c_dim,
kernelshape=(3,3), pad=(1,1), stride=(1,1), name='deconv%d_1'%(level+1),
weight_init=nn.IIDGaussian(std=0.01))(Xlevel_next_pred_1))
if level in Xlevels_next_pred_0:
coefs = nn.parameter(nn.init_array(nn.Constant(0.5), (2,)), name='sum%d.coef'%level)
Xlevel_next_pred = coefs[0] * Xlevel_next_pred + coefs[1] * Xlevels_next_pred_0[level]
# TODO: tanh should be after sum
Xlevels_next_pred[level] = Xlevel_next_pred
X_next_pred = Xlevels_next_pred[0]
Y = cgt.concatenate(Ylevels.values(), axis=1)
Y_diff_pred = cgt.concatenate(Ylevels_diff_pred.values(), axis=1)
X_diff = cgt.tensor4('X_diff', fixed_shape=(None,) + x_shape)
X_next = X + X_diff
loss = ((X_next - X_next_pred) ** 2).mean(axis=0).sum() / 2.
net_name = 'FcnActionCondEncoderNet_levels' + ''.join(str(level) for level in levels)
input_vars = OrderedDict([(var.name, var) for var in [X, U, X_diff]])
pred_vars = OrderedDict([('Y_diff_pred', Y_diff_pred), ('Y', Y), ('X_next_pred', X_next_pred)])
return net_name, input_vars, pred_vars, loss