Initialize carry of RNNCells now that it is an instance function

I’ve been using an older version of Flax, and was able to incorporate existing RNNCells into a custom RNNCell where I can keep its carry in case I want to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CustomRNNCell(nn.Module):
RNNCell: nn.recurrent.RNNCellBase
hidden_dims: int
output_dims: int
@nn.compact
def __call__(self, carry, inputs, train: bool):
x = nn.Dense(features=self.hidden_dims)(x)
x = nn.leaky_relu(x)
carry, x = self.RNNCell()(carry, x)
x = nn.Dense(features=self.hidden_dims//2)(x)
x = nn.leaky_relu(x)
x = nn.Dense(features=self.output_dims)(x)
x = nn.sigmoid(x)
return carry, x
def initialize_carry(self, rng, batch_dims, init_fn=nn.zeros):
return self.RNNCell.initialize_carry(rng, batch_dims, self.hidden_dims, init_fn)
</code>
<code>class CustomRNNCell(nn.Module): RNNCell: nn.recurrent.RNNCellBase hidden_dims: int output_dims: int @nn.compact def __call__(self, carry, inputs, train: bool): x = nn.Dense(features=self.hidden_dims)(x) x = nn.leaky_relu(x) carry, x = self.RNNCell()(carry, x) x = nn.Dense(features=self.hidden_dims//2)(x) x = nn.leaky_relu(x) x = nn.Dense(features=self.output_dims)(x) x = nn.sigmoid(x) return carry, x def initialize_carry(self, rng, batch_dims, init_fn=nn.zeros): return self.RNNCell.initialize_carry(rng, batch_dims, self.hidden_dims, init_fn) </code>
class CustomRNNCell(nn.Module):
    RNNCell: nn.recurrent.RNNCellBase
    hidden_dims: int
    output_dims: int

    @nn.compact
    def __call__(self, carry, inputs, train: bool):
        x = nn.Dense(features=self.hidden_dims)(x)
        x = nn.leaky_relu(x)

        carry, x = self.RNNCell()(carry, x)

        x = nn.Dense(features=self.hidden_dims//2)(x)
        x = nn.leaky_relu(x)
        x = nn.Dense(features=self.output_dims)(x)
        x = nn.sigmoid(x)

        return carry, x
    

    def initialize_carry(self, rng, batch_dims, init_fn=nn.zeros):
        return self.RNNCell.initialize_carry(rng, batch_dims, self.hidden_dims, init_fn)

This worked because initialize_carry was a class function. Now I upgraded to the new version, where I got errors because the init function was moved to be an instance function. I’m not sure what the easiest way would be to implement the previous behavior I had, since the RNNCell is only instantiated inside the call function…

I can’t have something like what I’ve seen in examples, since then there’s a conflict between the carry coming as an input and one defined through initialize_carry:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CustomRNNCell(nn.Module):
RNNCell: nn.recurrent.RNNCellBase
hidden_dims: int
output_dims: int
@nn.compact
def __call__(self, carry, inputs, train: bool):
x = nn.Dense(features=self.hidden_dims)(x)
x = nn.leaky_relu(x)
rnn_cell = self.RNNCell()
carry = rnn_cell.initialize_carry(...) # this would overwrite the input carry?
carry, x = self.RNNCell()(carry, x)
x = nn.Dense(features=self.hidden_dims//2)(h)
x = nn.leaky_relu(x)
x = nn.Dense(features=self.output_dims)(x)
x = nn.sigmoid(x)
return carry, x
</code>
<code>class CustomRNNCell(nn.Module): RNNCell: nn.recurrent.RNNCellBase hidden_dims: int output_dims: int @nn.compact def __call__(self, carry, inputs, train: bool): x = nn.Dense(features=self.hidden_dims)(x) x = nn.leaky_relu(x) rnn_cell = self.RNNCell() carry = rnn_cell.initialize_carry(...) # this would overwrite the input carry? carry, x = self.RNNCell()(carry, x) x = nn.Dense(features=self.hidden_dims//2)(h) x = nn.leaky_relu(x) x = nn.Dense(features=self.output_dims)(x) x = nn.sigmoid(x) return carry, x </code>
class CustomRNNCell(nn.Module):
    RNNCell: nn.recurrent.RNNCellBase
    hidden_dims: int
    output_dims: int

    @nn.compact
    def __call__(self, carry, inputs, train: bool):
        x = nn.Dense(features=self.hidden_dims)(x)
        x = nn.leaky_relu(x)

        rnn_cell = self.RNNCell()
        carry = rnn_cell.initialize_carry(...) # this would overwrite the input carry?
        carry, x = self.RNNCell()(carry, x)

        x = nn.Dense(features=self.hidden_dims//2)(h)
        x = nn.leaky_relu(x)
        x = nn.Dense(features=self.output_dims)(x)
        x = nn.sigmoid(x)

        return carry, x 

I see a way around this by using setup to instantiate the RNNCell, and then having the initialize_carry function as before use that. But I would prefer to avoid that since then I have to define everything in the setup function myself.

Is there a way to retain using the compact notation to get the carry initialization working as before? I’m guessing this change of having initialize_carry as a class vs instance method was introduced to make things simpler, not more complicated, so I feel I’m missing something.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật