Tensor:
三个性质:
- A unique label (name)
- A dimension (shape)
- A data type (dtype)
四种类型
- tf.Variable
- tf.constant
- tf.placeholder
- tf.SparseTensor
tf.constant(value, dtype, name = "")
arguments
- `value`: Value of n dimension to define the tensor. Optional
- `dtype`: Define the type of data:
- `tf.string`: String variable
- `tf.float32`: Flot variable
- `tf.int16`: Integer variable
- "name": Name of the tensor. Optional. By default, `Const_1:0`
操作:
- tf.add(a, b)
- tf.substract(a, b)
- tf.multiply(a, b)
- tf.div(a, b)
- tf.pow(a, b)
- tf.exp(a)
- tf.sqrt(a)
tf.get_variable(name = "", values, dtype, initializer)
argument
- `name = ""`: Name of the variable
- `values`: Dimension of the tensor
- `dtype`: Type of data. Optional
- `initializer`: How to initialize the tensor. Optional
If initializer is specified, there is no need to include the `values` as the shape of `initializer` is used.
# Create a Variable
## Create 2 Randomized values
var = tf.get_variable("var", [1, 2])
print(var.shape)
var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32, initializer=tf.zeros_initializer)
print(var_init_1.shape)
Placeholder:
placeholder用来初始化数据,用feed_dict方法来为placeholder提供值。在Session运行的过程中喂placeholder。
Session:
- tf.Session(): Open a session. All the operations will flow within the sessions
- run(multiply): execute the operation created in step 2.
- print(result_1): Finally, you can print the result
- close(): Close the session
Create a constant tensor
constant | object |
---|---|
D0 | tf.constant(1, tf.int16) |
D1 | tf.constant([1,3,5], tf.int16) |
D2 | tf.constant([ [1, 2], [3, 4] ],tf.int16) |
D3 | tf.constant([ [[1, 2],[3, 4], [5, 6]] ], tf.int16) |
Create an operator
Create an operator | Object |
---|---|
a+b | tf.add(a, b) |
a*b | tf.multiply(a, b) |
Create a variable tensor
Create a variable | object |
---|---|
randomized value | tf.get_variable("var", [1, 2]) |
initialized first value | tf.get_variable("var_init_2", dtype=tf.int32, initializer=[ [1, 2], [3, 4] ]) |
Open a session
Session | object |
---|---|
Create a session | tf.Session() |
Run a session | tf.Session.run() |
Evaluate a tensor | variable_name.eval() |
Close a session | sess.close() |
Session by block | with tf.Session() as sess: |