吴裕雄--天生自然TensorFlow2教程:损失函数及其梯度
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教程:损失函数及其梯度的更多相关文章
- 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战
import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- 吴裕雄--天生自然TensorFlow2教程:反向传播算法
- 吴裕雄--天生自然TensorFlow2教程:链式法则
import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...
- 吴裕雄--天生自然TensorFlow2教程:激活函数及其梯度
import tensorflow as tf a = tf.linspace(-10., 10., 10) a with tf.GradientTape() as tape: tape.watch( ...
- 吴裕雄--天生自然TensorFlow2教程:梯度下降简介
import tensorflow as tf w = tf.constant(1.) x = tf.constant(2.) y = x * w with tf.GradientTape() as ...
- 吴裕雄--天生自然TensorFlow2教程:误差计算
import tensorflow as tf y = tf.constant([1, 2, 3, 0, 2]) y = tf.one_hot(y, depth=4) # max_label=3种 y ...
随机推荐
- Go_json
package main import ( "encoding/json" "fmt" ) // 结构体与json // 1.序列化: 把Go语言中的结构体变量 ...
- [Note]Splay学习笔记
开个坑记录一下学习Splay的历程. Code 感谢rqy巨佬的代码,让我意识到了Splay可以有多短,以及我之前的Splay有多么的丑... int fa[N], ch[N][2], rev[N], ...
- Python记之薄暮笔记
——————————————————————————————接下来请欣赏与众不同的表演. Python打印所有的字符串时,都用引号将其括起. 有一些独特而有用的字符串表示方式. 可使用三引号表示很长的 ...
- 百炼OJ - 1005 - I Think I Need a Houseboat
题目链接:http://bailian.openjudge.cn/practice/1005/ 思路 一个半圆面积每年增长50,已知一个点坐标,求第几年点在半圆内. #include <stdi ...
- Python实现AVL树
参考: https://www.cnblogs.com/linxiyue/p/3659448.html?utm_source=tuicool&utm_medium=referral class ...
- 如何预测股票分析--自动ARIMA
在上一篇中,我们发现knn和线性回归一样,表现的不是特别好,来看看时间序列的表现 时间序列预测法其实是一种回归预测方法,属于定量预测,其基本原理是;一方面承认事物发展的延续性,运用过去时间序列的数据进 ...
- bugku 白哥的鸽子
首先下载之后发现是一个txt 然后更改一下后缀名发现这是一张jpg 图片然后看一下属性然后用hxd分析一下 看看有没有zip 发现没有 然后拖到地下然后发现有一串密码有些问题 然后查一下发现是 栏杆密 ...
- CDH安装错误集锦
CDH安装过程中出现的错误 1.iptables: /sbin/iptables 不存在 我是因为误删将 /sbin/下的文件删除了. 解决方法: yum install iptables 2.c ...
- Django流程-以登录功能为例
Django流程-以登录功能为例 一.注意点 1.新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2.启动Django ...
- SQL过滤条件on和where
在使用left jion时,会生成一张中间的临时表,然后再将这张临时表返回给用户. on和where条件的区别如下:1. on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表 ...