不能做简单的变量初始化 - '数据类型不被理解'

时间:2016-06-22 18:36:52

标签: tensorflow

我正在使用Tensorflow上的CIFAR-10教程,但我无法使用任何变量声明。即使是简单的事情:

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

给出错误:

   ---------------------------------------------------------------------------
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

我迫不及待地想知道出了什么问题以及出了什么问题。

提前致谢!

2 个答案:

答案 0 :(得分:3)

我不熟悉本教程,但看起来你提供了tf.constant_initializer(0.0)作为数据类型,它返回一个初始值生成器来生成常量。 tf.get_variable()的第三个参数应该是变量的数据类型,对于biases变量,它通常类似于tf.float32或tf.float64。

答案 1 :(得分:0)

文档也让我失望。我只是希望对未来的读者更加明确。

tutorial

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

给出了一个建议,即可能只传递3件事就行了。错误。关键词论证需要明确。所以以下内容不起作用:

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

但以下代码现在可以使用:

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
希望它有所帮助。