8.7.8 多次变形

  本例还可以通过应用二次变形矩阵来实现。Matrix类提供concat()方法。该方法可以实现多次变形矩阵的应用。

concat()方法的函数说明为:

方法定义:

public function concat(m:Matrix):void

作用:

连接(Concatenates)一个矩阵到当前矩阵,其效果等于两次变形的效果。在数学角度解释,这种连接等同于两个矩阵相乘。

备注:

1该方法无返回值,将直接修改当前矩阵。

2如果要避免修改,请使用clone方法复制当前矩阵。

参数:

 m:Matrix 用于同当前矩阵连接的一个矩阵

使用concat()方法将两个变形矩阵相乘,在此过程中,txty会参与运算,所以不必在代码中自行设置。通过创建一个处理伸缩映射的变形矩阵,并将它同显示对象的矩阵副本相乘,得到的矩阵将是同直接使用scale()方法等效的。

需要注意的是,该方法直接修改当前矩阵,所以如下的代码是不能达到预期的:

function flip_V_2():void

{

         flipstep+=0.1;

         flipMatrix.d=Math.sin(flipstep);

         objMatrix.concat(flipMatrix);

         flipobjbox.flipObj.transform.matrix=objMatrix;

}

因为在这段代码执行时,每次objMatrix都会被改变,每次执行都是在上次执行的结果上处理。正确的写法应该使用clone()方法,创建objMatrix的副本:

……

var flipMatrix:Matrix=new Matrix();

……

//函数主体

function flip_V_3():void{

flipstep+=0.1;

flipMatrix.d=Math.sin(flipstep);

trace("flipmatrix:"+flipMatrix.toString());

var tempMatrix:Matrix=objMatrix.clone();

trace("OriginalMatrix:"+tempMatrix.toString());

tempMatrix.concat(flipMatrix);

trace("Concat result matrix:"+tempMatrix.toString());

flipobjbox.flipObj.transform.matrix=tempMatrix;

}

……

 

function transtimerHandler(event:TimerEvent):void {

         //动画代码编写入口

         //flip_V_0();

         //flip_V_1();

         //flip_V_2();

         flip_V_3();

}

执行这段程序,动画的显示结果符合预期。

这段代码还有什么有优化的地方吗?我想加快运行速度 class FFN(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, drop=0., fused_if_available=True, D=None): super().__init__() self.fused_mlp_func = fused_mlp_func if fused_if_available else None out_features = out_features or in_features # 1024 hidden_features = hidden_features or in_features # 4096 #self.fc1 = nn.Linear(in_features, hidden_features) #self.fc2 = nn.Linear(hidden_features, out_features) self.act = nn.GELU(approximate='tanh') self.drop = nn.Dropout(drop, inplace=True) if drop > 0 else nn.Identity() # 设置router self.expert_num = 8 self.route = nn.Linear(D, self.expert_num) # 对Liner层进行分组 #self.fc1_list = nn.ModuleList([nn.Linear(in_features, hidden_features//self.expert_num, bias=True) for _ in range(self.expert_num)]) #self.fc2_list = nn.ModuleList([nn.Linear(hidden_features//self.expert_num, out_features, bias=True) for _ in range(self.expert_num)]) self.hidden_per_expert = hidden_features // self.expert_num self.fc1_weight = nn.Parameter(torch.Tensor(self.expert_num, self.hidden_per_expert, in_features)) self.fc1_bias = nn.Parameter(torch.Tensor(self.expert_num, self.hidden_per_expert)) self.fc2_weight = nn.Parameter(torch.Tensor(self.expert_num, out_features, self.hidden_per_expert)) self.fc2_bias = nn.Parameter(torch.Tensor(self.expert_num, out_features)) def forward(self, x, pn): if pn in [1, 2, 3, 4, 5, 6, 8, 10]: k = self.expert_num # 并行计算所有专家的fc1 fc1_out = torch.einsum('bsd,ehd->bseh', x, self.fc1_weight) # [batch_size, seq_len, expert_num, hidden_per_expert] fc1_out = fc1_out + self.fc1_bias.unsqueeze(0).unsqueeze(0) # 广播偏置 fc1_out = self.act(fc1_out) # 并行计算所有专家的fc2 fc2_out = torch.einsum('bseh,eoh->bseo', fc1_out, self.fc2_weight) # [batch_size, seq_len, expert_num, out_features] fc2_out = fc2_out + self.fc2_bias.unsqueeze(0).unsqueeze(0) # 广播偏置 fc2_out = self.drop(fc2_out) # 对所有专家的输出求和 [batch_size, seq_len, out_features] stu_proj = fc2_out.sum(dim=2) # 注意:原代码中最后乘以 (self.expert_num/k),当k等于expert_num时,这个因子为1,所以可以省略 return stu_proj else: k = int(self.expert_num * 0.75) gate = self.route(x) _, topk_indices = self.set_top_k(gate, k) # 2B, token, expert; # 2B, token, k # 并行计算fc1 (所有专家) fc1_out = torch.einsum('bsd,ehd->bseh', x, self.fc1_weight) # [batch_size, seq_len, expert_num, hidden_per_expert] fc1_out = fc1_out + self.fc1_bias.unsqueeze(0).unsqueeze(0) # 广播偏置 fc1_out = self.act(fc1_out) # 并行计算fc2 (所有专家) fc2_out = torch.einsum('bseh,eoh->bseo', fc1_out, self.fc2_weight) # [batch_size, seq_len, expert_num, out_features] fc2_out = fc2_out + self.fc2_bias.unsqueeze(0).unsqueeze(0) # 广播偏置 fc2_out = self.drop(fc2_out) topk_indices_exp = topk_indices.unsqueeze(-1).expand(-1, -1, -1, fc2_out.size(-1)) # [batch_size, seq_len, k, out_features] expert_outputs = torch.gather(fc2_out, dim=2, index=topk_indices_exp) # [batch_size, seq_len, k, out_features] stu_proj = expert_outputs.sum(dim=2) # [batch_size, seq_len, out_features] return stu_proj * (self.expert_num/k)
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值