微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?