Tensorflow1.x入门

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值