【人工智能笔记】第三十九节:TF2实现VITGAN对抗生成网络,ModulatedLinear调制模块 实现

网络结构图
该章节介绍VITGAN对抗生成网络中,ModulatedLinear调制模块 部分的代码实现。

目录(文章发布后会补上链接):

  1. 网络结构简介
  2. Mapping NetWork 实现
  3. PositionalEmbedding 实现
  4. MLP 实现
  5. MSA多头注意力 实现
  6. SLN自调制 实现
  7. CoordinatesPositionalEmbedding 实现
  8. ModulatedLinear 实现
  9. Siren 实现
  10. Generator生成器 实现
  11. PatchEmbedding 实现
  12. ISN 实现
  13. Discriminator鉴别器 实现
  14. VITGAN 实现

ModulatedLinear调制模块 简介

Generator生成器
论文原文
论文原文
ModulatedLinear调制模块 是用于将二维位置信息映射到模型特征中,增加模型细节,就是里面的 f θ ( E f o u , y i ) f_\theta(E_{fou},y^i) fθ(Efou,yi) ,对应图中 Fourier Embedding部分。

注意:该部分代码可能有误,欢迎留言指正!!!

代码实现

import tensorflow as tf
import numpy as np
import math

class ModulatedLinear(tf.keras.layers.Layer):
    def __init__(
        self,
        hidden_dim,
        output_dim,
        demodulation=True,
        use_bias=False,
        kernel_initializer=None,
    ):
        super().__init__()
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim
        self.demodulation = demodulation
        self.use_bias = use_bias
        self.kernel_initializer = kernel_initializer

    def build(self, inputs_shape):
        e_fou_shape, style_shape = inputs_shape
        # print('e_fou_shape:', e_fou_shape)
        # print('style_shape:', style_shape)
        self.scale = 1 / math.sqrt(self.hidden_dim)
        self.weight = self.add_weight(
            name='w',
            shape=[1,self.hidden_dim,self.output_dim],
            dtype=tf.float32,
            # initializer=tf.initializers.GlorotNormal(),
            initializer=self.kernel_initializer,
        )
        self.style_to_mod = tf.keras.layers.Dense(
            self.hidden_dim, 
            use_bias=self.use_bias,
            kernel_initializer=self.kernel_initializer,
        )

    def call(self, inputs):
        '''
        input: (B*L,P*P,E)
        style: (B, L, E=P*P*C)
        '''
        assert isinstance(inputs, tuple) and len(inputs) == 2
        x, style = inputs
        batch_size = tf.shape(x)[0] # B*L
        # Computing the weight
        style = self.style_to_mod(style)
        style = tf.reshape(style, [batch_size, self.hidden_dim, 1]) # (B*L, hidden_dim, 1)
        weight = self.scale * self.weight * style # (B*L, hidden_dim, output_dim)
        weight = weight / (tf.math.reduce_euclidean_norm(weight, axis=1, keepdims=True) + 1e-8) # [B*L, hidden_dim, output_dim]

        # Computing the out
        x = tf.matmul(x, weight) # (B*L, P*P, output_dim)
        return x


if __name__ == "__main__":
    output_dim = 3 # 16*16*3
    hidden_dim = 768 # 16*16*3
    kernel_initializer = tf.random_uniform_initializer(
        -1/output_dim,
        1/output_dim
    )
    layer = ModulatedLinear(
        hidden_dim=hidden_dim,
        output_dim=output_dim,
    )
    e_fou = tf.random.uniform([2*196,256,768], dtype=tf.float32)
    y = tf.random.uniform([2,196,768], dtype=tf.float32)
    o1 = layer((e_fou, y))
    tf.print('o1:', tf.shape(o1))

参考资料:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VITGAN是一种基于ViT架构的生成对抗网络。在VITGAN中,有几个部分需要代码实现,包括Mapping Network、ModulatedLinear调制模块等。 Mapping Network是VITGAN中的一个部分,它负责将输入的噪声转换为特征向量w。具体代码实现可以参考相关文献或代码库。 ModulatedLinear调制模块VITGAN中的另一个部分,它用于生成器的生成过程。具体的代码实现也可以在相关的文献或代码库中找到。 总之,VITGAN的代码实现涉及到Mapping Network、ModulatedLinear调制模块等部分,可以通过查阅相关文献或代码库来获取详细的代码实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【人工智能笔记】第三十三TF2实现VITGAN对抗生成网络,Mapping NetWork 实现](https://blog.csdn.net/highlevels/article/details/124871484)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【人工智能笔记第三十九TF2实现VITGAN对抗生成网络ModulatedLinear调制模块 实现](https://blog.csdn.net/highlevels/article/details/124962554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PPHT-H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值