import tensorflow as tf

x = tf.random.normal([2, 4])
w = tf.random.normal([4, 3])
b = tf.zeros([3])
y = tf.constant([2, 0])
with tf.GradientTape() as tape:
tape.watch([w, b])
prob = tf.nn.softmax(x @ w + b, axis=1)
loss = tf.reduce_mean(tf.losses.MSE(tf.one_hot(y, depth=3), prob))
grads = tape.gradient(loss, [w, b])
grads[0]

x = tf.random.normal([2, 4])
w = tf.random.normal([4, 3])
b = tf.zeros([3])
y = tf.constant([2, 0])
with tf.GradientTape() as tape:
tape.watch([w, b])
logits =x @ w + b
loss = tf.reduce_mean(
tf.losses.categorical_crossentropy(tf.one_hot(y, depth=3),logits,from_logits=True))
grads = tape.gradient(loss, [w, b])
grads[0]

吴裕雄--天生自然TensorFlow2教程:损失函数及其梯度的更多相关文章

  1. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  2. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  3. 吴裕雄--天生自然TensorFlow2教程:反向传播算法

  4. 吴裕雄--天生自然TensorFlow2教程:链式法则

    import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...

  5. 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  6. 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...

  7. 吴裕雄--天生自然TensorFlow2教程:激活函数及其梯度

    import tensorflow as tf a = tf.linspace(-10., 10., 10) a with tf.GradientTape() as tape: tape.watch( ...

  8. 吴裕雄--天生自然TensorFlow2教程:梯度下降简介

    import tensorflow as tf w = tf.constant(1.) x = tf.constant(2.) y = x * w with tf.GradientTape() as ...

  9. 吴裕雄--天生自然TensorFlow2教程:误差计算

    import tensorflow as tf y = tf.constant([1, 2, 3, 0, 2]) y = tf.one_hot(y, depth=4) # max_label=3种 y ...

随机推荐

  1. Commercial Lighting: LED Ceiling Light, LED Ceiling Light

    Unlike ceiling lamps, floor lamps, chandeliers, lamps that can sometimes rely on "faces", ...

  2. selenium的错误截图

    在自动化测试过程中,测试执行期间需要收集获取截图信息,一方面为了错误调试代码,一方面也为了和开发沟通, 获取当前的截图 save_screenshot是获取当前截图的方法,以百度首页为例,打开百度首页 ...

  3. B/S架构和C/S的区别

    经常在招聘网站上看到要求熟悉B/S C/S架构,具体含义是: B/S---Browser/Server  浏览器/服务器模式 C/S---Client/Server   客户端/服务器模式 通俗点讲: ...

  4. form表单提交且接口回调显示提交成功

    前端: <form method="post" enctype="multipart/form-data" id="formSubmit&quo ...

  5. dw选择器

    选择器并没有一个固定的定义,在某种程度上说,jQuery的选择器和样式表中的选择器十分相似.选择器具有如下特点:1.简化代码的编写2.隐式迭代3.无须判断对象是否存在jQuery 的选择器可谓之强大无 ...

  6. JDBC简单代码

    1..写简单sql语句执行 DROP TABLE IF EXISTS `jdbctest`; CREATE TABLE `jdbctest` ( `id` ) NOT NULL AUTO_INCREM ...

  7. Bugku-CTF分析篇-flag被盗(flag被盗,赶紧溯源!)

    flag被盗 flag被盗,赶紧溯源!

  8. bugku 矛盾 30

    首先打开网址链接会发现一串代码 然后进行分析代码的意思首先是一个函数查一下这个函数 然后会发现现代码第一句写的是输入数字,然后会发现第二行有一个感叹号意思是输入的如果不是数字则回复数字 如果输入数字则 ...

  9. Go键盘输入和打印输出

    package main import ( "fmt" "bufio" "os" ) func main() { /* 输入和输出: fmt ...

  10. pyqt中定时器的使用

    1.定义一个定时器函数 # 定时器 from PyQt5.QtCore import QTimer def timer_start(): timer = QTimer() # fun1是监听的函数,如 ...