Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module 'sonnet' has no attribute 'AbstractModule' #128

Closed
poweryin opened this issue May 7, 2019 · 19 comments
Closed

module 'sonnet' has no attribute 'AbstractModule' #128

poweryin opened this issue May 7, 2019 · 19 comments

Comments

@poweryin
Copy link

poweryin commented May 7, 2019

Hi,I've installed sonnet by using pip install dm-sonnet
However when I run "python evaluate_sample.py"
I got AttributeError: module 'sonnet' has no attribute 'AbstractModule'
Thanks!

@malcolmreynolds
Copy link
Collaborator

@poweryin where is this evaluate_sample.py from? Is it from the Kinetics repository?

Also, could you paste the output of running the following in your interpreter:

import sonnet as snt
print(dir(snt))
print(snt.__version__)

@poweryin
Copy link
Author

poweryin commented May 9, 2019

thanks,it is from kenetics-I3D repository.I figure it out .it is due to tensorflow's version is too low.thanks

@szq261299
Copy link

when i try to install the graph-nets , i meet the same problem.......

@tomhennigan
Copy link
Collaborator

@szq261299 would you mind printing the output of this in your Python interpreter:

import sonnet as snt
print(dir(snt))
print(snt.__version__)

@szq261299
Copy link

@tomhennigan
hello, my python version is 3.6.4, I followed your instructions and the results are as follows:

import sonnet as snt
print(dir(snt))
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

print(snt.__version__)
AttributeError                            Traceback (most recent call last)
<ipython-input-10-980fa547e124> in <module>
      1 import sonnet as snt
      2 print(dir(snt))
----> 3 print(snt.__version__)

AttributeError: module 'sonnet' has no attribute '__version__'

In order to find the version number of sonnet, I type the following on the command line.

pip list 
...
dm-sonnet                          1.33
...

so what should I do ?

@tomhennigan
Copy link
Collaborator

I can't reproduce this in a clean colab. Perhaps you have a file in your current directory called sonnet.py and you're importing that instead of the library? Can you try printing the output of print(snt.__file__)?

@szq261299
Copy link

print(snt.__file__)

AttributeError                            Traceback (most recent call last)
<ipython-input-5-86bcd137c072> in <module>
----> 1 print(snt.__file__)

AttributeError: module 'sonnet' has no attribute '__file__'

@malcolmreynolds
Copy link
Collaborator

@szq261299 what about print(snt.__path__)?

@szq261299
Copy link

When I uninstalled and reinstalled , it works now . But I can't reproduce this problem. It is so strange....

@szq261299
Copy link

Thanks a lot~

@ghost
Copy link

ghost commented Sep 17, 2019

When I uninstalled and reinstalled , it works now . But I can't reproduce this problem. It is so strange....

Uninstalling + reinstalling seems to be a solution that worked for me = =.

@ArnaudC
Copy link

ArnaudC commented Oct 6, 2019

Uninstalling and reinstalling is not working for me. (tf-gpu)

pip3 uninstall "dm-sonnet>=2.0.0b0"
pip3 uninstall "tensorflow-probability>=0.8.0rc0"

Then

pip3 install --upgrade tensorflow-gpu
pip3 install "tensorflow-probability>=0.8.0rc0" --pre
pip3 install "dm-sonnet>=2.0.0b0" --pre

My tf and Sonnet version :

print("TensorFlow version {}".format(tf.__version__))
print("Sonnet version {}".format(snt.__version__))

TensorFlow version 2.0.0
Sonnet version 2.0.0b0
Python 3.6.8
library cudart64_100.dll

<module 'sonnet' from 'F:\Python\Python 3.6.8\lib\site-packages\sonnet\init.py'>
module 'sonnet' has no attribute 'AbstractModule'
File "C:\Users\a\ImageGeneration\VAEv2.py", line 100, in
class Encoder(snt.AbstractModule):

@tomhennigan
Copy link
Collaborator

Hey @ArnaudC, if the code you're using wants snt.AbstractModule then it's likely that it's been designed for Sonnet 1 + TensorFlow 1 (in Sonnet 2 we just have snt.Module, docs here).

I'll see if we can give a better error message in this case, but for now you'll want to use the 1.X series of both TensorFlow and Sonnet.

Also (to save you some time) the current stable version of tensorflow-probability does not support the latest TF1 release so we should use the older version for now:

$ pip install "tensorflow-gpu<2" "dm-sonnet<2" "tensorflow-probability==0.7.0"

If you're developing new code then I would suggest using TensorFlow 2 and Sonnet 2 but if you have existing code that you don't want to rewrite then downgrading to TF1/Sonnet 1 should do the trick.

@lucky096
Copy link

Hey @tomhennigan, I need to upgrade to sonnet2/tf2, can you confirm is there any way to upgrade the code written in sonnet1 to sonnet2? For instance, the snt.AbstractModule from sonnet1 is now just snt.Module. I checked and they are not same. It seems backward compatibility is not there. So did you mean that to maintain models designed in sonnet1, we should stick to sonnet1/tf1 and we can't upgrade them to sonnet2/tf2?

@tomhennigan
Copy link
Collaborator

Hi @lucky096 there is no automated way to convert Sonnet 1 -> Sonnet 2 and the library is not backwards compatible.

The main change between the Sonnet 1 and 2 is how variables and submodules are created and used. Sonnet 2 doesn't have any magical reuse of modules or variables, instead users are expected to use normal Python code to manage this (e.g. save parameters as self.w = tf.Variable(...)).

We provide @snt.once to make a method run once and only once, this can make data dependent initialization (very common in most neural nets) more convenient.

# TensorFlow 2 / Sonnet 2
class AddNModule(snt.Module):

  @snt.once
  def _initialize(self, x):
    # Make sure to initialize variables and submodules once and only once.
    self.n = tf.Variable(tf.ones(x.shape))
    self.s = SomeSubModule()

  def __call__(self, x):
    self._initialize(x)
    return self.s(self.n + x)

# TensorFlow 1 / Sonnet 1
class AddNModule(snt.AbstractModule):
  def __call__(self, x):
    n = tf.get_variable("n", x.shape, initializer=tf.ones)
    return SomeSubModule()(n + x)

I have ported a lot of code between Sonnet 1 -> Sonnet 2 and for most modules moving initialization into a method annotated with @snt.once is all you need to change (aside from changing to snt.Module name and dealing with things that have been renamed in TensorFlow).

@tfqbasha
Copy link

tfqbasha commented Mar 3, 2020

Hey,
I was using deepmind's kinetics-i3d repository and got this AttributeError. Below are the versions of tf and snt I am using.
TensorFlow version 2.1.0
Sonnet version 2.0.0b0

Based on the discussion above, I see that AbstractModule is not used in sonnet2.0. Do I have to go back to sonnet1.0 or is there a kinetics-i3d based on sonnet2.0?

@malcolmreynolds
Copy link
Collaborator

@tfqbasha as mentioned above, models written in Sonnet v1 will not work unmodified with V2. I would suggest asking the authors of Kinetics directly whether they are planning on making a Sonnet v2 version available.

@AlexanderMelde
Copy link

@tfqbasha you can follow this issue google-deepmind/kinetics-i3d#85 to see if the developers will respond to the update request.

@BigfishWa
Copy link

@szq261299你介意在你的 Python 解释器中打印这个输出吗:

import sonnet as snt
print(dir(snt))
print(snt.__version__)

2022-05-20 11:39:34.302485: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
['BaseBatchNorm', 'BatchApply', 'BatchNorm', 'Bias', 'Conv1D', 'Conv1DLSTM', 'Conv1DTranspose', 'Conv2D', 'Conv2DLSTM', 'Conv2DTranspose', 'Conv3D', 'Conv3DLSTM', 'Conv3DTranspose', 'DeepRNN', 'Deferred', 'DepthwiseConv2D', 'Dropout', 'Embed', 'ExponentialMovingAverage', 'Flatten', 'GRU', 'GroupNorm', 'InstanceNorm', 'LSTM', 'LSTMState', 'LayerNorm', 'Linear', 'Mean', 'Metric', 'Module', 'Optimizer', 'RNNCore', 'Reshape', 'Sequential', 'Sum', 'TrainableState', 'UnrolledLSTM', 'UnrolledRNN', 'VanillaRNN', 'all', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'version', 'absolute_import', 'allow_empty_variables', 'build', 'custom_variable_getter', 'deep_rnn_with_residual_connections', 'deep_rnn_with_skip_connections', 'distribute', 'division', 'dynamic_unroll', 'flatten', 'format_variables', 'initializers', 'leaky_clip_by_value', 'log_variables', 'lstm_with_recurrent_dropout', 'merge_leading_dims', 'mixed_precision', 'nets', 'no_name_scope', 'once', 'optimizers', 'pad', 'print_function', 'regularizers', 'reshape', 'scale_gradient', 'split_leading_dim', 'static_unroll']
2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants