def build_encoder(tparams,options): x = tensor.matrix('x', dtype='int32') y = tensor.matrix('y',dtype='int32') layer0_input = tparams['Wemb'][tensor.cast(x.flatten(),dtype='int32')].reshape((x.shape[0],1,x.shape[1],tparams['Wemb'].shape[1])) shape =[] shape.append(layer0_input.shape) layer1_inputs = [] for i in range(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input,filter_shape=filter_shape, pool_size=pool_size,prefix=_p('cnn_encoder',i)) shape.append(conv_layer.shape) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input_x = tensor.concatenate(layer1_inputs,1) shapes = tensor.concatenate(shape,0) layer0_input = tparams['Wemb'][tensor.cast(y.flatten(),dtype='int32')].reshape((y.shape[0],1,y.shape[1],tparams['Wemb'].shape[1])) layer1_inputs = [] for i in range(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input,filter_shape=filter_shape, pool_size=pool_size,prefix=_p('cnn_encoder',i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input_y = tensor.concatenate(layer1_inputs,1) feat_x = l2norm(layer1_input_x) feat_y = l2norm(layer1_input_y) return [x, y], feat_x, feat_y
def build_model(tparams, options): trng = RandomStreams(SEED) # Used for dropout. use_noise = theano.shared(numpy_floatX(0.)) # input sentence: n_steps * n_samples x = tensor.matrix('x', dtype='int32') # label: (n_samples,) y = tensor.vector('y', dtype='int32') layer0_input = tparams['Wemb'][tensor.cast(x.flatten(), dtype='int32')].reshape( (x.shape[0], 1, x.shape[1], tparams['Wemb'].shape[1])) layer0_input = dropout(layer0_input, trng, use_noise) layer1_inputs = [] for i in xrange(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input, filter_shape=filter_shape, pool_size=pool_size, prefix=_p('cnn_encoder', i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input = tensor.concatenate(layer1_inputs, 1) layer1_input = dropout(layer1_input, trng, use_noise) # this is the label prediction you made pred = tensor.nnet.softmax( tensor.dot(layer1_input, tparams['Wy']) + tparams['by']) f_pred_prob = theano.function([x], pred, name='f_pred_prob') f_pred = theano.function([x], pred.argmax(axis=1), name='f_pred') # get the expression of how we calculate the cost function # i.e. corss-entropy loss index = tensor.arange(x.shape[0]) cost = -tensor.log(pred[index, y] + 1e-6).mean() return use_noise, x, y, f_pred_prob, f_pred, cost
def build_model(tparams,options): trng = RandomStreams(SEED) # Used for dropout. use_noise = theano.shared(numpy_floatX(0.)) # input sentence: n_steps * n_samples x = tensor.matrix('x', dtype='int32') # label: (n_samples,) y = tensor.vector('y',dtype='int32') layer0_input = tparams['Wemb'][tensor.cast(x.flatten(),dtype='int32')].reshape((x.shape[0],1,x.shape[1],tparams['Wemb'].shape[1])) layer0_input = dropout(layer0_input, trng, use_noise) layer1_inputs = [] for i in xrange(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input,filter_shape=filter_shape, pool_size=pool_size,prefix=_p('cnn_encoder',i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input = tensor.concatenate(layer1_inputs,1) layer1_input = dropout(layer1_input, trng, use_noise) # this is the label prediction you made pred = tensor.nnet.softmax(tensor.dot(layer1_input, tparams['Wy']) + tparams['by']) f_pred_prob = theano.function([x], pred, name='f_pred_prob') f_pred = theano.function([x], pred.argmax(axis=1), name='f_pred') # get the expression of how we calculate the cost function # i.e. corss-entropy loss index = tensor.arange(x.shape[0]) cost = -tensor.log(pred[index, y] + 1e-6).mean() return use_noise, x, y, f_pred_prob, f_pred, cost
def build_model(tparams, options): trng = RandomStreams(SEED) # Used for dropout. use_noise = theano.shared(numpy_floatX(0.)) x = tensor.matrix('x', dtype='int32') y = tensor.matrix('y', dtype='int32') cy = tensor.matrix('cy', dtype='int32') layer0_input = tparams['Wemb'][tensor.cast(x.flatten(), dtype='int32')].reshape( (x.shape[0], 1, x.shape[1], tparams['Wemb'].shape[1])) layer1_inputs = [] for i in xrange(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input, filter_shape=filter_shape, pool_size=pool_size, prefix=_p('cnn_encoder', i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input_x = tensor.concatenate(layer1_inputs, 1) layer1_input_x = dropout(layer1_input_x, trng, use_noise) layer0_input = tparams['Wemb'][tensor.cast(y.flatten(), dtype='int32')].reshape( (y.shape[0], 1, y.shape[1], tparams['Wemb'].shape[1])) layer1_inputs = [] for i in xrange(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input, filter_shape=filter_shape, pool_size=pool_size, prefix=_p('cnn_encoder', i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input_y = tensor.concatenate(layer1_inputs, 1) layer1_input_y = dropout(layer1_input_y, trng, use_noise) layer0_input = tparams['Wemb'][tensor.cast( cy.flatten(), dtype='int32')].reshape( (cy.shape[0], 1, cy.shape[1], tparams['Wemb'].shape[1])) layer1_inputs = [] for i in xrange(len(options['filter_hs'])): filter_shape = options['filter_shapes'][i] pool_size = options['pool_sizes'][i] conv_layer = encoder(tparams, layer0_input, filter_shape=filter_shape, pool_size=pool_size, prefix=_p('cnn_encoder', i)) layer1_input = conv_layer layer1_inputs.append(layer1_input) layer1_input_cy = tensor.concatenate(layer1_inputs, 1) layer1_input_cy = dropout(layer1_input_cy, trng, use_noise) layer1_input_x = l2norm(layer1_input_x) layer1_input_y = l2norm(layer1_input_y) layer1_input_cy = l2norm(layer1_input_cy) # Tile by number of contrast terms # (ncon*n_samples) * (2*n_h) layer1_input_x = tensor.tile(layer1_input_x, (options['ncon'], 1)) layer1_input_y = tensor.tile(layer1_input_y, (options['ncon'], 1)) cost = tensor.log(1 + tensor.sum( tensor.exp(-options['gamma'] * ((layer1_input_x * layer1_input_y).sum(axis=1) - (layer1_input_x * layer1_input_cy).sum(axis=1))))) return use_noise, [x, y, cy], cost