- Mastering TensorFlow 1.x
- Armando Fandango
- 352字
- 2021-06-25 22:50:55
Variables
So far, we have seen how to create tensor objects of various kinds: constants, operations, and placeholders. While working with TensorFlow to build and train models, you will often need to hold the values of parameters in a memory location that can be updated at runtime. That memory location is identified by variables in TensorFlow.
In TensorFlow, variables are tensor objects that hold values that can be modified during the execution of the program.
While tf.Variable appears similar to tf.placeholder, there are subtle differences between the two:

In TensorFlow, a variable can be created with tf.Variable(). Let's see an example of placeholders and variables with a linear model:
- We define the model parameters w and b as variables with initial values of [.3] and [-0.3], respectively:
w = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
- The input x is defined as a placeholder and the output y is defined as an operation:
x = tf.placeholder(tf.float32)
y = w * x + b
- Let's print w, v, x, and y and see what we get:
print("w:",w)
print("x:",x)
print("b:",b)
print("y:",y)
We get the following output:
w: <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>
x: Tensor("Placeholder_2:0", dtype=float32)
b: <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>
y: Tensor("add:0", dtype=float32)
The output shows that x is a placeholder tensor and y is an operation tensor, while w and b are variables with shape (1,) and data type float32.
Before you can use the variables in a TensorFlow session, they have to be initialized. You can initialize a single variable by running its initializer operation.
For example, let's initialize the variable w:
tfs.run(w.initializer)
However, in practice, we use a convenience function provided by the TensorFlow to initialize all the variables:
tfs.run(tf.global_variables_initializer())
You can also use the tf.variables_initializer() function to initialize only a set of variables.
The global initializer convenience function can also be invoked in the following manner, instead of being invoked inside the run() function of a session object:
tf.global_variables_initializer().run()
After initializing the variables, let's run our model to give the output for values of x = [1,2,3,4]:
print('run(y,{x:[1,2,3,4]}) : ',tfs.run(y,{x:[1,2,3,4]}))
We get the following output:
run(y,{x:[1,2,3,4]}) : [ 0. 0.30000001 0.60000002 0.90000004]
- 24小時(shí)學(xué)會(huì)電腦組裝與維護(hù)
- Aftershot Pro:Non-destructive photo editing and management
- 用“芯”探核:龍芯派開發(fā)實(shí)戰(zhàn)
- Istio入門與實(shí)戰(zhàn)
- 電腦常見問(wèn)題與故障排除
- 現(xiàn)代辦公設(shè)備使用與維護(hù)
- 3ds Max Speed Modeling for 3D Artists
- 單片機(jī)原理及應(yīng)用系統(tǒng)設(shè)計(jì)
- 微服務(wù)分布式架構(gòu)基礎(chǔ)與實(shí)戰(zhàn):基于Spring Boot + Spring Cloud
- Machine Learning Solutions
- 微控制器的應(yīng)用
- 計(jì)算機(jī)組成技術(shù)教程
- MicroPython Cookbook
- The Deep Learning Workshop
- Arduino+3D打印創(chuàng)新電子制作2