Keras: Initializer type '<dtype: 'string'>' and explicit dtype '<dtype: 'float32'>' don't match

Created on 10 Jan 2018  路  5Comments  路  Source: keras-team/keras

I'm writing my own layer in example_layer.py as follows.

`from tensorflow.python.keras.layers import Layer

class MyLayer(Layer):

def __init__(self, **kwargs):
    super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):

    self.kernel = self.add_weight(name='kernel', shape=input_shape[1:], initializer='uniform', trainable=True)
    super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x):
    return x * self.kernel

def compute_output_shape(self, input_shape):
    return input_shape`

but raise Value error: Initializer type '' and explicit dtype '' don't match

Most helpful comment

Finally, I solved the problem. The reason is that I ran the above code in ipython notebook for many times. Instead, i directly ran the code in .py file, and nothing happends. The issue can be closed.
Thanks to taehoonlee

All 5 comments

tf.keras.__version__ = 2.0.8
tensorflow gpu version

Please try:

from tensorflow.python.keras import models
from tensorflow.python.keras import layers
from tensorflow.python.keras import initializers

class MyLayer(layers.Layer):
    def __init__(self, **kwargs):
        super(MyLayer, self).__init__(**kwargs)
        self.kernel_initializer = initializers.get('uniform')

    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel', shape=input_shape[1:],
                                      initializer=self.kernel_initializer, trainable=True)
        super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!

    def call(self, x):
        return x * self.kernel

    def compute_output_shape(self, input_shape):
        return input_shape

model = models.Sequential()
model.add(MyLayer(input_shape=(5,)))

I just tried it, but it does not work

Traceback


ValueError Traceback (most recent call last)
in ()
----> 1 cnn_model = creata_cnn_model()

in creata_cnn_model()
48 out = Reshape(x_train_cnn.shape[-2:], name='reshape_cnn')(out)
49 # out = Convolution2D(1, kernel_size=, data_format='channels_first', padding='valid')(out)
---> 50 out = MyLayer(name='my_layer')(out)
51
52 input_model = Input(shape=x_train_model.shape[1:])

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.pyc in __call__(self, inputs, kwargs)
250 """
251 # Actually call the layer (optionally building it).
*--> 252 output = super(Layer, self).__call__(inputs, *kwargs)

253
254 # Update learning phase info.

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.pyc in __call__(self, inputs, args, *kwargs)
557 input_shapes = [x.get_shape() for x in input_list]
558 if len(input_shapes) == 1:
--> 559 self.build(input_shapes[0])
560 else:
561 self.build(input_shapes)

/home/shawn/shawn/tianchi/wuren_1205/data/example_layer.py in build(self, input_shape)
8 self.kernel_initializer = initializers.get('uniform')
9
---> 10 def build(self, input_shape):
11 print input_shape
12 self.kernel = self.add_weight(name='kernel', shape=input_shape[1:], initializer=self.kernel_initializer, trainable=True)

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.pyc in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
212 regularizer=regularizer,
213 constraint=constraint,
--> 214 trainable=trainable)
215 return weight
216

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.pyc in add_variable(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
456 dtype=dtypes.as_dtype(dtype),
457 constraint=constraint,
--> 458 trainable=trainable and self.trainable)
459 if variable in existing_variables:
460 return variable

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
1201 partitioner=partitioner, validate_shape=validate_shape,
1202 use_resource=use_resource, custom_getter=custom_getter,
-> 1203 constraint=constraint)
1204 get_variable_or_local_docstring = (
1205 """%s

/home/shawn/miniconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
1077 if init_dtype != dtype:
1078 raise ValueError("Initializer type '%s' and explicit dtype '%s' "
-> 1079 "don't match." % (init_dtype, dtype))
1080 if initializer is None:
1081 initializer = self._initializer

ValueError: Initializer type '' and explicit dtype '' don't match.

Finally, I solved the problem. The reason is that I ran the above code in ipython notebook for many times. Instead, i directly ran the code in .py file, and nothing happends. The issue can be closed.
Thanks to taehoonlee

Was this page helpful?
0 / 5 - 0 ratings