Tensorflow lite,Tensorflow -mofan1

 2023-09-22 阅读 21 评论 0

摘要:例子 输出一个他每一次训练之后 他的参数是多少 每隔二十次训练输出参数 接近0.1 接近0.3 首先import tensorflow as tf #tf是tensorflow的缩写 import numpy as np #numpy是一个科学计算的库 编写一些data 生成一百个随机数 随机数生成参数 一维结构 Weights的范围限制为【-1

例子
输出一个他每一次训练之后 他的参数是多少
每隔二十次训练输出参数
接近0.1 接近0.3
首先import tensorflow as tf #tf是tensorflow的缩写
import numpy as np #numpy是一个科学计算的库
这里写图片描述

编写一些data
这里写图片描述
生成一百个随机数

这里写图片描述
随机数生成参数 一维结构
Weights的范围限制为【-1,1】

Tensorflow lite、这里写图片描述

初始值设置为一维结构的0

这里写图片描述

一步一步学习 逐渐接近 0.1 0.3
从初始值不断提升
定义y 是我预测的y
预测的y 和实际的y 的差别 loss

tensorflow.net,tensorflow中有一类在tensor的某一维度上求值的函数。如:
求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
翻文档可知的
优化器 是梯度下降算法
减少误差
key idea
学习效率 小于1

我们虽然建立了变量
但是在神经网络中我们还没有初始化变量
我们要初始化这个结构
初始化之前 定义一个sess
Session
Session像一个指针 指向处理的地方

这里写图片描述

开始让神经网络训练
训练201次
每隔二十部 打印训练的结果

tensorflow conv1d、这里写图片描述
最终结果基本正确 已经很接近于真实值0.1 和 0.3了

Ⅱ.class tf.train.GradientDescentOptimizer

这个类是实现梯度下降算法的优化器。(结合理论可以看到,这个构造函数需要的一个学习率就行了)

init(learning_rate, use_locking=False,name=’GradientDescent’)

tensorflow.keras?作用:创建一个梯度下降优化器对象
参数:
learning_rate: A Tensor or a floating point value. 要使用的学习率
use_locking: 要是True的话,就对于更新操作(update operations.)使用锁
name: 名字,可选,默认是”GradientDescent”.
compute_gradients(loss,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,grad_loss=None)

作用:对于在变量列表(var_list)中的变量计算对于损失函数的梯度,这个函数返回一个(梯度,变量)对的列表,其中梯度就是相对应变量的梯度了。这是minimize()函数的第一个部分,
参数:
loss: 待减小的值
var_list: 默认是在GraphKey.TRAINABLE_VARIABLES.
gate_gradients: How to gate the computation of gradients. Can be GATE_NONE, GATE_OP, or GATE_GRAPH.
aggregation_method: Specifies the method used to combine gradient terms. Valid values are defined in the class AggregationMethod.
colocate_gradients_with_ops: If True, try colocating gradients with the corresponding op.
grad_loss: Optional. A Tensor holding the gradient computed for loss.
apply_gradients(grads_and_vars,global_step=None,name=None)

作用:把梯度“应用”(Apply)到变量上面去。其实就是按照梯度下降的方式加到上面去。这是minimize()函数的第二个步骤。 返回一个应用的操作。
参数:
grads_and_vars: compute_gradients()函数返回的(gradient, variable)对的列表
global_step: Optional Variable to increment by one after the variables have been updated.
name: 可选,名字
get_name()

minimize(loss,global_step=None,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,name=None,grad_loss=None)

详细分析Tensorflow。作用:非常常用的一个函数
通过更新var_list来减小loss,这个函数就是前面compute_gradients() 和apply_gradients().的结合

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/82408.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息