# coding:utf8
"""
author:无米的快乐生活~
"""
from __future__ import print_function
import tensorflow as tf
# 常量节点1
node1 = tf.constant(3.0, dtype=tf.float32)
# 常量节点2
node2 = tf.constant(4.0)
print(node1)
print(node2)
#Tensor("Const:0", shape=(), dtype=float32)
#Tensor("Const_1:0", shape=(), dtype=float32)
# 创建 Session 对象,并调用 run 函数执行节点
sess = tf.Session()
print(sess.run([node1, node2]))
#[3.0, 4.0]
node3 = tf.add(node1, node2)
print("node3:", node3)
#node3: Tensor("Add:0", shape=(), dtype=float32)
print("sess.run(node3):", sess.run(node3))
#sess.run(node3): 7.0
# 占位符,参数化接受外部输入
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b
print(adder_node)
#Tensor("add:0", dtype=float32)
print(tf.add(a,b))
#Tensor("Add_1:0", dtype=float32)
print(sess.run(adder_node, {a:3, b:4.5}))
#[ 3. 7.]
print(sess.run(adder_node, {a:[1,3], b:[2,4]}))
#[ 3. 7.]
add_and_tripple = adder_node * 3
print(sess.run(add_and_tripple, {a:3, b:4.5}))
#22.5
'''
创建并使用模型
'''
# 变量允许我们像graph 中添加可训练的参数,通过类型和初始value 创建
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
print(W, b, x, linear_model)
#<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
#显式调用初始化所有变量的函数
init = tf.global_variables_initializer()
print(W, b, x, linear_model)
#<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
sess.run(init)
print(W, b, x, linear_model)
#<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
print(sess.run(linear_model, {x:[1, 2,3, 4]}))
#[ 0. 0.30000001 0.60000002 0.90000004]
'''
评估模型
'''
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
#23.66
# 我们非常精准地猜出了 W 和 b 的精确值……
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
#0.0
'''
优化
'''
# 梯度下降优化,为什么是0.01?
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# 从新初始化参数
sess.run(init)
for i in range(1000):
sess.run(train, {x: [1, 2, 3, 4], y: [ 0, -1, -2, -3]})
print(sess.run([W, b]))
#[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
print(sess.run([W, b, loss], {x: [1, 2, 3, 4], y: [ 0, -1, -2, -3]}))
#[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32), 5.6999738e-11]
Tensorflow基础学习
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- TensorFlow支持各种异构的平台,支持多CPU/GPU,服务器,移动设备,具有良好的跨平台的特性;Tenso...
- 前言 上一篇文章中搭建了CUDA9.0 + cuDNN7.0的基础环境. 先简单说下即将要进行的步骤以及原因。 1...