TensorFlow Eager执行:AttributeError:'NoneType'对象没有属性'_tape'

时间:2018-04-18 20:26:16

标签: tensorflow keras attributeerror

我正在尝试在Eager Execution中使用TF运行自定义构建模型,但收到以下错误,我真的不知道如何解释:

sent=tf.convert_to_tensor(contexts_train[i], dtype=tf.float32)
quest=tf.convert_to_tensor(questions_train[i], dtype=tf.float32)
answer=tf.convert_to_tensor(answers_train[i], dtype=tf.float32)

我的输入是转换为张量的数组:

def grad(model, sent, quest, targets):
      with tfe.GradientTape() as tape:
        loss_value = loss(model, sent, quest, targets)
        return tape.gradient(loss_value, model.variables)
def loss(model, sent, quest, y):
    prediction = model.predict(sent, quest)
    return tf.keras.losses.categorical_crossentropy(y, prediction)

这就是我定义我的毕业和失去函数的方式:

grads = grad(model, sent, quest, answer)
optimizer.apply_gradients(zip(grads, model.variables),
                        global_step=tf.train.get_or_create_global_step())

我如何称呼培训:

class Model(tf.keras.Model):

    def __init__(self):
        super(Model, self).__init__()
        self.embed=tf.keras.layers.Embedding(42,50)
        self.grucell=tf.keras.layers.GRUCell(50)
        self.rnn=tf.keras.layers.RNN(self.grucell)
        self.dense=tf.keras.layers.Dense(42,activation=tf.nn.softmax)
        self.dropout=tf.keras.layers.Dropout(0.3)

    def predict(self, sentence, question):
        encoded_sentence=self.embed(sentence)
        encoded_sentence=tf.keras.backend.expand_dims(encoded_sentence, axis=-1)  
        encoded_sentence=self.rnn(encoded_sentence)
        encoded_sentence=self.dropout(encoded_sentence)

        encoded_question=self.embed(question)
        encoded_question=tf.keras.backend.expand_dims(encoded_question, axis=-1)
        encoded_question=self.rnn(encoded_question)
        encoded_question=self.dropout(encoded_question)

        merged= tf.keras.layers.concatenate([encoded_sentence, encoded_question])
        pred= self.dense(merged)
        pred= tf.keras.backend.expand_dims(pred, axis=1)

        return pred

为了完整起见,这里是模型:

Rng.Offset(0, xOffsetColumn).Value = UCase(Date)

获得一些帮助以了解后台发生的事情会很棒。

1 个答案:

答案 0 :(得分:1)

您需要在tape.gradient块的范围之外调用with。特别是,将grad功能更改为以下内容:

def grad(model, sent, quest, targets):
    with tfe.GradientTape() as tape:
      loss_value = loss(model, sent, quest, targets)
    return tape.gradient(loss_value, model.variables)