about TensorFlow Computing model
TensorFlow's programming differs greatly from the way I used to approach programming. Previous programming, whether the compiler type of language or scripting language, is a step-by-step, variable calculation, you will get results, such as c=a+b, when the execution of the statement, you will get the value of C. But TensorFlow is not, it first to programmatically, build a calculation diagram out, and then enable a session to take the data as input, through the calculation steps specified in this diagram calculation, the final results.
Ordinary command-type programming is easy to understand and debug, command statements are basically not optimized, according to the original logic execution. TensorFlow This symbolic programming has more embedding and optimization, not easy to understand and debug, but the speed will be a certain increase. tensor tensor
Tensor is a very important concept in TensorFlow, it defines the rules of calculation and does not save the calculated data, which is an essential part of constructing the calculation diagram. First look at the Official document introduction:
A Tensor is a symbolic handle to one of the outputs of the Operation. It does not hold the values of this operation ' s output, but instead provides a means of computing those values in a Tensor Flow TF. Session.
This class has two primary purposes:
A Tensor can is passed as an input to another Operation. This is builds a dataflow connection between operations, which enables TensorFlow to execute of entire Graph that represents A large, multi-step computation.
After the graph has been launched into a session, the value of the Tensor can is computed by passing it to TF. Session.run. T.eval () is a shortcut for calling Tf.get_default_session (). Run (t).
In simple terms, tensor is a handle that represents a calculation operation that does not save data, but simply provides a way to compute the data. There are two main purposes to use tensor, one is tensor can connect different operations, so as to build a calculation diagram, support TensorFlow to do large-scale multi-step computation, the other is when the tensor is enabled by a conversation session, It is possible to calculate the corresponding output data for the operation.
It can be said that the TensorFlow calculation process is the use of tensor to build a calculation diagram, and then use the session to start the calculation, the final results of the process. Simple Validation Code
Write a bit of code to verify the above statement.
import TensorFlow as TF def prn_obj (obj): print ' \ n '. Join (['%s:%s '% item for item in OBJ.__ Dict__.items ()]) def run (): G1 = tf. Graph () with G1.as_default (): a = tf. Variable (Tf.random_normal ([2,3],stddev=0.35,name= ' a ')) B = tf. Variable (Tf.random_normal ([2,3],stddev=0.35,name= ' B ')) C = A + b d = Tf.reduce_max ([b,c],0) e = tf
. Reduce_max ([a,c],0) F = Tf.reduce_max ([d,e],0) print ' A:----------------------------------------' Prn_obj (a) print ' B:----------------------------------------' prn_obj (b) print ' C:--------------- -------------------------' Print prn_obj (c) print ' d:----------------------------------------' pri NT Prn_obj (d) print ' E:----------------------------------------' Print prn_obj (e) print ' F:------- ---------------------------------' Print prn_obj (f) run ()
Here is the result, as you can see, there is no actual data in the tensor, and A and B are variables that don't look at first. Take C as an example, C is a tensor, there are several attributes to be noted:
-op: "ADD" sets the type of operation
-_shape: (2, 3) Specify the shape of the demand data
-_dtype:dtype: ' float32 ' sets the type of requirement data
-_CONSUMERS:[TF. Operation ' Max/input ' Type=pack, TF. Operation ' Max_1/input ' type=pack] consumers, the literal meaning of the consumer, records the next calculation of the tensor's calculated results as input tensor
Understand tensor is a thing for tensorflow programming or very good is necessary, because the TensorFlow first to build a calculation diagram, must have a clear understanding of the concept of the input and output of each operation can not be confused in programming.
A:----------------------------------------_variable:tensor ("variable:0", Shape= (2, 3), Dtype=float32_ref) _initial _value:tensor ("a:0", Shape= (2, 3), Dtype=float32) _save_slice_info:none _caching_device:none _snapshot:Tensor ("
Variable/read:0 ", Shape= (2, 3), Dtype=float32) _initializer_op:name:" Variable/assign "OP:" Assign "Input:" Variable " Input: "A" attr {key: "T" value {type:dt_float}} attr {key: "_class" value {list {s: "loc:
@Variable '}}} attr {key: ' use_locking ' value {b:true}} attr {key: ' Validate_shape ' value { B:true}} B:----------------------------------------_variable:tensor ("variable_1:0", Shape= (2, 3), Dtype=float32_r EF) _initial_value:tensor ("b:0", Shape= (2, 3), Dtype=float32) _save_slice_info:none _caching_device:none _snapshot: Tensor ("variable_1/read:0", Shape= (2, 3), Dtype=float32) _initializer_op:name: "Variable_1/assign" OP: "Assign" Input:
"Variable_1" Input: "B" attr {key: "T" value {Type:dt_float}} attr {key: ' _class ' value {list {s: ' loc: @Variable_1 '}} attr {key: ' Use _locking ' value {b:true}} attr {key: ' Validate_shape ' value {b:true}} C:----------------------
------------------_op:name: "Add" OP: "Add" Input: "Variable/read" Input: "Variable_1/read" attr {key: "T" value { Type:dt_float}} _shape: (2, 3) _value_index:0 _handle_shape:unknown_rank:true _dtype:<dtype: ' float32 ' > _c ONSUMERS:[<TF. Operation ' Max/input ' Type=pack>, <TF.
Operation ' Max_1/input ' type=pack>] _handle_dtype:0 None d:----------------------------------------_op:name: "Max" OP: "Max" input: "Max/input" Input: "Max/reduction_ Indices ' attr {key: ' T ' value {type:dt_float}} attr {key: ' Tidx ' value {type:dt_int32}} attr {key: ' Keep_dims ' value {b:false}} _shape: (2, 3) _value_index:0 _handle_shape:unknown_rank:true _dtype: <dtype: ' float32 ' > _consumers:[<TF.
Operation ' Max_2/input ' type=pack>] _handle_dtype:0 None e:----------------------------------------_op:name: "max_1" OP: "Max" Input: "Max_1/input" Input: "max_1/
Reduction_indices ' attr {key: ' T ' value {type:dt_float}} attr {key: ' Tidx ' value {type:dt_int32 } attr {key: ' Keep_dims ' value {b:false}} _shape: (2, 3) _value_index:0 _handle_shape:unknown_rank:tru E _dtype:<dtype: ' float32 ' > _CONSUMERS:[<TF.
Operation ' Max_2/input ' type=pack>] _handle_dtype:0 None f:----------------------------------------_op:name: "Max_2" OP: "Max" Input: "Max_2/input" Input: "max_2/
Reduction_indices ' attr {key: ' T ' value {type:dt_float}} attr {key: ' Tidx ' value {type:dt_int32 } attr {key: ' Keep_dims ' value {b:false}} _shape: (2, 3) _value_index:0 _handle_shape:unknown_rank:tru
E _dtype:<dtype: ' float32 ' > _consumers:[] _handle_dtype:0 None