jas*_*son 2 python typeerror tensorflow
我有一个在深度学习中实现的代码Tensorflow。我使用Keras模块
self.n_clusters = 10
self.alpha = 0.01
clustering_layer = ClusteringLayer(self.n_clusters, alpha=self.alpha, name='clustering')(hidden)
Run Code Online (Sandbox Code Playgroud)
我的错误主要来自上面,所以我附上了它。
它给了我以下错误:
--> 118 clustering_layer = ClusteringLayer(self.n_clusters, alpha=self.alpha, name='clustering')(hidden)
119 self.model = Model(inputs=self.autoencoder.input, outputs=clustering_layer)
120
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/keras/engine/base_layer.pyc in __call__(self, inputs, *args, **kwargs)
694 if all(hasattr(x, 'get_shape') for x in input_list):
695 input_shapes = nest.map_structure(lambda x: x.get_shape(), inputs)
--> 696 self.build(input_shapes)
697
698 # Check input assumptions set after layer building, e.g. input shape.
<ipython-input-13-8890754cc8a3> in build(self, input_shape)
73 input_dim = input_shape[1]
74 self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim))
---> 75 self.clusters = self.add_weight(shape=(self.n_clusters, input_dim), initializer='glorot_uniform', name='clusters')
76 if self.initial_weights is not None:
77 self.set_weights(self.initial_weights)
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/keras/engine/base_layer.pyc in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, getter)
532 trainable=trainable and self.trainable,
533 partitioner=partitioner,
--> 534 use_resource=use_resource)
535
536 if regularizer is not None:
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/training/checkpointable/base.pyc in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
495 new_variable = getter(
496 name=name, shape=shape, dtype=dtype, initializer=initializer,
--> 497 **kwargs_for_getter)
498
499 # If we set an initializer and the variable processed it, tracking will not
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/keras/engine/base_layer.pyc in make_variable(name, shape, dtype, initializer, partition_info, trainable, caching_device, validate_shape, constraint, use_resource, partitioner)
1871 validate_shape=validate_shape,
1872 constraint=constraint,
-> 1873 use_resource=use_resource)
1874 return v
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in variable(initial_value, trainable, collections, validate_shape, caching_device, name, dtype, constraint, use_resource)
2232 name=name, dtype=dtype,
2233 constraint=constraint,
-> 2234 use_resource=use_resource)
2235
2236
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in <lambda>(**kwargs)
2222 constraint=None,
2223 use_resource=None):
-> 2224 previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
2225 for getter in ops.get_default_graph()._variable_creator_stack: # pylint: disable=protected-access
2226 previous_getter = _make_getter(getter, previous_getter)
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in default_variable_creator(next_creator, **kwargs)
2194 collections=collections, validate_shape=validate_shape,
2195 caching_device=caching_device, name=name, dtype=dtype,
-> 2196 constraint=constraint)
2197 elif not use_resource and context.executing_eagerly():
2198 raise RuntimeError(
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/resource_variable_ops.pyc in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint)
310 name=name,
311 dtype=dtype,
--> 312 constraint=constraint)
313
314 # pylint: disable=unused-argument
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/resource_variable_ops.pyc in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, constraint)
415 with ops.name_scope("Initializer"), ops.device(None):
416 initial_value = ops.convert_to_tensor(
--> 417 initial_value(), name="initial_value", dtype=dtype)
418 self._handle = _eager_safe_variable_handle(
419 shape=initial_value.get_shape(),
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/keras/engine/base_layer.pyc in <lambda>()
1858 initializer = initializer(dtype=dtype)
1859 init_val = lambda: initializer( # pylint: disable=g-long-lambda
-> 1860 shape, dtype=dtype, partition_info=partition_info)
1861 variable_dtype = dtype.base_dtype
1862 if use_resource is None:
/usr/local/python/2.7-conda5.2/lib/python2.7/site-packages/tensorflow/python/ops/init_ops.pyc in __call__(self, shape, dtype, partition_info)
466 scale /= max(1., fan_out)
467 else:
--> 468 scale /= max(1., (fan_in + fan_out) / 2.)
469 if self.distribution == "normal":
470 stddev = math.sqrt(scale)
TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'
Run Code Online (Sandbox Code Playgroud)
第 118 行是我的代码位置。该错误似乎发生在 tensorflow 包中。它给了我TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'. 我同时尝试python 2.7和python 3.6但有同样的问题。
如何处理这种情况?
一个非常相似的情况在github 中,它的错误可以在其代码中解决,但我的错误似乎发生在 init_ops.pyc
这里的问题似乎是 ClusteringLayer 传递了一个 tf.Dimension 对象作为簇数,用于初始化权重。改为执行 int(self.n_clusters) 以绕过维度对象问题。
| 归档时间: |
|
| 查看次数: |
3895 次 |
| 最近记录: |