gwtw14
gwtw14

Reputation: 255

Cannot do simple initialization of variable - 'data type not understood'

I was running through the CIFAR-10 tutorial on Tensorflow, but I cannot get any of my variable declarations to work. Even something simple as:

biases = tf.get_variable('biases', [64], tf.constant_initializer(0.0))

gives the error:

   ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-86228512ca30> in <module>()
----> 1 biases = tf.get_variable('biases', [64], tf.constant_initializer(0.0))

/home/mmm/programs/anaconda/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)
    730       initializer=initializer, regularizer=regularizer, trainable=trainable,
    731       collections=collections, caching_device=caching_device,
--> 732       partitioner=partitioner, validate_shape=validate_shape)
    733 
    734 

/home/mmm/programs/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape)
    594           regularizer=regularizer, reuse=self.reuse, trainable=trainable,
    595           collections=collections, caching_device=caching_device,
--> 596           partitioner=partitioner, validate_shape=validate_shape)
    597 
    598   def _get_partitioned_variable(

/home/mmm/programs/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape)
    159         initializer=initializer, regularizer=regularizer, reuse=reuse,
    160         trainable=trainable, collections=collections,
--> 161         caching_device=caching_device, validate_shape=validate_shape)
    162 
    163   def _get_partitioned_variable(

/home/mmm/programs/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _get_single_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, validate_shape)
    423 
    424     should_check = reuse is not None
--> 425     dtype = dtypes.as_dtype(dtype)
    426     shape = tensor_shape.as_shape(shape)
    427 

/home/mmm/programs/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/dtypes.pyc in as_dtype(type_value)
    534 
    535   for key, val in _NP_TO_TF:
--> 536     if key == type_value:
    537       return val
    538 

TypeError: data type not understood

I am desperate to find out what is wrong and where it went wrong.

Thanks in advance!

Upvotes: 3

Views: 1895

Answers (2)

Charlie Parker
Charlie Parker

Reputation: 5291

The documentation also put me off. I just want to be more explicit for future readers.

The tutorial has

`tf.get_variable(<name>, <shape>, <initializer>)`: Creates or returns a variable with a given name.

which gave the suggestion that maybe just passing 3 things would work. Wrong. Need to be explicit on the key word argument. So the following won't work:

def get_mdl_get_var(x):
    # variables for parameters
    W = tf.get_variable('W', [784, 10], tf.random_normal_initializer(mean=0.0,stddev=0.1))
    b = tf.get_variable('b', [10], tf.constant_initializer(value=0.1))
    Wx_b = tf.matmul(x, W)+b
    y = tf.nn.softmax(Wx_b)
    return y

but the following code works now:

def get_mdl_get_var(x):
    # variables for parameters
    W = tf.get_variable(name='W', shape=[784, 10], initializer=tf.random_normal_initializer(mean=0.0,stddev=0.1))
    b = tf.get_variable(name='b', shape=[10], initializer=tf.constant_initializer(value=0.1))
    Wx_b = tf.matmul(x, W)+b
    y = tf.nn.softmax(Wx_b)
    return y

hope it helps.

Upvotes: 0

bradden_gross
bradden_gross

Reputation: 678

I'm not familiar with the tutorial but it looks like you provided tf.constant_initializer(0.0) as your data type which returns an initializer to generate constants. The third parameter of tf.get_variable() should be the data type of your variable which for a biases variable is usually something like tf.float32 or tf.float64.

Upvotes: 3

Related Questions