当前位置:网站首页>Tensorflow Basics
Tensorflow Basics
2022-04-22 21:10:00 【A monk in Shimizu】
TensorFlow Working mechanism
TensorFlow Let's implement machine learning algorithms by creating and computing interactive operations . These interactions form what we call “ Calculation chart ”, Through the calculation diagram, we can intuitively express the complex functional structure .
What is a calculation chart ?
Calculation diagram components :
- node ( Indicates the operation to be performed )
- edge ( For the flow of data )
Use TensorFlow There are two main steps :
- Create diagrams
- Execute the diagram
stay TensorFlow When we create related operations in , Not immediately executed , Instead, one node after another is created in the diagram , These nodes record the operations we want to perform , But it will not be carried out .
After completing the description of the calculation diagram , You can run the calculation shown in the diagram . To achieve this , We need to create and run sessions .
- First, in the tf.Session() Start up diagram in
- And then call Session Object's .run() Method completes the run ( This method starts from the output of the request , Then reverse the calculation , Here, the calculation of nodes must be performed according to the dependency set . So the final calculated part of the graph actually depends on our output demand )
- When the task is completed , Best use sess.close() Command to close session , Ensure that the resources used by the session are freed
Take an example :
import tensorflow as tf
# Create the first three nodes , Will value 5,2 and 3 Give to a,b,c
a= tf.constant(5)
print(a)
b= tf.constant(2)
print(b)
c=tf.constant(3)
print(c)
# Create the last three nodes , Perform simple arithmetic operations
d=tf.multiply(a,b)
print(d)
e=tf.add(c,b)
print(e)
f=tf.subtract(d,e)
print(f)
# Execution diagram
'''sess=tf.Session()
out=sess.run(f)
sess.close()'''
with tf.Session() as sess:
print(sess.run(a))
print(sess.run(b))
print(sess.run(c))
print(sess.run(d))
print(sess.run(e))
print(sess.run(f))
Variable 、 Placeholders and simple optimizations
Variable :
The function of the optimization process is to adjust the parameters of some given models . So ,TensorFlow Using variables . With others every time you run a session “ Refill ” Data Tensor Objects of different , Variables remain fixed in the graph . It's important , Because their current state may affect their changes in the next iteration . Like other Tensor object , Variables can be used as inputs to other operations in the diagram .
Using variables is done in two stages :
- First call tf.Variable() Function to create a variable and define the value it will be initialized .
- Then you must use tf.global_variables_initializer() Method to run the session to explicitly perform the initialization operation .
Like other Tensor object , Variables are calculated only when the model is running .
init_val = tf.random_normal((1,5),0,1)
var=tf.Variable(init_val,name='var')
print('pre run\n {} '.format(var))
# When defining variables, you must follow steps Perform initialization operation This step is essential
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print('\n post run:\n{}'.format(sess.run(var)))
Place holder :
Placeholders are empty variables for filling in data . It can be understood that in the figure, we first create a structure , There is nothing in this structure, just occupying a place in the figure , We can fill these places with the data we want to fill as needed .
The placeholder has an optional shape parameter (shape). If a shape has no input value or the input value is None, Then the placeholder can be used to input data of any size .
There are two steps to using placeholders :
- Create placeholders
- input data ( To define a placeholder, you must give it some input values , Otherwise an exception is thrown )
The input data is passed as a dictionary to session.run() Method , Each key corresponds to a placeholder variable name , Matching values are in list or Numpy Data values given in array form
x_data=np.random.randn(5,10)
w_data=np.random.randn(10,1)
with tf.Graph().as_default():
# Create placeholders
x=tf.placeholder(tf.float32,shape=(5,10))
w=tf.placeholder(tf.float32,shape=(10,1))
b=tf.fill((5,1),-1)
xw=tf.matmul(x,w)
xwb=xw+b
s=tf.reduce_max(xwb)
with tf.Session() as sess:
# Pass data into placeholders
outs=sess.run(s,feed_dict={x:x_data,w:w_data})
print("outs={}".format(outs))
版权声明
本文为[A monk in Shimizu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222105562732.html
边栏推荐
- (1) UART subsystem learning plan
- 新闻速递 I MobTech通过中国信通院“安全专项评测”
- Get specified TC rule
- ImportError: cannot import name ‘get_ all_ providers‘ from ‘onnxruntime. capi._ pybind_ state‘
- Selenium_Webdriver视频自动化脚本分享
- 基于PAOGD_HW1的弹出的小球-简单建模、插值动画
- Lizard book learning Day1 - overview of machine learning
- Leetcode question brushing Diary - 15 Sum of three
- Introduction to dynamic programming of leetcode learning plan day1,2 (4 questions in total)
- Leetcode-238 - product of arrays other than itself
猜你喜欢

All paths of 344 leetcode binary tree

DSPACE模拟简单事故现场

Selenium_ Webdriver video automation script sharing

JMeter data and software
![解数独[Pre-DS-hash结构 + component-DFS | Pre-hash-1-9特性--二进制状态压缩 + DFS]](/img/06/295b26f1389da3d90ad211dc447123.png)
解数独[Pre-DS-hash结构 + component-DFS | Pre-hash-1-9特性--二进制状态压缩 + DFS]

新聞速遞 I MobTech通過中國信通院“安全專項評測”

On "waiting for awakening mechanism"

机器视觉需要掌握的知识

Confidence interval and interval estimation

jmeter视频教学课程
随机推荐
Handwritten linked list ~ contains one-way linked list and two-way linked list. Please rest assured
新聞速遞 I MobTech通過中國信通院“安全專項評測”
Knowledge of machine vision
TC流程添加状态
Selenium_ Webdriver video automation script sharing
Building a new generation of computing platform, stepvr will open the "door" of metauniverse in 2022
The traffic not included in the Internet era is all included in the industrial Internet era
Introduction to dynamic programming of leetcode learning plan day1,2 (4 questions in total)
反射与注解
Openvx - read and write image file [pgm format] as VX_ Image objects, and write operations
.103Navigator
大量mapper IO优化(使用多线程异步+CountDownLatch)
344-Leetcode 二叉树的所有路径
Leetcode question brushing Diary - 15 Sum of three
Leetcode-209-subarray with the smallest length
TC fabric manager - packaging and unpacking
[what is istio?] You're out before you know it. You can understand it quickly in 40 minutes
.103Navigator
Return to heaven ladder-l2-020 Kung Fu successor (25 points) (BFS or DFS)
TensorFlow基础知识
