Tensorflow : ValueError: The initializer passed is not valid. It should be a callable with no arguments and `shape` should be fully defined

Tensorflow : ValueError: The initializer passed is not valid. It should be a callable with no arguments and `shape` should be fully defined

我正在尝试使用 TensorFlow 模型,但出现此错误:

ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of tf.keras.initializers.*' andshape` should be fully defined.

代码如下所示:

      sentences             = tf.placeholder(tf.int32, [None,None], name='sentences')
      self.targets          = tf.placeholder(tf.int32, [None, None], name='labels'  )

      word_embedding            = tf.get_variable(name='word_embedding_',
                                     shape=[vocab_size, word_embedding_dim],
                                     dtype=tf.float32,
                                     initializer = tf.contrib.layers.xavier_initializer())




    # lookup and sequence count ----------------------------------------------------------->>
    # embedding lookup
    embedding_lookup = tf.nn.embedding_lookup(word_embedding, sentences)

    # ignore padding during sequence unfolding in lstm
    sequence_leng = tf.count_nonzero(sentences,axis=-1)



    # sequence learning network ------------------------------------------------------------->>

    #bilstm model
    with tf.variable_scope('forward'):
        fr_cell = tf.contrib.rnn.LSTMCell(num_units = rnn_units)
        dropout_fr = tf.contrib.rnn.DropoutWrapper(fr_cell, output_keep_prob = 1. - keep_prob)

    with tf.variable_scope('backward'):
        bw_cell = tf.contrib.rnn.LSTMCell(num_units = rnn_units)
        dropout_bw = tf.contrib.rnn.DropoutWrapper(bw_cell, output_keep_prob = 1. - keep_prob)

    with tf.variable_scope('encoder') as scope:
        model,last_state = tf.nn.bidirectional_dynamic_rnn(dropout_fr,
                                                           dropout_bw,
                                                           inputs=embedding_lookup,
                                                           sequence_length=sequence_leng,
                                                           dtype=tf.float32)


    logits = tf.concat([last_state[0].c,last_state[1].c],axis=-1)






    # dense layer --------------------------------------------------------------------->>

    # dense layer with xavier weights
    fc_layer = tf.get_variable(name='fully_connected',
                               shape=[2*rnn_units, self.targets.shape[1]],
                               dtype=tf.float32,
                               initializer=tf.contrib.layers.xavier_initializer())

    # bias 
    bias    = tf.get_variable(name='bias',
                               shape=[self.targets.shape[1]],
                               dtype=tf.float32,
                               initializer=tf.contrib.layers.xavier_initializer())

    #final output 
    self.x_ = tf.add(tf.matmul(logits,fc_layer),bias)




    #optimization and loss calculation ---------------------------------->>

问题出在以下几行,

fc_layer = tf.get_variable(name='fully_connected',
                           shape=[2*rnn_units, self.targets.shape[1]],
                           dtype=tf.float32,
                           initializer=tf.contrib.layers.xavier_initializer())

# bias 
bias = tf.get_variable(name='bias',
                       shape=[self.targets.shape[1]],
                       dtype=tf.float32,
                       initializer=tf.contrib.layers.xavier_initializer())

如果您查看 self.targets.shape[1],则等于 None。 TensorFlow 不允许您生成 tf.Variable 个未定义完整形状的对象。这意味着它们的 none 个维度可以是 None。因此,你得到了错误。

因此,进行以下更改应该可以解决该问题。我只是为 targets 占位符的最后一个维度赋值。

n_units = <some value>

sentences = tf.placeholder(tf.int32, [None,None], name='sentences')
self.targets = tf.placeholder(tf.int32, [None, n_units], name='labels'  )

word_embedding = tf.get_variable(name='word_embedding_',
                                     shape=[vocab_size, word_embedding_dim],
                                     dtype=tf.float32,
                                     initializer = tf.contrib.layers.xavier_initializer())

# ... this part stays the same

# dense layer --------------------------------------------------------------------->>

# dense layer with xavier weights
fc_layer = tf.get_variable(name='fully_connected',
                            shape=[2*rnn_units, self.targets.shape[1]],
                            dtype=tf.float32,
                            initializer=tf.contrib.layers.xavier_initializer())

# bias 
bias = tf.get_variable(name='bias',
                            shape=[self.targets.shape[1]],
                            dtype=tf.float32,
                            initializer=tf.contrib.layers.xavier_initializer())

#final output 
self.x_= tf.add(tf.matmul(logits,fc_layer),bias)