使用TF2.0 Keras和使用tf.GradientTape时,“没有为任何变量提供渐变:”

问题描述

'在我的代码中,我需要采用网络输出相对于其两个输入的派生形式。然后使用这两个新变量来计算损失,然后计算损失wrt到模型权重和apply_gradient的梯度以最小化损失。我正在使用TF 2.3.0。这是代码摘要

''' n_hidden = 10

NN类(模型): #设置图层 def init (自己): 超级(NN,自我)。 init () #第一个全连接的隐藏层。 self.inp = layers.InputLayer(input_shape = {2,)) self.fc1 = layers.Dense(n_hidden,激活='线性') self.fc2 = layers.Dense(1,激活= tf.nn.relu)

  Stream<List<Product>> get getAllProducts {
    return productCollection.get().asstream().map(_productDataFromSnapshot);
  }

  List<Product> _productDataFromSnapshot(QuerySnapshot snapshot) {
    List<Product> productsList = List<Product>();

    for (int i = 0; i < snapshot.docs.length; i++) {
      productsList.add(Product(
        productID: snapshot.docs[i].data()['productID'] ?? "",name: snapshot.docs[i].data()['name'] ?? "",volume: snapshot.docs[i].data()['volume'] ?? "",price: snapshot.docs[i].data()['price'] ?? "",quantity: snapshot.docs[i].data()['quantity'] ?? "",brand: snapshot.docs[i].data()['brand'] ?? "",productAttribute: snapshot.docs[i].data()['productAttribute'] ?? "",productCtgName: snapshot.docs[i].data()['productCtgName'] ?? "",productSubCtgName: snapshot.docs[i].data()['productSubCtgName'] ?? "",productCtgID: snapshot.docs[i].data()['productCtgID'] ?? "",productSubCtgID: snapshot.docs[i].data()['productSubCtgID'] ?? "",));
    }
    return productsList;
  }


class Product {
  String productID;
  String name;
  int price;
  int salePrice;
  DateTime addDate;
  String volume;
  int quantity;
  String productAttribute;
  String productSubCtgID;
  String productCtgID;
  String productSubCtgName;
  String productCtgName;
  String brand;
  String pic;

  Product({
    this.productID,this.name,this.price,this.addDate,this.volume,this.quantity,this.productAttribute,this.brand,this.productCtgID,this.productSubCtgID,this.productCtgName,this.productSubCtgName,this.pic,this.salePrice,});

defsolve_model(net):

# Set forward pass.
def call(self,x,z,is_training = False):   
    x = tf.math.log(x)
    z = tf.math.log(z)
    y = tf.stack([x,z],axis = 1)
    y = self.inp(y)
    y = self.fc1(y)
    y = self.fc2(y)
    y = y[:,0]
    return y

net = NN() resolve_model(net)

'代码由于apply_gradient行而制动。看起来它无法将grads(来自loss_avg本身)连接到模型可训练变量。对我来说,它看起来是连通的,就像模型在计算V时所做的预测一样,然后它先计算d1和d2,然后计算loss_avg,所以对我来说,loss_avg与网络的最后一层相连/相关。知道我缺少什么吗?我见过类似的帖子,但到目前为止仍找不到解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...