anaconda
pip install --ignore-installed --upgrade tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple
创建图,启动图,常量
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
m1 = tf.constant([[3, 3]])
m2 = tf.constant([[2], [3]])
# 计算 2x1矩阵 * 1x2矩阵
product = tf.matmul(m1, m2)
print(product)
# Tensor("MatMul:0", shape=(1, 1), dtype=int32)
# 定义一个绘画,启动默认图
# 调用sess的run方法来执行矩阵乘法op
# run(product)触发了图中的3个op
# sess = tf.Session()
# result = sess.run(product)
# sess.close()
# 一般像下面这样定义会话
with tf.Session() as sess:
result = sess.run(product)
print(result)
# [[15]]
变量
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
x = tf.Variable([1, 2])
a = tf.constant([3, 3])
# 增加减法和加法两个op
sub = tf.subtract(x, a)
add = tf.add(x, sub)
# 使用变量前必须进行初始化,这里使用全局初始化操作
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(sub))
# [-2 - 1]
print(sess.run(add))
# [-1 1]
# -------------------------------------------------------
# 定义一个变量初始化为0
state = tf.Variable(0, name='counter')
new_value = tf.add(state, 1)
# 定义一个赋值的op,类似 state=new_value
update = tf.assign(state, new_value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(state))
# 循环执行五次
for _ in range(5):
sess.run(update)
# 执行5次 state = state+1
print(sess.run(state))
# 0
# 1
# 2
# 3
# 4
# 5
feed和fetch
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Fetch 同时运行多个op
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
add = tf.add(input2, input3)
mul = tf.multiply(input1, add)
with tf.Session() as sess:
result = sess.run([mul, add])
print(result)
# [21.0, 7.0]
result1 = sess.run(mul)
print(result1)
# 21.0
# Feed
# 创建占位符,在运行时才传入值
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
# feed 的数据以字典的形式传入
print(sess.run(output, feed_dict={input1: [7], input2: [2]}))
# [14.]
tf基本训练流程
import tensorflow as tf
import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 使用numpy生成100个随机点(生成假设样本点)
x_data = np.random.rand(100)
y_data = x_data * 0.1 + 0.2
# 构建一个线性模型,来预测样本点(k和b的初始值对最后训练出来的结果无影响)
b = tf.Variable(0.)
k = tf.Variable(0.)
# 训练结果越好,k就越接近真是斜率 0.1 ,b就越接近 0.2
y = k * x_data + b
# 二次代价函数(y_data真实值-y预测值,即得到误差的平方再求平均值)
loss = tf.reduce_mean(tf.square(y_data - y))
# 定义一个梯度下降法来进行训练的优化器
optimizer = tf.train.GradientDescentOptimizer(0.2)
# 最小化代价函数
train = optimizer.minimize(loss)
# 变量初始化
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# 训练201次,每20次打印一下k和b的值
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run([k, b]))
# 0[0.045916118, 0.097572036]
# 20[0.0970313, 0.20139201]
# 40[0.09822227, 0.20083371]
# 60[0.0989354, 0.20049927]
# 80[0.099362485, 0.20029898]
# 100[0.09961822, 0.20017906]
# 120[0.099771395, 0.2001072]
# 140[0.099863105, 0.2000642]
# 160[0.09991802, 0.20003845]
# 180[0.0999509, 0.20002304]
# 200[0.099970594, 0.20001379]
非线性回归
import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 使用numpy生成100个随机点(在-0.5到0.5 均匀分布 的100个点)(变成200行1列的矩阵)
x_data = np.linspace(-0.5, 0.5, 100)[:, np.newaxis]
# 生成与x_data形状相同的随机噪声值
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise
# 定义两个placeholder, 形状[None, 1] 任意行 一列
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])
# 定义神经网络中间层
# 权值连接输入层和中间层,输入层点为一个神经元,假定中间层有10个神经元[1, 10]
Weights_L1 = tf.Variable(tf.random_normal([1, 10]))
biases_L1 = tf.Variable(tf.zeros([1, 10]))
# 双曲正切函数作为激活函数,得到中间层L1的输出
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
# 定义输出层
Weights_L2 = tf.Variable(tf.random_normal([10, 1]))
biases_L2 = tf.Variable(tf.zeros([1, 1]))
Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)
# 二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
# 使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
# 变量的初始化
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step, feed_dict={x: x_data, y: y_data})
# 训练完成,开始预测
prediction_value = sess.run(prediction, feed_dict={x: x_data})
# 画图
plt.figure()
# 画实际点
plt.scatter(x_data, y_data)
# 用红色实线,线宽为5 画出预测值
plt.plot(x_data, prediction_value, 'r-', lw=5)
plt.show()
输出 :

网友评论