1. 安装
首先安装 Python3.6,然后安装 tensorflow1.x
pip install tensorflow==1.15.0
2. 例子
import tensorflow as tf
import numpy as np
from numpy.random import RandomState
data_size = 128
batch_size = 8
learning_rate = 0.001
n_epochs = 5000
# 生成数据集
rdm = RandomState(1)
X = rdm.random([data_size, 2])
Y = np.array([[int(x1 + x2 > 1)] for (x1, x2) in X])
# ************************************** 定义模型-begin ************************************** #
x = tf.placeholder(dtype=tf.float32, shape=(None, 2), name='x-input')
y = tf.placeholder(dtype=tf.float32, shape=(None, 1), name='y-input')
w1 = tf.Variable(initial_value=tf.random_normal(shape=(2, 3), stddev=1, seed=1))
w2 = tf.Variable(initial_value=tf.random_normal(shape=(3, 1), stddev=1, seed=1))
# 网络层
h = tf.matmul(x, w1)
y_pred = tf.sigmoid(tf.matmul(h, w2))
# loss
cross_entropy = -tf.reduce_mean(y * tf.log(tf.clip_by_value(y_pred, 1e-10, 1.0)) + (1 - y)
* tf.log(tf.clip_by_value(1 - y_pred, 1e-10, 1.0)))
# 后向传播
train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
# ************************************** 定义模型-end ************************************** #
with tf.Session() as sess:
# 初始化所有的数据
tf.global_variables_initializer().run()
for i in range(1, n_epochs + 1):
start = i * batch_size % data_size
end = min(start + batch_size, data_size)
sess.run(train_step, feed_dict={x: X[start:end], y: Y[start:end]})
if i % 1000 == 0:
print(f'After loop {i}, cross entropy loss is:{sess.run(cross_entropy, feed_dict={x: X, y: Y})}')
3. name_scope
在某个 name_scope 中定义的所有变量,他们的 name 属性上都会增加区域名作为前缀,用以区别对象属于哪个区域,便于在 tensorboard 中展示清晰的逻辑关系图。
a = tf.Variable(tf.constant(4), name='a')
print(f"without name_scope: {a.name}")
with tf.name_scope('scope1'):
a = tf.Variable(tf.constant(4), name='a')
print(f"with name_scope: {a.name}")
without name_scope: a:0
with name_scope: scope1/a:0
4. variable_scope
tf.Variable()
每次都会新建变量,如果希望重用(共享)一些变量,就需要用到了 tf.get_variable()
,它会去搜索变量名,有就直接用,没有再新建。其中定义的变量也会增加区域名作为前缀。
with tf.variable_scope('scope1', reuse=tf.AUTO_REUSE):
a = tf.get_variable(name='a', dtype=tf.float32,
initializer=tf.random_normal(shape=(2, 3), stddev=1, seed=1))
b = tf.get_variable(name='a')
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(a.name)
print(b.name)
scope1/a:0
scope1/a:0