当前位置: 首页 > 知识库问答 >
问题:

ValueError:没有为任何变量提供渐变,检查您的图表中不支持渐变的操作,变量之间

章振
2023-03-14

我正在使用tensorflow训练“Show and tell”模型,该模型自动生成图像的字幕。我怎么会犯这个错误。

这是回溯:

------------------------------------------------------------------------
---
ValueError                                Traceback (most recent call 
last)
<ipython-input-36-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-35-39693d0edd0a> in train(learning_rate, continue_training, transfer)
 31     learning_rate = tf.train.exponential_decay(learning_rate, global_step,
 32                                        int(len(index)/batch_size), 0.95)
---> 33     train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss)
 34     tf.global_variables_initializer().run()
 35 
/home/niraj/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in minimize(self, loss, global_step, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, name, grad_loss)
320           "No gradients provided for any variable, check your graph for ops"
321           " that do not support gradients, between variables %s 
and loss %s." %
--> 322           ([str(v) for _, v in grads_and_vars], loss))
323 
324     return self.apply_gradients(grads_and_vars, 
global_step=global_step,

ValueError:没有为任何变量提供渐变,检查您的图表中不支持渐变的操作,在变量之间["tf。变量'word_embedding: 0'形状=(2943,256)dtype=float32_ref

我知道这个错误是因为在优化过程中有一个变量不能保持梯度,而这个变量又会切割图形,但我无法将其挑出。

我正在使用已经训练好的VGG-net 16模型参数和具有相应注释的FLICKR-30图像数据集。

代码如下:

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.Variable(tf.random_uniform([self.n_words, self.dim_embed], -0.1, 0.1), name='word_embedding')

    self.embedding_bias = tf.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.Variable(tf.random_uniform([dim_in, dim_hidden], -0.1, 0.1), name='img_embedding')
    self.img_embedding_bias = tf.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.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.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)

共有1个答案

胡国兴
2023-03-14

丢失生成例程中的分支无效。

with tf.variable_scope("RNN"):
        for i in range(self.n_lstm_steps): 
            if i > 0:
                [...]
            else:
                [...]
                if i > 0: 
                    [...]
                if i > 0:
                    [...]

请注意,最后两个if永远不会运行,因为它们在else子句中,这意味着

 类似资料:
  • 问题内容: 我正在尝试使用Keras实现一个简单的序列到序列模型。但是,我一直看到以下内容: 像其他的问题这个还是看这个问题在Github上认为,这可能有一些做的交叉熵损失函数; 但我看不到我在做什么错。 我不认为这是问题所在,但我要提一提,确切地说,我是每晚构建的TensorFlow 。 以下代码是一个独立的示例,应从上面重现异常: 更新资料 要点链接 Github问题链接 问题答案: 您的代码

  • Another hard-to-write CSS3 feature is a gradient. You have to repeat long gradient definition multiple times with different vendor prefixes. Also, if you want to cover all gradient-supported browsers,

  • 使用渐变填充可以在要应用其他任何颜色时应用渐变颜色混和。创建渐变填色是在一个或多个对象间创建颜色平滑过渡的好方法。您可以将渐变存储为色板,从而便于将渐变应用于多个对象。 注:如果要创建颜色可以沿不同方向顺畅分布的单个多色对象,请使用网格对象。 要查看使用渐变来改进绘画的视频,请参阅 www.adobe.com/go/lrvid4017_ai_cn。有关创建渐变的教程,请参阅 “Unleash th

  • 问题内容: 默认情况下,我的条形图始终使用渐变颜色绘制。我只想要一种 没有任何样式效果 的简单颜色。 有人可以帮忙吗? 码: 谢谢 问题答案: 问题出在您正在使用中。JFreeChart版本1.0.13的默认设置是使用,它将为条形添加金属感。如果您想要“旧”外观,解决方案是使用。 那应该做。 另外,如果要使用JFreeChart’s ,则可以在初始化渲染器之前通过调用static方法来强制其使用。

  • 渐变 渐变是在两种邻近的颜色(包括黑色和白色)之间实现平滑过渡的若干方法之一。 A:由黑色到白色的线性渐变; B:多种颜色的线性渐变; C:由彩色到透明的线性渐变(将鼠标置于图形上方可显示透明度); D:由彩色到白色的径向渐变

  • Highcharts 饼图 以下实例演示了渐变饼图。 我们在前面的章节已经了解了 Highcharts 基本配置语法。接下来让我们来看下其他的配置。 配置 colors 配置 使用 Highcharts.getOptions().colors 属性来配置颜色。 //颜色的填充 Highcharts.getOptions().colors = Highcharts.map(Highcharts.g