def forward(self, hidden_states): hidden_states = self.dropout(hidden_states) hidden_states = self.dense(hidden_states) hidden_states = F.tanh(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.out_proj(hidden_states) return hidden_states
def get_usr_feature(self, usr_var): """定义计算用户特征的前向运算过程""" # 获取到用户数据 usr_id, usr_gender, usr_age, usr_job = usr_var # 将用户的ID数据经过embedding和Linear计算,得到的特征保存在feats_collect中 feats_collect = [] usr_id_feat = F.relu(self.usr_fc(self.usr_emb(usr_id))) feats_collect.append(usr_id_feat) # 计算用户的性别特征,并保存在feats_collect中 usr_gender_feat = F.relu( self.usr_gender_fc(self.usr_gender_emb(usr_gender))) feats_collect.append(usr_gender_feat) # 选择是否使用用户的年龄-职业特征 if self.use_usr_age_job: usr_age_feat = F.relu(self.usr_age_fc(self.usr_age_emb(usr_age))) feats_collect.append(usr_age_feat) usr_job_feat = F.relu(self.usr_job_fc(self.usr_job_emb(usr_job))) feats_collect.append(usr_job_feat) # 将用户的特征级联,并通过Linear层得到最终的用户特征 usr_feat = paddle.concat(feats_collect, axis=-1) user_features = F.tanh(self.usr_combined(usr_feat)) # 通过3层全链接层,获得用于计算相似度的用户特征和电影特征 for n_layer in self._user_layers: user_features = n_layer(user_features) return user_features
def forward(self, inputs, encoder_word_pos, gsrm_word_pos): b, c, h, w = inputs.shape conv_features = paddle.reshape(inputs, shape=[-1, c, h * w]) conv_features = paddle.transpose(conv_features, perm=[0, 2, 1]) # transformer encoder b, t, c = conv_features.shape enc_inputs = [conv_features, encoder_word_pos, None] word_features = self.wrap_encoder_for_feature(enc_inputs) # pvam b, t, c = word_features.shape word_features = self.fc0(word_features) word_features_ = paddle.reshape(word_features, [-1, 1, t, c]) word_features_ = paddle.tile(word_features_, [1, self.max_length, 1, 1]) word_pos_feature = self.emb(gsrm_word_pos) word_pos_feature_ = paddle.reshape(word_pos_feature, [-1, self.max_length, 1, c]) word_pos_feature_ = paddle.tile(word_pos_feature_, [1, 1, t, 1]) y = word_pos_feature_ + word_features_ y = F.tanh(y) attention_weight = self.fc1(y) attention_weight = paddle.reshape(attention_weight, shape=[-1, self.max_length, t]) attention_weight = F.softmax(attention_weight, axis=-1) pvam_features = paddle.matmul(attention_weight, word_features) #[b, max_length, c] return pvam_features
def forward(self, x): x_in = x for i, layer in enumerate(self.convs): x = layer(x) if i != (len(self.convs) - 1): x = F.tanh(x) x = self.last_bn(x_in + x) return x
def select_action(self, state): state = paddle.to_tensor(state.reshape(1, -1)).astype('float32') mean, log_std = self.forward(state) std = log_std.exp() normal = Normal(mean, std) z = normal.sample([1]) action = self.max_action * F.tanh(z).detach().numpy()[0].reshape([-1]) return action
def forward(self, hidden_states): """ Args: hidden_states (Tensor): Hidden states of the classification model. """ hidden_states = self.dropout(hidden_states) hidden_states = self.dense(hidden_states) hidden_states = F.tanh(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.out_proj(hidden_states) return hidden_states
def GRU(x, h_nei, W_z, W_r, U_r, W_h): hidden_size = x.shape[-1] sum_h = paddle.sum(h_nei, axis=1) z_input = paddle.concat([x, sum_h], axis=1) z = F.sigmoid(W_z(z_input)) r_1 = paddle.reshape(W_r(x), shape=[-1, 1, hidden_size]) r_2 = U_r(h_nei) r = F.sigmoid(r_1 + r_2) gated_h = r * h_nei sum_gated_h = paddle.sum(gated_h, axis=1) h_input = paddle.concat([x, sum_gated_h], axis=1) pre_h = F.tanh(W_h(h_input)) new_h = (1.0 - z) * sum_h + z * pre_h return new_h
def pointer(self, x, state): x_ = paddle.fluid.layers.expand(state.unsqueeze(1),expand_times=[1, x.shape[1], 1]) out = paddle.concat([x, x_], axis=2) s0 = F.tanh(self.linear(out)) s = self.weights(s0).reshape(shape=[x.shape[0], x.shape[1]]) a = F.softmax(s) res = a.unsqueeze(1).bmm(x).squeeze(1) if self.normalize: if self.training: # In training we output log-softmax for NLL scores = F.log_softmax(s) else: # ...Otherwise 0-1 probabilities scores = F.softmax(s) else: scores = a.exp() return scores,res
def forward(self, inputs): # 公共网络层 x = F.relu(self.conv1(inputs)) x = F.relu(self.conv2(x)) x = F.relu(self.conv3(x)) # 行动策略网络层 x_act = F.relu(self.act_conv1(x)) x_act = paddle.reshape( x_act, [-1, 4 * self.board_height * self.board_width]) x_act = F.log_softmax(self.act_fc1(x_act)) # 状态价值网络层 x_val = F.relu(self.val_conv1(x)) x_val = paddle.reshape( x_val, [-1, 2 * self.board_height * self.board_width]) x_val = F.relu(self.val_fc1(x_val)) x_val = F.tanh(self.val_fc2(x_val)) return x_act,x_val
def forward(self, s_t_hat, encoder_outputs, encoder_feature, enc_padding_mask, coverage): b, t_k, n = encoder_outputs.shape dec_fea = self.decode_proj(s_t_hat) # B x 2*hidden_dim dec_fea_expanded = paddle.expand(dec_fea.unsqueeze(1), [b, t_k, n]) # B x t_k x 2*hidden_dim dec_fea_expanded = paddle.reshape(dec_fea_expanded, [-1, n]) # B * t_k x 2*hidden_dim att_features = encoder_feature + dec_fea_expanded # B * t_k x 2*hidden_dim if config.is_coverage: coverage_input = paddle.reshape(coverage, [-1, 1]) # B * t_k x 1 coverage_feature = self.W_c( coverage_input) # B * t_k x 2*hidden_dim att_features = att_features + coverage_feature e = F.tanh(att_features) # B * t_k x 2*hidden_dim scores = self.v(e) # B * t_k x 1 scores = paddle.reshape(scores, [-1, t_k]) # B x t_k attn_dist_ = F.softmax(scores, axis=1) * enc_padding_mask # B x t_k normalization_factor = attn_dist_.sum(1, keepdim=True) # attn_dist = attn_dist_ / normalization_factor attn_dist = attn_dist_ / ( paddle.reshape(normalization_factor, [-1, 1]) + paddle.ones_like(paddle.reshape(normalization_factor, [-1, 1])) * sys.float_info.epsilon) # See the issue: https://github.com/atulkum/pointer_summarizer/issues/54 attn_dist = attn_dist.unsqueeze(1) # B x 1 x t_k c_t = paddle.bmm(attn_dist, encoder_outputs) # B x 1 x n c_t = paddle.reshape(c_t, [-1, config.hidden_dim * 2]) # B x 2*hidden_dim attn_dist = paddle.reshape(attn_dist, [-1, t_k]) # B x t_k if config.is_coverage: coverage = paddle.reshape(coverage, [-1, t_k]) coverage = coverage + attn_dist return c_t, attn_dist, coverage
def get_mov_feat(self, mov_var): """ get movie features""" # 获得电影数据 mov_id, mov_cat, mov_title, mov_poster = mov_var feats_collect = [] # 获得batchsize的大小 batch_size = mov_id.shape[0] # 计算电影ID的特征,并存在feats_collect中 mov_id = self.mov_emb(mov_id) mov_id = self.mov_fc(mov_id) mov_id = F.relu(mov_id) feats_collect.append(mov_id) # 如果使用电影的种类数据,计算电影种类特征的映射 if self.use_mov_cat: # 计算电影种类的特征映射,对多个种类的特征求和得到最终特征 mov_cat = self.mov_cat_emb(mov_cat) mov_cat = paddle.sum(mov_cat, axis=1, keepdim=False) mov_cat = self.mov_cat_fc(mov_cat) feats_collect.append(mov_cat) if self.use_mov_title: # 计算电影名字的特征映射,对特征映射使用卷积计算最终的特征 mov_title = self.mov_title_emb(mov_title) mov_title = F.relu( self.mov_title_conv2(F.relu(self.mov_title_conv(mov_title)))) mov_title = paddle.sum(mov_title, axis=2, keepdim=False) mov_title = F.relu(mov_title) mov_title = paddle.reshape(mov_title, [batch_size, -1]) feats_collect.append(mov_title) # 使用一个全连接层,整合所有电影特征,映射为一个200维的特征向量 mov_feat = paddle.concat(feats_collect, axis=1) mov_features = F.tanh(self.mov_concat_embed(mov_feat)) for n_layer in self._movie_layers: mov_features = n_layer(mov_features) return mov_features
def forward(self, h, x, mess_graph): """forward""" mask = paddle.ones([h.shape[0], 1]) mask[0] = 0 for it in range(self.depth): h_nei = index_select_ND(h, 0, mess_graph) sum_h = paddle.sum(h_nei, axis=1) z_input = paddle.concat([x, sum_h], axis=1) z = F.sigmoid(self.W_z(z_input)) r_1 = paddle.reshape(self.W_r(x), shape=[-1, 1, self.hidden_size]) r_2 = self.U_r(h_nei) r = F.sigmoid(r_1 + r_2) gated_h = r * h_nei sum_gated_h = paddle.sum(gated_h, axis=1) h_input = paddle.concat([x, sum_gated_h], axis=1) pre_h = F.tanh(self.W_h(h_input)) h = (1.0 - z) * sum_h + z * pre_h h = h * mask return h
def get_movie_feature(self, mov_var): """定义电影特征的前向计算过程""" # 获得电影数据 mov_id, mov_cat, mov_title, mov_poster = mov_var feats_collect = [] # 获得batchsize的大小 batch_size = mov_id.shape[0] # 计算电影ID的特征,并存在feats_collect中 mov_id_feat = F.relu(self.mov_fc(self.mov_emb(mov_id))) feats_collect.append(mov_id_feat) # 如果使用电影的种类数据,计算电影种类特征的映射 if self.use_mov_cat: # 计算电影种类的特征映射,对多个种类的特征求和得到最终特征 mov_cat_feat = self.mov_cat_emb(mov_cat) mov_cat_feat = paddle.sum( mov_cat_feat, axis=1, keepdim=False) # shape: [btc, feature_size] mov_cat_feat = F.relu(self.mov_cat_fc(mov_cat_feat)) feats_collect.append(mov_cat_feat) if self.use_mov_title: mov_title_feat = self.mov_title_emb(mov_title) mov_title_feat = self.mov_title_conv2( F.relu(self.mov_title_conv(mov_title_feat))) mov_title_feat = paddle.sum( mov_title_feat, axis=2, keepdim=False) # shape: [bat, 1, feature_size] mov_title_feat = F.relu(mov_title_feat) mov_title_feat = paddle.reshape(mov_title_feat, [batch_size, -1]) feats_collect.append(mov_title_feat) # 使用一个全连接层,整合所有电影特征,映射为一个200维的特征向量 mov_feat = paddle.concat(feats_collect, axis=-1) mov_features = F.tanh(self.mov_concat_embed(mov_feat)) # 继续使用深度网络提取特征 for n_layer in self._movie_layers: mov_features = n_layer(mov_features) return mov_features
def get_usr_feat(self, usr_var): """ get usr features""" # 获取到用户数据 usr_id, usr_gender, usr_age, usr_job = usr_var # 将用户的ID数据经过embedding和Linear计算,得到的特征保存在feats_collect中 feats_collect = [] usr_id = self.usr_emb(usr_id) usr_id = self.usr_fc(usr_id) usr_id = F.relu(usr_id) feats_collect.append(usr_id) # 计算用户的性别特征,并保存在feats_collect中 usr_gender = self.usr_gender_emb(usr_gender) usr_gender = self.usr_gender_fc(usr_gender) usr_gender = F.relu(usr_gender) feats_collect.append(usr_gender) # 选择是否使用用户的年龄-职业特征 if self.use_usr_age_job: # 计算用户的年龄特征,并保存在feats_collect中 usr_age = self.usr_age_emb(usr_age) usr_age = self.usr_age_fc(usr_age) usr_age = F.relu(usr_age) feats_collect.append(usr_age) # 计算用户的职业特征,并保存在feats_collect中 usr_job = self.usr_job_emb(usr_job) usr_job = self.usr_job_fc(usr_job) usr_job = F.relu(usr_job) feats_collect.append(usr_job) # 将用户的特征级联,并通过Linear层得到最终的用户特征 usr_feat = paddle.concat(feats_collect, axis=1) user_features = F.tanh(self.usr_combined(usr_feat)) #通过3层全链接层,获得用于计算相似度的用户特征和电影特征 # for n_layer in self._user_layers: # user_features = n_layer(user_features) return user_features
def forward(self, input): """Calculate forward propagation. Parameters ---------- input: Tensor [shape=(B, T_mel, C)] Output sequence of features from decoder. Returns ------- output: Tensor [shape=(B, T_mel, C)] Output sequence of features after postnet. """ for i in range(len(self.conv_batchnorms) - 1): input = F.dropout(F.tanh(self.conv_batchnorms[i](input)), self.dropout, training=self.training) output = F.dropout(self.conv_batchnorms[self.num_layers - 1](input), self.dropout, training=self.training) return output
def forward(self, src_ids, sent_ids=None, pos_ids=None, input_mask=None, attn_bias=None, past_cache=None, use_causal_mask=False): """ Args: src_ids (`Variable` of shape `[batch_size, seq_len]`): Indices of input sequence tokens in the vocabulary. sent_ids (optional, `Variable` of shape `[batch_size, seq_len]`): aka token_type_ids, Segment token indices to indicate first and second portions of the inputs. if None, assume all tokens come from `segment_a` pos_ids(optional, `Variable` of shape `[batch_size, seq_len]`): Indices of positions of each input sequence tokens in the position embeddings. input_mask(optional `Variable` of shape `[batch_size, seq_len]`): Mask to avoid performing attention on the padding token indices of the encoder input. attn_bias(optional, `Variable` of shape `[batch_size, seq_len, seq_len] or False`): 3D version of `input_mask`, if set, overrides `input_mask`; if set not False, will not apply attention mask past_cache(optional, tuple of two lists: cached key and cached value, each is a list of `Variable`s of shape `[batch_size, seq_len, hidden_size]`): cached key/value tensor that will be concated to generated key/value when performing self attention. if set, `attn_bias` should not be None. Returns: pooled (`Variable` of shape `[batch_size, hidden_size]`): output logits of pooler classifier encoded(`Variable` of shape `[batch_size, seq_len, hidden_size]`): output logits of transformer stack info (Dictionary): addtional middle level info, inclues: all hidden stats, k/v caches. """ assert len( src_ids.shape ) == 2, 'expect src_ids.shape = [batch, sequecen], got %s' % (repr( src_ids.shape)) assert attn_bias is not None if past_cache else True, 'if `past_cache` is specified; attn_bias should not be None' d_seqlen = P.shape(src_ids)[1] if pos_ids is None: pos_ids = P.arange(0, d_seqlen, 1, dtype='int32').reshape([1, -1]).cast('int64') if attn_bias is None: if input_mask is None: input_mask = P.cast(src_ids != 0, 'float32') assert len(input_mask.shape) == 2 input_mask = input_mask.unsqueeze(-1) attn_bias = input_mask.matmul(input_mask, transpose_y=True) if use_causal_mask: sequence = P.reshape( P.arange(0, d_seqlen, 1, dtype='float32') + 1., [1, 1, -1, 1]) causal_mask = (sequence.matmul(1. / sequence, transpose_y=True) >= 1.).cast('float32') attn_bias *= causal_mask else: assert len( attn_bias.shape ) == 3, 'expect attn_bias tobe rank 3, got %r' % attn_bias.shape attn_bias = (1. - attn_bias) * -10000.0 attn_bias = attn_bias.unsqueeze(1).tile([1, self.n_head, 1, 1]) # avoid broadcast =_= if sent_ids is None: sent_ids = P.zeros_like(src_ids) src_embedded = self.word_emb(src_ids) pos_embedded = self.pos_emb(pos_ids) sent_embedded = self.sent_emb(sent_ids) embedded = src_embedded + pos_embedded + sent_embedded embedded = self.dropout(self.ln(embedded)) encoded, hidden_list, cache_list = self.encoder_stack( embedded, attn_bias, past_cache=past_cache) if self.pooler is not None: pooled = F.tanh(self.pooler(encoded[:, 0, :])) else: pooled = None additional_info = { 'hiddens': hidden_list, 'caches': cache_list, } if self.return_additional_info: return pooled, encoded, additional_info return pooled, encoded
usr_gender_feat = usr_gender_fc(usr_gender_emb(usr_gender_var)) usr_gender_feat = F.relu(usr_gender_feat) usr_combined = Linear(in_features=80, out_features=200) # 收集所有的用户特征 _features = [usr_id_feat, usr_job_feat, usr_age_feat, usr_gender_feat] print("打印每个特征的维度:", [f.shape for f in _features]) _features = [k.numpy() for k in _features] _features = [paddle.to_tensor(k) for k in _features] # 对特征沿着最后一个维度级联 usr_feat = paddle.concat(_features, axis=1) usr_feat = F.tanh(usr_combined(usr_feat)) # 自定义一个电影ID数据 mov_id_data = np.array((1, 2)).reshape(-1).astype('int64') # 对电影ID信息做映射,并紧接着一个FC层 MOV_DICT_SIZE = 3952 + 1 mov_emb = Embedding(num_embeddings=MOV_DICT_SIZE, embedding_dim=32) mov_fc = Linear(32, 32) print("输入的电影ID是:", mov_id_data) mov_id_data = paddle.to_tensor(mov_id_data) mov_id_feat = mov_fc(mov_emb(mov_id_data)) mov_id_feat = F.relu(mov_id_feat)
def forward(self, x): return paddle.clip( (x + F.tanh(self.layers(((x * 1).detach()) - 0.4462414))), 0.0, 1.0)
def forward(self, x, fusions): r_f = paddle.concat([x, fusions], axis=2) r = F.tanh(self.linear_r(r_f)) g = F.sigmoid(self.linear_g(r_f)) o = g * r + (1-g) * x return o
def forward(self, state): a = F.relu(self.l1(state)) a = F.relu(self.l2(a)) return self.max_action * F.tanh(self.l3(a))
def forward(self,inputs_emb): r = self.linear_r(inputs_emb) g = self.linear_g(inputs_emb) r,g = F.tanh(r),F.sigmoid(g) o = g*r + (1-g)*self.weights(inputs_emb) return o
usr_gender_var = paddle.to_tensor(usr_gender_data) usr_gender_feat = usr_gender_fc(usr_gender_emb(usr_gender_var)) usr_gender_feat = F.relu(usr_gender_feat) # 自定义一个用户职业数据 usr_job_data = np.array((0, 20)).reshape(-1).astype('int64') # 用户职业的最大ID是20,所以Embedding层size的第一个参数设置为20 + 1 = 21 USR_JOB_DICT_SIZE = 20 + 1 usr_job_emb = Embedding(num_embeddings=USR_JOB_DICT_SIZE, embedding_dim=16) usr_job_fc = Linear(in_features=16, out_features=16) usr_job = paddle.to_tensor(usr_job_data) usr_job_feat = usr_job_emb(usr_job) usr_job_feat = usr_job_fc(usr_job_feat) usr_job_feat = F.relu(usr_job_feat) # 收集所有的用户特征 _features = [usr_id_feat, usr_job_feat, usr_age_feat, usr_gender_feat] _features = [k.numpy() for k in _features] _features = [paddle.to_tensor(k) for k in _features] id_feat = F.tanh(FC_ID(_features[0])) job_feat = F.tanh(FC_JOB(_features[1])) age_feat = F.tanh(FC_AGE(_features[2])) genger_feat = F.tanh(FC_GENDER(_features[-1])) # 对特征求和 usr_feat = id_feat + job_feat + age_feat + genger_feat print("用户融合后特征的维度是:", usr_feat.shape)
def forward(self, state): a = F.relu(self.l1(state)) a = F.relu(self.l2(a)) # 输出层激活函数采用tanh,将输出映射至[-1,1] return F.tanh(self.l3(a))
def mish(x): return x * F.tanh(F.softplus(x))
mov_title_data = paddle.to_tensor(mov_title_data) print("电影名称数据的输入形状: ", mov_title_data.shape) # 1. 通过Embedding映射电影名称数据; mov_title_feat = mov_title_emb(mov_title_data) print("输入通过Embedding层的输出形状: ", mov_title_feat.shape) # 2. 对Embedding后的向量使用卷积层进一步提取特征; mov_title_feat = F.relu(mov_title_conv(mov_title_feat)) print("第一次卷积之后的特征输出形状: ", mov_title_feat.shape) mov_title_feat = F.relu(mov_title_conv2(mov_title_feat)) print("第二次卷积之后的特征输出形状: ", mov_title_feat.shape) batch_size = mov_title_data.shape[0] # 3. 最后对特征进行降采样,keepdim=False会让输出的维度减少,而不是用[2,1,1,32]的形式占位; mov_title_feat = paddle.sum(mov_title_feat, axis=2, keepdim=False) print("reduce_sum降采样后的特征输出形状: ", mov_title_feat.shape) mov_title_feat = F.relu(mov_title_feat) mov_title_feat = paddle.reshape(mov_title_feat, [batch_size, -1]) mov_combined = Linear(in_features=96, out_features=200) # 收集所有的电影特征 _features = [mov_id_feat, mov_cat_feat, mov_title_feat] _features = [k.numpy() for k in _features] _features = [paddle.to_tensor(k) for k in _features] # 对特征沿着最后一个维度级联 mov_feat = paddle.concat(_features, axis=1) mov_feat = mov_combined(mov_feat) mov_feat = F.tanh(mov_feat) print("融合后的电影特征维度是:", mov_feat.shape)