Tensorflow 学习笔记

Google Deep learning

从机器学习到深度学习

softmax

softmax可以把数值转换为对应在0-1之间的概率。

\[$S({y_i}) = \frac{e^{y_i}}{\sum_{j}e^{y_{j}}}$\]
def softmax(x):
    return np.exp(x) / np.sum(np.exp(x), axis=0)

cross-entropy 交叉熵

\[$(D(S, L) = -\sum_{j} L_{i} log(S_{i})$\]

正则化的作用

正则化可以使训练速度提升,优化梯度下降的效果。

正则化可以参考一些函数直接进行。

文档中的关键点

手动指定设备来运行

# Creates a graph.
with tf.device('/cpu:0'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

允许 GPU 内存上涨

开始的时候只会占用一点 gpu 内存,随程序占用而变大。 但是不会释放(黑人问号??),因此可能导致更糟糕的内存分配。

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)

多 GPU 不看了,现在还没那个命。。。

LSTM 理解

LSTM 每个时刻都可以有一个输出,使用 keras 则是把最后一个时刻的输出提取出来了。

PROBLEMS

颤抖吧,人类。

Tensor Tensor() is not an element of this graph.

I had this problem when doing inference in a different thread than where I loaded my model. Here’s how I fixed the problem:

Right after loading or constructing your model, save the TensorFlow graph:
graph = tf.get_default_graph()

In the other thread (or perhaps in an asynchronous event handler), do:

global graph
with graph.as_default():
    (... do inference here ...)

I learned about this from https://www.tensorflow.org/versions/r0.11/api_docs/python/framework.html#get_default_graph

Indices and tables