Tensorflow Day1

Tensor_flow The Second Lesson

2-1 创建图、启动图

包括对图(Graphs),会话(Session),张量(Tensor),变量(Variable)的一些解释和操作

  1. 使用图来表示计算任务
  2. 在被称之为会话的上下文中执行图
  3. 使用tensor表示数据
  4. 通过变量维护状态
  5. 使用feed和fetch可以为任意的操作赋值或者从其中获取数据

Tensorflow是一个编程系统,使用图来表示计算任务,图中的节点称之为op(operation),一个op获得0个或多个Tensor,
执行计算,产生0个或多个Tensor。Tensor看作是一个n维数组或列表。图必须在会话里面被启动。
Tensorflow结构

import tensorflow as tf
m1 = tf.constant([[3, 3]])                  # 定义两个常量op
m2 = tf.constant([[2], [3]])                # 创建一个矩阵乘法op,把m1和m2传入
product = tf.matmul(m1, m2)
print(product)                              # 未启动会话窗,输出一个Tensor

sess = tf.Session()                         # 定义一个会话窗,启动默认图
result = sess.run(product)                  # 调用sess的run方法来执行矩阵乘法op,run出发了图中的3个op
print(result)
sess.close()

with tf.Session() as sess:                  # 该方法不用使用sess.close()来关闭会话 
    result = sess.run(product)
    print(result)

2-2 变量

import tensorflow as tf

x = tf.Variable([1, 2])
a = tf.constant([3, 3])
sub = tf.subtract(x, a)                      # 增加一个减法op
add = tf.add(x, sub)                         # 增加一个加法op

init = tf.global_variables_initializer()     # 初始化全局变量

with tf.Session() as sess:
     sess.run(init)
     print(sess.run(sub))
     print(sess.run(add))

state = tf.Variable(0, name='counter')        # 创建一个变量初始化为0
new_value = tf.add(state, 1)                  # 创建一个op,作用是使state加1
update = tf.assign(state, new_value)          # 赋值op
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(state))
    for _ in range(5):
        sess.run(update)
        print(sess.run(state))

2-3 Fetch and Feed

Fetch指的是可以同时run多个op
Feed的使用,占位符placeholder,使用feed_dict = 来赋值

import tensorflow as tf
# Fetch
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(1.0)

add = tf.add(input1, input2)
mul = tf.multiply(add, input3)

with tf.Session() as sess:
    result = sess.run([mul, add])                   # Fetch的用法
    print(result)

# Feed
# 创建占位符placeholder
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
    result = sess.run(output, feed_dict={input1: [8.], input2: [2.]})
    print(result)

2-4 Tensorflow的简单使用案例

import tensorflow as tf
import numpy as np

# 使用numpy创建100个随机点
x_data = np.random.rand(100)
y_data = x_data*0.1 + 0.2                           # 我们需要去拟合的真实线

# 构造一个线性模型
b = tf.Variable(0.)
k = tf.Variable(0.)
y = k*x_data + b

# 二次代价函数
loss = tf.reduce_mean(tf.square(y_data - y))
# 定义一个梯度下降法来进行训练的优化器
optimizer = tf.train.GradientDescentOptimizer(0.2)  # 0.2表示学习率
# 最小化代价函数
train = optimizer.minimize(loss)

# 初始化变量
init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    for step in range(201):
        sess.run(train)
        if step%20 == 0:
            print(sess.run([k, b]))
'''
运行结果:
[0.053331703, 0.09987458]
[0.103005074, 0.19838887]
[0.10165868, 0.19911078]
[0.100915514, 0.19950919]
[0.10050532, 0.1997291]
[0.100278914, 0.19985047]
[0.10015395, 0.19991747]
[0.100084975, 0.19995445]
[0.10004688, 0.19997486]
[0.10002587, 0.19998613]
[0.10001429, 0.19999234]
'''
posted @ 2020-01-12 17:26  旅人_Eric  阅读(102)  评论(0编辑  收藏  举报