本站消息

站长简介


前每日优鲜python全栈开发工程师,自媒体达人,逗比程序猿,钱少话少特宅,我的公众号:想吃麻辣香锅

  python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长



+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

ValueError:未使用Keras功能API为任何变量提供渐变

发布于2021-05-07 06:46     阅读(215)     评论(0)     点赞(1)     收藏(2)


0

1

2

3

4



我正在使用keras函数API(tf.keras)为图像字幕网络实现以下Keras模型:

import glob
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import pickle
from tqdm import tqdm
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, Input
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.applications import InceptionV3
from tensorflow.keras.preprocessing import image
from tensorflow.keras import Model
import nltk
from google.colab import drive
drive.mount('/content/drive')

def data_generator(batch_size = 32):
    partial_caps = []
    next_words = []
    images = []

    df = pd.read_csv(folder_drive + 'flickr8k_training_dataset.txt', delimiter='\t')
    df = df.sample(frac=1) #shuffle rows
    iter = df.iterrows()
    c = []
    imgs = []
    for i in range(df.shape[0]):
        x = next(iter)
        c.append(x[1][1])
        imgs.append(x[1][0])


    count = 0
    while True:
        for j, text in enumerate(c):
            current_image = encoding_train[imgs[j]]
            for i in range(len(text.split())-1):
                count+=1

                partial = [word2idx[txt] for txt in text.split()[:i+1]]
                partial_caps.append(partial)

                # Initializing with zeros to create a one-hot encoding matrix
                # This is what we have to predict
                # Hence initializing it with vocab_size length
                n = np.zeros(vocab_size)
                # Setting the next word to 1 in the one-hot encoded matrix
                n[word2idx[text.split()[i+1]]] = 1
                next_words.append(n)

                images.append(current_image)

                if count>=batch_size:
                    next_words = np.asarray(next_words)
                    images = np.asarray(images)
                    partial_caps = sequence.pad_sequences(partial_caps, maxlen=max_len, padding='post')
                    yield [[images, partial_caps], next_words]
                    partial_caps = []
                    next_words = []
                    images = []
                    count = 0

image_input = Input(shape = (2048,))
x = layers.Dense(embedding_size, activation='relu')(image_input)
image_output = layers.RepeatVector(max_len)(x)
image_model = Model(inputs=image_input,outputs = image_output)
image_model.summary()

caption_input = Input(shape = (max_len,))
y = layers.Embedding(vocab_size,embedding_size,input_length=max_len)(caption_input)
y = layers.LSTM(256,return_sequences=True)(y)
caption_output = layers.TimeDistributed(layers.Dense(embedding_size))(y)
caption_model = Model(inputs = caption_input, outputs = caption_output)
caption_model.summary()

conca = layers.Concatenate(axis=1)([image_model.output,caption_model.output])
z = layers.Bidirectional(layers.LSTM(256, input_shape = (max_len,300), return_sequences=False))(conca)
z = layers.Dense(vocab_size)(z)
final_output = layers.Activation('softmax')(z)
final_model = Model(inputs = [image_model.input,caption_model.input], outputs = final_output)
final_model.summary()

模型图

final_model.compile(loss='categorical_crossentropy', optimizer="rmsprop", metrics=['accuracy'])
final_model.fit_generator(data_generator(batch_size=2048), steps_per_epoch = samples_per_epoch//2048, 
                          verbose=1,epochs = 50)

运行fit_generator方法时,总是会出现以下错误:

Epoch 1/50
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-225-9cb298634256> in <module>()
      1 final_model.fit_generator(data_generator(batch_size=2048), steps_per_epoch = samples_per_epoch//2048, 
----> 2                           verbose=1,epochs = 50)

12 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:541 train_step  **
        self.trainable_variables)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1804 _minimize
        trainable_variables))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:521 _aggregate_gradients
        filtered_grads_and_vars = _filter_grads(grads_and_vars)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:1219 _filter_grads
        ([v.name for _, v in grads_and_vars],))

    ValueError: No gradients provided for any variable: ['embedding_6/embeddings:0', 'dense_14/kernel:0', 'dense_14/bias:0', 'lstm_10/lstm_cell_18/kernel:0', 'lstm_10/lstm_cell_18/recurrent_kernel:0', 'lstm_10/lstm_cell_18/bias:0', 'time_distributed_6/kernel:0', 'time_distributed_6/bias:0', 'bidirectional_4/forward_lstm_11/lstm_cell_20/kernel:0', 'bidirectional_4/forward_lstm_11/lstm_cell_20/recurrent_kernel:0', 'bidirectional_4/forward_lstm_11/lstm_cell_20/bias:0', 'bidirectional_4/backward_lstm_11/lstm_cell_21/kernel:0', 'bidirectional_4/backward_lstm_11/lstm_cell_21/recurrent_kernel:0', 'bidirectional_4/backward_lstm_11/lstm_cell_21/bias:0', 'dense_17/kernel:0', 'dense_17/bias:0'

谁能帮助我确定错误的出处,因为我从未见过错误,并且我一直在检查SO中的类似帖子,但是那里的任何解决方案都对我有用。


解决方案


试试这个:

final_model = Model(inputs = [image_input ,caption_input ], outputs = final_output)



0

1

2

3

4

5

6

7



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/964742/c6a6d8a10d43fec3bd57/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)