我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用theano.tensor.ltensor3()。
def arch_memnet_lexical(self): ''' each memory slot is a lexical. ''' contexts = T.ltensor3('contexts') querys = T.lmatrix('querys') yvs = T.lvector('yvs') hop = 1 params = [] question_layer = Embed(self.vocab_size, self.hidden_dim) q = T.reshape(question_layer(querys.flatten()), (self.batchsize, self.sen_maxlen, self.hidden_dim) ) if self.kwargs.get('position_encoding'): lmat = position_encoding(self.sen_maxlen, self.hidden_dim).dimshuffle('x', 0, 1) print '[memory network] use PE' q = q * lmat u = mean(q, axis=1) params.extend(question_layer.params) mem_layers = [] for hi in range(hop): mem_layer = MemoryLayer(self.batchsize, self.mem_size, self.unit_size, self.vocab_size, self.hidden_dim, **self.kwargs) params.extend(mem_layer.params) mem_layers.append(mem_layer) o = mem_layer(contexts, u) u = u + o linear = LinearLayer(self.hidden_dim, self.vocab_size) params.extend(linear.params) probs = softmax(linear(u)) inputs = { 'contexts': contexts, 'querys': querys, 'yvs': yvs, 'cvs': T.lmatrix('cvs') } return (probs, inputs, params)
def arch_lstmq(self, param_b=2): contexts = T.ltensor3('contexts') querys = T.lmatrix('querys') yvs = T.lvector('yvs') params = [] question_layer = Embed(self.vocab_size, self.hidden_dim) params.extend(question_layer.params) q = T.reshape(question_layer(querys.flatten()), (self.batchsize, self.sen_maxlen, self.hidden_dim) ) lmat = position_encoding(self.sen_maxlen, self.hidden_dim).dimshuffle('x', 0, 1) q = q * lmat u = mean(q, axis=1) embed_layer = Embed(self.vocab_size, self.hidden_dim) params.extend(embed_layer.params) lmat = position_encoding(self.unit_size, self.hidden_dim).dimshuffle('x', 'x', 0, 1) m = T.reshape(embed_layer(contexts.flatten()), (self.batchsize, self.mem_size, self.unit_size, self.hidden_dim)) m = mean(m * lmat, axis=2) lstm = LSTMq(self.batchsize, self.hidden_dim) params.extend(lstm.params) o = lstm(m.dimshuffle(1, 0, 2), u) linear = LinearLayer(self.hidden_dim, self.vocab_size) params.extend(linear.params) probs = softmax(linear(o)) inputs = { 'contexts': contexts, 'querys': querys, 'yvs': yvs, 'cvs': T.lmatrix('cvs') } return (probs, inputs, params)
def arch_memnet_selfsup(self): ''' memory net with self supervision. ''' contexts = T.ltensor3('contexts') querys = T.lmatrix('querys') yvs = T.lmatrix('yvs') params = [] question_layer = Embed(self.vocab_size, self.hidden_dim) q = T.reshape(question_layer(querys.flatten()), (self.batchsize, self.sen_maxlen, self.hidden_dim) ) if self.kwargs.get('position_encoding'): lmat = position_encoding(self.sen_maxlen, self.hidden_dim).dimshuffle('x', 0, 1) print '[memory network] use PE' q = q * lmat u = mean(q, axis=1) params.extend(question_layer.params) mem_layer = MemoryLayer(self.batchsize, self.mem_size, self.unit_size, self.vocab_size, self.hidden_dim, **self.kwargs) probs = mem_layer.get_probs(contexts, u).dimshuffle(0, 2) inputs = { 'contexts': contexts, 'querys': querys, 'yvs': yvs, 'cvs': T.lmatrix('cvs') } return (probs, inputs, params)
def __init__(self, data, config, fast_predict=False): self.embedding_shapes = data.embedding_shapes; self.lstm_type = config.lstm_cell self.lstm_hidden_size = int(config.lstm_hidden_size) self.num_lstm_layers = int(config.num_lstm_layers) self.max_grad_norm = float(config.max_grad_norm) self.vocab_size = data.word_dict.size() self.label_space_size = data.label_dict.size() self.unk_id = data.unk_id # Initialize layers and parameters self.embedding_layer = EmbeddingLayer(data.embedding_shapes, data.embeddings) self.params = [p for p in self.embedding_layer.params] self.rnn_layers = [None] * self.num_lstm_layers for l in range(self.num_lstm_layers): input_dim = self.embedding_layer.output_size if l == 0 else self.lstm_hidden_size input_dropout = config.input_dropout_prob if (config.per_layer_dropout or l == 0) else 0.0 recurrent_dropout = config.recurrent_dropout_prob self.rnn_layers[l] = get_rnn_layer(self.lstm_type)(input_dim, self.lstm_hidden_size, input_dropout_prob=input_dropout, recurrent_dropout_prob=recurrent_dropout, fast_predict=fast_predict, prefix='lstm_{}'.format(l)) print (self.rnn_layers[l]) self.params.extend(self.rnn_layers[l].params) self.softmax_layer = SoftmaxLayer(self.lstm_hidden_size, self.label_space_size) self.params.extend(self.softmax_layer.params) # Build model # Shape of x: [seq_len, batch_size, num_features] self.x0 = tensor.ltensor3('x') self.y0 = tensor.lmatrix('y') self.mask0 = tensor.matrix('mask', dtype=floatX) self.is_train = tensor.bscalar('is_train') self.x = self.x0.dimshuffle(1, 0, 2) self.y = self.y0.dimshuffle(1, 0) self.mask = self.mask0.dimshuffle(1, 0) self.inputs = [None] * (self.num_lstm_layers + 1) self.inputs[0] = self.embedding_layer.connect(self.x) self.rev_mask = self.mask[::-1] for l, rnn in enumerate(self.rnn_layers): outputs = rnn.connect(self.inputs[l], self.mask if l % 2 == 0 else self.rev_mask, self.is_train) self.inputs[l+1] = outputs[::-1] self.scores, self.pred = self.softmax_layer.connect(self.inputs[-1]) self.pred0 = self.pred.reshape([self.x.shape[0], self.x.shape[1]]).dimshuffle(1, 0)