TypeError:DataType float32 for attr 'Tindices&

2019-06-11 02:36发布

I am doing Stanford's CS224n course. I get an error in assignment2 q2_parser_model.py in my dependency parser

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

Following is the code snippet and line where I am getting error

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条回答
相关推荐>>
2楼-- · 2019-06-11 02:54

I got my mistake, it was due to an improper datatype definition. I defined a placeholder as float32 instead of int32

self.labels_placeholder = tf.placeholder(tf.int32, shape=(self.config.batch_size, self.config.n_classes))
查看更多
冷血范
3楼-- · 2019-06-11 03:02

Your self.input_placeholder must be passed to tf.nn.embedding_lookup as an array of int32 or int64 , so you could just:

features = tf.nn.embedding_lookup(embedding, 
                            np.asarray(self.input_placeholder, dtype=np.int32))
查看更多
登录 后发表回答