Tensor
A tensor consists of a set of primitive values shaped into array of any number of dimensions.
Tensorflow Core WalkThrough
- Building the computational graph –
tf.Graph
- Running the computational graph –
tf.Session
Graph
contains two types of objects
- Operations: the node of the graph
- Tensors: the edges in the graph
Session
|
|
Feeding
|
|
Layers
Creating layers
|
|
initialing layers
|
|
Layer Function shortcuts
|
|
Training
Define the data
Define the model
fully_connected layer
|
|
loss
mean_squared_error
|
|
Optimize
Optimizers
– tf.train.Optimizer
GradientDescent
|
|
|
|
Adam
|
|
Complete Program
|
|
also could use with
, don’t need to close
|
|
Batch
get_batch():
- get_batch(): by self
- sklearn: gen_batches()
- shuffle_batch_x, shuffle_batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size=Config.batch_size, capacity=10000,min_after_dequeue=5000, enqueue_many=True)
Variable
tf.Variable() & tf.get_variable()
使用tf.Variable()
时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()
时,系统不会处理冲突,而会报错
|
|
|
|
当我们需要共享变量的时候,需要使用tf.get_variable()
。在其他情况下,这两个的用法是一样的
variable_scope & name_scop
tf.variable_scope
可以让变量有相同的命名,包括tf.get_variable
得到的变量,还有tf.Variable
的变量
tf.name_scope
可以让变量有相同的命名,只是限于tf.Variable
的变量
如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。为了解决这个问题,TensorFlow 提出了 tf.variable_scope
函数:它的主要作用是,在一个作用域 scope 内共享一些变量,简单来说就是给变量名前再加了个变量空间名
tf.multinomial
tf.multinomial(logits, num_samples, seed=None, name=None)
从multinomial分布中采样,样本个数是num_samples,每个样本被采样的概率由logits给出
参数:
logits: 2-D Tensor with shape [batch_size, num_classes]. Each slice [i, :] represents the unnormalized log probabilities for all classes.2维量,shape是 [batch_size, num_classes],每一行都是关于种类的未归一化的对数概率
num_samples: 0-D. Number of independent samples to draw for each row slice.标量,表示采样的个数,更重要的是,它限制了返回张量中元素的范围{:0,1,2,…,num_samples-1 }
返回值:
The drawn samples of shape [batch_size, num_samples],注意元素的取值范围取决于num_samples
|
|
tf.reshape tf.shape
|
|
tf.nn.softmax_cross_entropy_with_logits() & tf.nn.sparse.softmax_cross_entropy_with_logits()
|
|
Having two different functions is a convenience, as they produce the same result.
The difference is simple:
- For
sparse_softmax_cross_entropy_with_logits
, labels must have the shape [batch_size] and the dtype int32 or int64. Each label is an int in range[0, num_classes-1]
. - For
softmax_cross_entropy_with_logits
, labels must have the shape [batch_size, num_classes] and dtype float32 or float64.
Labels used in softmax_cross_entropy_with_logits
are the one hot version of labels used in sparse_softmax_cross_entropy_with_logits
.
Another tiny difference is that with sparse_softmax_cross_entropy_with_logits
, you can give -1 as a label to have loss 0
on this label.
不错的整理from某乎:https://zhuanlan.zhihu.com/p/33560183
cross_entropy 交叉熵
softmax()
sigmoid()
tf.reduce_max()
see in https://medium.com/@aerinykim/tensorflow-101-what-does-it-mean-to-reduce-axis-9f39e5c6dea2
tf.reduce_max(x, 0) kills the 0-th dimension. So ‘3’ in (3,2,5) will be gone.
tf.get_collection()
A collection is nothing but a named set of values.
Every value is a node of the computational graph.
Every node has its name and the name is composed by the concatenation of scopes, /
and values, like: preceding/scopes/in/that/way/value
get_collection
, without scope
allow fetching every value in the collection without applying any filter operation.
When the scope
parameter is present, every element of the collection is filtered and its returned only if the name of the node starts with the specified scope
.
see in https://stackoverflow.com/questions/44691406/how-to-understand-tf-get-collection-in-tensorflow
Return: The list of values in the collection with the given name
, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.
Gradients Computation
Processing gradients before applying them.
Calling minimize()
takes care of both computing the gradients and applying them to the variables. If you want to process the gradients before applying them you can instead use the optimizer in three steps:
- Compute the gradients with
compute_gradients()
. - Process the gradients as you wish.
- Apply the processed gradients with
apply_gradients()
.
Example:
|
|