【问题标题】:Tensorflow: TypeError: get_variable() got multiple values for keyword argument 'name'Tensorflow:TypeError:get_variable()为关键字参数'name'获得了多个值
【发布时间】:2018-03-12 18:03:16
【问题描述】:

我正在使用 tensorflow 训练“展示和讲述”模型,其中模型会自动生成图像的标题。我怎么会收到这个错误。

这是回溯:

TypeError                                 Traceback (most recent call 
last)
<ipython-input-14-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-13-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-1b31c4175b3a> 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(tf.random_uniform([self.n_words, self.dim_embed], -0.1, 0.1), name='word_embedding')
 14 
 15             self.embedding_bias = tf.get_variable(tf.zeros([dim_embed]), name='embedding_bias')

TypeError:get_variable() 为关键字参数“名称”获取了多个值

问题可能是我向 get_variable 初始化程序传递了一些额外的参数,但我无法追踪问题发生的位置。

代码如下:

def get_data(annotation_path, feature_path):
 annotations = pd.read_table(annotation_path, sep='\t', header=None, names=['image', 'caption'])
 return np.load(feature_path,'r'), annotations['caption'].values

def preProBuildWordVocab(sentence_iterator, word_count_threshold=30): # function from Andre Karpathy's NeuralTalk
print('preprocessing %d word vocab' % (word_count_threshold, ))
word_counts = {}
nsents = 0
for sent in sentence_iterator:
  nsents += 1
  for w in sent.lower().split(' '):
    word_counts[w] = word_counts.get(w, 0) + 1
vocab = [w for w in word_counts if word_counts[w] >= word_count_threshold]
print('preprocessed words %d -> %d' % (len(word_counts), len(vocab)))


ixtoword = {}
ixtoword[0] = '.'  
wordtoix = {}
wordtoix['#START#'] = 0 
ix = 1
for w in vocab:
  wordtoix[w] = ix
  ixtoword[ix] = w
  ix += 1

word_counts['.'] = nsents
bias_init_vector = np.array([1.0*word_counts[ixtoword[i]] for i in ixtoword])
bias_init_vector /= np.sum(bias_init_vector) 
bias_init_vector = np.log(bias_init_vector)
bias_init_vector -= np.max(bias_init_vector) 
return wordtoix, ixtoword, bias_init_vector.astype(np.float32)

class Caption_Generator():
def __init__(self, dim_in, dim_embed, dim_hidden, batch_size, n_lstm_steps, n_words, init_b):

    self.dim_in = dim_in
    self.dim_embed = dim_embed
    self.dim_hidden = dim_hidden
    self.batch_size = batch_size
    self.n_lstm_steps = n_lstm_steps
    self.n_words = n_words

    # declare the variables to be used for our word embeddings
    with tf.device("/cpu:0"):
        self.word_embedding = tf.get_variable(tf.random_uniform([self.n_words, self.dim_embed], -0.1, 0.1), name='word_embedding')

        self.embedding_bias = tf.get_variable(tf.zeros([dim_embed]), name='embedding_bias')

    # declare the LSTM itself
        self.lstm = tf.contrib.rnn.BasicLSTMCell(dim_hidden)

    # declare the variables to be used to embed the image feature embedding to the word embedding space
        self.img_embedding = tf.get_variable(tf.random_uniform([dim_in, dim_hidden], -0.1, 0.1), name='img_embedding')
        self.img_embedding_bias = tf.get_variable(tf.zeros([dim_hidden]), name='img_embedding_bias')

    # declare the variables to go from an LSTM output to a word encoding output
        self.word_encoding = tf.get_variable(tf.random_uniform([dim_hidden, n_words], -0.1, 0.1), name='word_encoding')
    # initialize this bias variable from the preProBuildWordVocab output
        self.word_encoding_bias = tf.get_variable(init_b, name='word_encoding_bias')

def build_model(self):
    # declaring the placeholders for our extracted image feature vectors, our caption, and our mask
    # (describes how long our caption is with an array of 0/1 values of length `maxlen`  
    img = tf.placeholder(tf.float32, [self.batch_size, self.dim_in])
    caption_placeholder = tf.placeholder(tf.int32, [self.batch_size, self.n_lstm_steps])
    mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])

    # getting an initial LSTM embedding from our image_imbedding
    image_embedding = tf.matmul(img, self.img_embedding) + self.img_embedding_bias

    # setting initial state of our LSTM
    state = self.lstm.zero_state(self.batch_size, dtype=tf.float32)

    total_loss = 0.0
    with tf.variable_scope("RNN"):
        for i in range(self.n_lstm_steps): 
            if i > 0:
               #if this isn’t the first iteration of our LSTM we need to get the word_embedding corresponding
               # to the (i-1)th word in our caption 
                with tf.device("/cpu:0"):
                    current_embedding = tf.nn.embedding_lookup(self.word_embedding, caption_placeholder[:,i-1]) + self.embedding_bias
            else:
                 #if this is the first iteration of our LSTM we utilize the embedded image as our input 
                current_embedding = image_embedding
            if i > 0: 
                # allows us to reuse the LSTM tensor variable on each iteration
                tf.get_variable_scope().reuse_variables()

                out, state = self.lstm(current_embedding, state)
                    #out, state = self.tf.nn.dynamic_rnn(current_embedding, state)


            if i > 0:
                #get the one-hot representation of the next word in our caption 
                labels = tf.expand_dims(caption_placeholder[:, i], 1)
                ix_range=tf.range(0, self.batch_size, 1)
                ixs = tf.expand_dims(ix_range, 1)
                concat = tf.concat([ixs, labels],1)
                onehot = tf.sparse_to_dense(
                concat, tf.stack([self.batch_size, self.n_words]), 1.0, 0.0)


                #perform a softmax classification to generate the next word in the caption
                logit = tf.matmul(out, self.word_encoding) + self.word_encoding_bias
                xentropy = tf.nn.softmax_cross_entropy_with_logits(logits=logit, labels=onehot)
                xentropy = xentropy * mask[:,i]

                loss = tf.reduce_sum(xentropy)
                total_loss += loss

        total_loss = total_loss / tf.reduce_sum(mask[:,1:])
        return total_loss, img,  caption_placeholder, mask

### Parameters ###
dim_embed = 256
dim_hidden = 256
dim_in = 4096
batch_size = 128
momentum = 0.9
n_epochs = 150

def train(learning_rate=0.001, continue_training=False, transfer=True):

tf.reset_default_graph()

feats, captions = get_data(annotation_path, feature_path)
wordtoix, ixtoword, init_b = preProBuildWordVocab(captions)

np.save('data/ixtoword', ixtoword)

index = (np.arange(len(feats)).astype(int))
np.random.shuffle(index)


sess = tf.InteractiveSession()
n_words = len(wordtoix)
maxlen = np.max( [x for x in map(lambda x: len(x.split(' ')), captions) ] )
caption_generator = Caption_Generator(dim_in, dim_hidden, dim_embed, batch_size, maxlen+2, n_words, init_b)

loss, image, sentence, mask = caption_generator.build_model()

saver = tf.train.Saver(max_to_keep=100)
global_step=tf.Variable(0,trainable=False)
learning_rate = tf.train.exponential_decay(learning_rate, global_step,
                                   int(len(index)/batch_size), 0.95)
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss)
tf.global_variables_initializer().run()

if continue_training:
    if not transfer:
        saver.restore(sess,tf.train.latest_checkpoint(model_path))
    else:
        saver.restore(sess,tf.train.latest_checkpoint(model_path_transfer))
losses=[]
for epoch in range(n_epochs):
    for start, end in zip( range(0, len(index), batch_size), range(batch_size, len(index), batch_size)):

        current_feats = feats[index[start:end]]
        current_captions = captions[index[start:end]]
        current_caption_ind = [x for x in map(lambda cap: [wordtoix[word] for word in cap.lower().split(' ')[:-1] if word in wordtoix], current_captions)]

        current_caption_matrix = sequence.pad_sequences(current_caption_ind, padding='post', maxlen=maxlen+1)
        current_caption_matrix = np.hstack( [np.full( (len(current_caption_matrix),1), 0), current_caption_matrix] )

        current_mask_matrix = np.zeros((current_caption_matrix.shape[0], current_caption_matrix.shape[1]))
        nonzeros = np.array([x for x in map(lambda x: (x != 0).sum()+2, current_caption_matrix )])

        for ind, row in enumerate(current_mask_matrix):
            row[:nonzeros[ind]] = 1

        _, loss_value = sess.run([train_op, loss], feed_dict={
            image: current_feats.astype(np.float32),
            sentence : current_caption_matrix.astype(np.int32),
            mask : current_mask_matrix.astype(np.float32)
            })

        print("Current Cost: ", loss_value, "\t Epoch {}/{}".format(epoch, n_epochs), "\t Iter {}/{}".format(start,len(feats)))
    print("Saving the model from epoch: ", epoch)
    saver.save(sess, os.path.join(model_path, 'model'), global_step=epoch)

try:
#train(.001,False,False) #train from scratch
    train(.001,True,True)    #continue training from pretrained weights @epoch500
#train(.001)  #train from previously saved weights 
except KeyboardInterrupt:
    print('Exiting Training')

【问题讨论】:

  • 你能分享堆栈跟踪吗?
  • @AlexandrePassos 我已经分享了上面的堆栈跟踪
  • 即使您不恢复模型也会发生这种情况吗?
  • @AbhishekPatel 是的。即使我不恢复它也会出现同样的错误

标签: variables tensorflow lstm recurrent-neural-network valueerror


【解决方案1】:

在你的构造函数中,尝试

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

问题是,第一个位置参数是name,而你在那里有初始化程序,然后你再次定义名称,因此出现错误。

您需要在使用tf.get_variable的任何地方进行同样的更改

【讨论】:

  • 不,它不起作用。它给出了同样的错误。我已经清楚地提到了 name = ''word_embedding'' 而不仅仅是 "word_embedding" 所以参数的位置在这里并不重要,这就是我的想法
  • @clarky 但您没有为第一个参数提供名称,因此它假定这是名称。这就是为什么你的错误说它有多个名称值,我相信这有效,你可能会在不同的行上得到相同的错误。请仔细检查。
  • 是的,它现在工作了@Abhishek Patel 但现在我遇到了这个错误。你对此有任何想法吗? TypeError:不允许将tf.Tensor 用作Python bool。使用if t is not None: 而不是if t: 来测试是否定义了张量,并使用TensorFlow ops(例如tf.cond)来执行以张量值为条件的子图。
  • @clarky 如果解决了特定问题,您能否接受答案,对其他人有帮助。至于 bool 问题,按照它说的做,使用建议的表达式,如果最初它应该是 false,那么只需将变量设置为 none。错误本身包含解决方案。
猜你喜欢
  • 2019-05-09
  • 1970-01-01
  • 2018-07-28
  • 2016-10-21
  • 1970-01-01
  • 2013-08-04
  • 2016-06-24
  • 2015-05-18
  • 1970-01-01
相关资源
最近更新 更多