TypeError:attr'Tindices'的DataType float32不在允许值列表中:int32,int64

时间:2017-06-11 07:34:11

标签: python python-2.7 tensorflow stanford-nlp

我在做斯坦福大学的CS224n课程。我在依赖解析器

中的assignment2 q2_parser_model.py中收到错误
== Initializing==

    Loading data... took 2.17 seconds
    Building parser... took 0.04 seconds
    Loading pretrained embeddings... took 2.16 seconds
    Vectorizing data... took 0.06 seconds
    Preprocessing training data...
    1000/1000 [==============================] - 1s     
    Building model...
    Traceback (most recent call last):
      File "q2_parser_model.py", line 286, in <module>
        main()
      File "q2_parser_model.py", line 252, in main
        model = ParserModel(config, embeddings)
      File "q2_parser_model.py", line 237, in __init__
        self.build()
      File "/home/jarvis/My projects/Machine Learning/CS224n/My assignments/assignment2/model.py", line 109, in build
        self.pred = self.add_prediction_op()
      File "q2_parser_model.py", line 149, in add_prediction_op
        x = self.add_embedding()
      File "q2_parser_model.py", line 119, in add_embedding
        features = tf.nn.embedding_lookup(embedding, self.input_placeholder)
      File "/home/jarvis/anaconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/embedding_ops.py", line 110, in embedding_lookup
        validate_indices=validate_indices)
      File "/home/jarvis/anaconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1293, in gather
        validate_indices=validate_indices, name=name)
      File "/home/jarvis/anaconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 582, in apply_op
        _Attr(op_def, input_arg.type_attr))
      File "/home/jarvis/anaconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint
        ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
    TypeError: DataType float32 for attr 'Tindices' not in list of allowed values: int32, int64

以下是我收到错误的代码段和行

def add_embedding(self):
        embedding = tf.Variable(self.pretrained_embeddings, name = "embedding")
    --> features = tf.nn.embedding_lookup(embedding, self.input_placeholder)
        embeddings = tf.reshape(features, [-1, self.config.n_features * 
        self.config.embedding_size])
        ### END YOUR CODE
        return embeddings

2 个答案:

答案 0 :(得分:1)

您的self.input_placeholder必须作为tf.nn.embedding_lookupint32的数组传递给int64,所以您可以:

features = tf.nn.embedding_lookup(embedding, 
                            np.asarray(self.input_placeholder, dtype=np.int32))

答案 1 :(得分:0)

我弄错了,这是由于数据类型定义不正确。我将占位符定义为float32而不是int32

self.labels_placeholder = tf.placeholder(tf.int32, shape=(self.config.batch_size, self.config.n_classes))