cpmpercussion / keras-mdn-layer

An MDN Layer for Keras using TensorFlow's distributions module
MIT License
165 stars 44 forks source link

The added layer must be an instance of class Layer. Found: <mdn.MDN object at 0x7fbd511f1860> #23

Closed schneider128k closed 4 years ago

schneider128k commented 4 years ago

I get an error message when trying to use the code below How to use in the readme file.

I am using colab with Python 3.6.9 and TensorFlow 1.15.0.

!python --version

prints out Python 3.6.9 and

%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)

prints out 1.15.0.

I have installed the latest version of the keras-mdn-layer package with

!pip install keras-mdn-layer

Collecting keras-mdn-layer
  Downloading https://files.pythonhosted.org/packages/f3/90/7c9233a1b334bf91bc7f9ec2534eb40f7bb418900f35cbd201864c600cf6/keras-mdn-layer-0.3.0.tar.gz
Building wheels for collected packages: keras-mdn-layer
  Building wheel for keras-mdn-layer (setup.py) ... done
  Created wheel for keras-mdn-layer: filename=keras_mdn_layer-0.3.0-cp36-none-any.whl size=7054 sha256=e53024a3d12d2c6bc1faa4ef682c44b5dbf2e8f6cad7ad8876a9cbecb84b666b
  Stored in directory: /root/.cache/pip/wheels/b6/e3/ba/8fb07898b8c8e5d4c1a035add0b71629b2fbe82ee8a5f0a2c8
Successfully built keras-mdn-layer
Installing collected packages: keras-mdn-layer
Successfully installed keras-mdn-layer-0.3.0

Code (copied from the readme file without any changes):

import keras
import mdn

N_HIDDEN = 15  # number of hidden units in the Dense layer
N_MIXES = 10  # number of mixture components
OUTPUT_DIMS = 2  # number of real-values predicted by each mixture component

model = keras.Sequential()
model.add(keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu'))
model.add(mdn.MDN(OUTPUT_DIMS, N_MIXES))
model.compile(loss=mdn.get_mixture_loss_func(OUTPUT_DIMS,N_MIXES), optimizer=keras.optimizers.Adam())
model.summary()

Error message :

Using TensorFlow backend.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-5428ceff4415> in <module>()
      8 model = keras.Sequential()
      9 model.add(keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu'))
---> 10 model.add(mdn.MDN(OUTPUT_DIMS, N_MIXES))
     11 model.compile(loss=mdn.get_mixture_loss_func(OUTPUT_DIMS,N_MIXES), optimizer=keras.optimizers.Adam())
     12 model.summary()

/usr/local/lib/python3.6/dist-packages/keras/engine/sequential.py in add(self, layer)
    131             raise TypeError('The added layer must be '
    132                             'an instance of class Layer. '
--> 133                             'Found: ' + str(layer))
    134         self.built = False
    135         if not self._layers:

TypeError: The added layer must be an instance of class Layer. Found: <mdn.MDN object at 0x7f65b87e3da0>

What am I missing?

cpmpercussion commented 4 years ago

Hi --- whoops, the readme code is slightly out of date! We've switched over to using tensorflow.keras which has some slight differences in how the Layer class is treated.

This can be fixed by using from tensorflow import keras instead of import keras.

E.g.:

from tensorflow import keras
import mdn

N_HIDDEN = 15  # number of hidden units in the Dense layer
N_MIXES = 10  # number of mixture components
OUTPUT_DIMS = 2  # number of real-values predicted by each mixture component

model = keras.Sequential()
model.add(keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu'))
model.add(mdn.MDN(OUTPUT_DIMS, N_MIXES))
model.compile(loss=mdn.get_mixture_loss_func(OUTPUT_DIMS,N_MIXES), optimizer=keras.optimizers.Adam())
model.summary()

If you want to use regular keras, the previous version (v0.2.3) still supports it and is available on PyPI.

schneider128k commented 4 years ago

Thanks a lot for your help! The above code works perfectly.