当前位置:网站首页>tensorflow入门
tensorflow入门
2022-07-25 10:27:00 【fatfatmomo】
数值计算表示为计算图,
TensorFlow的程序主干是一个图,
图节点是操作(代码中操作),有多个输入和一个输出
节点间的边表示之间流动的张量(tensors),比如n维数组
使用图,允许小而简单的操作建立复杂模型,会使梯度计算变简单---->自动求导
h=ReLU(Wx+b)
Variables:W,b
Placeholders(占位符):执行时间才会接受的点, x 即input。仅分配一个数据类型,一种大小的张量
Mathematical operations:数学操作节点 矩阵乘法、假发、激活函数等
import tensorflow as tf
import numpy as np
################################ 建立图 ###################################
b = tf.Variable(tf.zeros((100,)))
W = tf.Variable(tf.random_uniform((784,100),-1,1))
x = tf.placeholder(tf.float32,(100,784))
h = tf.nn.relu(tf.matmul(x,W)+b)
################################# 将图部署到会话上 ###############################
# 建立会话对象,调用参数Fetches,Feeds
# Fetches:list of graph nodes which return the output of the nodes
# Feeds:dictionary mapping from graph nodes to actual values,占位符
# 建立会话对象,采取默认环境,CPU,可以添加设备参数. 会话将图部署到硬件上?
sess = tf.Session()
sess.run(tf.initialize_all_variables())
sess.run(h, {x:np.random.random(100,784)})
################################ 训练模型 ######################################
prediction = tf.nn.softmax()#输出的预测值
label = tf.placeholder(tf.float32,[100,10])
cross_entropy = -tf.reduce_sum(label * tf.log(prediction),axis=1)
# 创建梯度下降的优化器,学习率设置0.5,最小化交叉熵cross_entropy
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(100):
batch_x,batch_label = data.next_batch()
sess.run(train_step,feed_dict={x: batch_x,label: batch_label})
with tf.variable_scope("foo"):
v=tf.get_variable("v",shape=[1])
边栏推荐
- BeautifulSoup的一些用法
- Esp8266 uses drv8833 drive board to drive N20 motor
- The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
- From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
- Learn NLP with Transformer (Chapter 2)
- Learn NLP with Transformer (Chapter 2)
- BGP federal experiment
- 二合一的集度,任重道远
- redis 哨兵,高可用的执行者
- AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
猜你喜欢
随机推荐
Learn NLP with Transformer (Chapter 7)
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
Learn NLP with Transformer (Chapter 6)
Nb-iot control LCD (date setting and reading)
软件测试技术之跨平台的移动端UI自动化测试(上)
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
HCIA实验(10)NAT
[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22
Visual thematic map of American airport go style: ArcGIS Pro version
数字孪生万物可视 | 联接现实世界与数字空间
HCIA实验(08)
Redis sentry, high availability executor
Hcip experiment (03)
HCIA experiment (06)
C3d model pytorch source code sentence by sentence analysis (II)
[servlet] request parsing
哥廷根大学提出CLIPSeg:一个使用文本和图像prompt能同时作三个分割任务的模型
Tree dynamic programming
100W!









