Tensorflow类型错误:使用`tf.张量`作为Python,不允许使用“bool”。

2024-04-28 13:14:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用tensorflow训练“显示并告诉”模型,其中模型自动生成图像的标题。我怎么会犯这个错误。在

这是回溯:

TypeError                                 Traceback (most recent call 
last)
<ipython-input-15-b6da0a27b701> in <module>()
  1 try:
  2     #train(.001,False,False) #train from scratch
----> 3     train(.001,True,True)    #continue training from pretrained weights @epoch500
  4     #train(.001)  #train from previously saved weights
  5 except keyboardInterrupt:

<ipython-input-14-39693d0edd0a> in train(learning_rate, continue_training, transfer)
 23     n_words = len(wordtoix)
 24     maxlen = np.max( [x for x in map(lambda x: len(x.split(' ')), captions) ] )
---> 25     caption_generator = Caption_Generator(dim_in, dim_hidden, dim_embed, batch_size, maxlen+2, n_words, init_b)
 26 
 27     loss, image, sentence, mask = caption_generator.build_model()

<ipython-input-12-7ef491a16183> in __init__(self, dim_in, dim_embed, dim_hidden, batch_size, n_lstm_steps, n_words, init_b)
 11         # declare the variables to be used for our word embeddings
 12         with tf.device("/cpu:0"):
---> 13             self.word_embedding = tf.get_variable("word_embedding", tf.random_uniform([self.n_words, self.dim_embed], -0.1, 0.1))
 14 
 15             self.embedding_bias = tf.get_variable("embedding_bias", tf.zeros([dim_embed]))

/home/niraj/anaconda2/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)
1063       collections=collections, caching_device=caching_device,
1064       partitioner=partitioner, validate_shape=validate_shape,
-> 1065       use_resource=use_resource, custom_getter=custom_getter)
1066 get_variable_or_local_docstring = (
1067     """%s

/home/niraj/anaconda2/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)
960           collections=collections, caching_device=caching_device,
961           partitioner=partitioner, validate_shape=validate_shape,
--> 962           use_resource=use_resource, custom_getter=custom_getter)
963 
964   def _get_partitioned_variable(self,

/home/niraj/anaconda2/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, use_resource, custom_getter)
365           reuse=reuse, trainable=trainable, collections=collections,
366           caching_device=caching_device, partitioner=partitioner,
--> 367           validate_shape=validate_shape, use_resource=use_resource)
368 
369   def _get_partitioned_variable(

/home/niraj/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource)
301                      trainable=True, collections=None, caching_device=None,
302                      partitioner=None, validate_shape=True, use_resource=None):
--> 303       is_scalar = shape is not None and not shape
304       # Partitioned variable case
305       if partitioner is not None and not is_scalar:

/home/niraj/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in __nonzero__(self)
511       `TypeError`.
512     """
--> 513     raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
514                     "Use `if t is not None:` instead of `if t:` to test if a "
515                     "tensor is defined, and use TensorFlow ops such as "

类型错误:不允许使用tf.Tensor作为Python bool。使用if t is not None:代替if t:来测试是否定义了张量,并使用张量流操作,例如传输条件执行以张量值为条件的子图。

代码如下:

^{pr2}$

Tags: inselfnonegetusedevicevalidatevariable
1条回答
网友
1楼 · 发布于 2024-04-28 13:14:38

问题源于将tf.Tensor作为^{}shape参数传递给此行:

self.word_embedding = tf.get_variable("word_embedding", tf.random_uniform([self.n_words, self.dim_embed], -0.1, 0.1))

我想你是想把随机张量作为initializer参数传递。解决此问题的最简单方法是为参数指定名称:

^{pr2}$

似乎所有对tf.get_variable()的调用都需要类似的修复。在

相关问题 更多 >