Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Python sample code for Sonnet
    claude sonnet


Python sample code for Sonnet


Here is a simple example of using Sonnet in Python to create a neural network module:

python
import sonnet as snt
import tensorflow as tf

class MyModule(snt.Module):
    def __init__(self, output_size, name=None):
        super().__init__(name=name)
        self.output_size = output_size
        
    def __call__(self, x):
        w = tf.Variable(tf.random.normal([x.shape[-1], self.output_size]), name="w")
        b = tf.Variable(tf.zeros([self.output_size]), name="b")
        return tf.matmul(x, w) + b

# Create an instance of the module  
mod = MyModule(10)

# Call the module with some input data
data = tf.ones([32, 100])
output = mod(data)

print(output)

This creates a simple linear layer module by subclassing `snt.Module`[1][3]. The module has an `__init__` method to define constructor arguments, and a `__call__` method that defines the computation.

The module automatically manages the variables `w` and `b` as part of the module instance[3]. Calling the module with input data `x` applies the linear transformation.

Sonnet provides a simple, composable abstraction for building neural networks in TensorFlow[1][5]. It encourages organizing code into reusable module classes with managed variables and parameters[3].

Citations:
[1] https://github.com/google-deepmind/sonnet
[2] https://github.com/google-deepmind/sonnet/blob/v2/examples/vqvae_example.ipynb
[3] https://sonnet.readthedocs.io/en/latest/modules.html
[4] https://www.sonnetsoftware.com/support/downloads/Aces2011/13_11_20111096.pdf
[5] https://sonnet.dev