TensorFlow 入门1---莫烦教程笔记

例子

x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# create tensorflow structure
# 产生随机数,最小值是-1,最大值是1
W = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

y = W*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 启动初始化
init = tf.initialize_all_variables()

# 激活
sess = tf.Session()
sess.run(init)

# 开始训练神经网络
for step in range(301):
    sess.run(train)
    if step%20 == 0:
        print(step,sess.run(W),sess.run(biases))

Session的使用

matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2)

# method1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()

# mothod2
# 用with的话会自动close session
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)

关于变量

state = tf.Variable(0, name = 'counter')
print(state.name)  # 输出  counter:0
one = tf.constant(1)
new_1 = tf.add(state, one)
update = tf.assign(state,new_1)

init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    for i in range(3):
        sess.run(update)  # 注意这句
        print(sess.run(state))
# 这里new_1 和 update 可以看成两个行为

placeholder

# input1 = tf.placeholder(tf.float32,[2,2]) # 规定结构是两行两列
# placeholder是在运行时再给值
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)

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

Activation Function

解决线性问题,也可以创造自己的激励函数,前提是此函数可微
在少量层网络中,激励函数的使用比较随意,深层网络激励函数需慎重考虑,有可能会导致梯度消失

添加层

def add_layer(inputs, in_size, out_size, activation_function=None): # 默认没有激活函数,即线性
    W = tf.Variable(tf.random_uniform([in_size, out_size])) # 生成随机变量的话可能比初始为0效果好
    b = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs,W) + b
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

构造神经网络结构

一个拟合曲线的例子:

import tensorflow as tf
import numpy as np

def add_layer(inputs, in_size, out_size, activation_function=None): # 默认没有激活函数,即线性
    W = tf.Variable(tf.random_uniform([in_size, out_size])) # 生成随机变量的话可能比初始为0效果好
    print(type(inputs))
    print(type(W))
    b = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(tf.cast(inputs,tf.float32),W) + b
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

xs = tf.placeholder(tf.float32, [None,1])
ys = tf.placeholder(tf.float32, [None,1])

# 生成数据
x_data = np.linspace(-1,1,300)[:,np.newaxis] # 创建一个从-1到1并含有300个数的等差数列,转化为列向量
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# 注意这里输入维度是1不是300
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)

# reduction_indices= [1]  # 按行求和
# reduction_indices= [0]  # 按列求和
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 0.1是学习率

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    if i % 50:
        print(sess.run(loss,feed_dict={xs:x_data, ys:y_data}))

numpy.random.nornal(loc = 0.0, scale = 1.0, size = None)
loc:正态分布均值,scale:正态分布标准差,size:输出的shape,默认只输出一个值

结果可视化

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(inputs, in_size, out_size, activation_function=None): # 默认没有激活函数,即线性
    W = tf.Variable(tf.random_uniform([in_size, out_size])) # 生成随机变量的话可能比初始为0效果好
    print(type(inputs))
    print(type(W))
    b = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(tf.cast(inputs,tf.float32),W) + b
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# 生成数据
x_data = np.linspace(-1,1,300)[:,np.newaxis] # 创建一个从-1到1并含有300个数的等差数列,转化为列向量
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise


xs = tf.placeholder(tf.float32, [None,1])
ys = tf.placeholder(tf.float32, [None,1])
# 注意这里输入维度是1不是300
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)

# reduction_indices= [1]  # 按行求和
# reduction_indices= [0]  # 按列求和
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 0.1是学习率

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

# 生成一个图片框
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.ion()
plt.show()

for i in range(1000):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    if i % 50 == 0 :
        # to see the step improvement
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        prediction_value = sess.run(prediction, feed_dict={xs:x_data})
        lines = ax.plot(x_data,prediction_value, 'r-', lw=5)
        # 如果想要连续plot,必须要将上次的抹除掉,然后再plot另外一条
        plt.pause(1)

可视化工具Tensorboard

初步使用

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(inputs, in_size, out_size, activation_function=None): # 默认没有激活函数,即线性
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            W = tf.Variable(tf.random_uniform([in_size, out_size]),name='weight') # 生成随机变量的话可能比初始为0效果好
        with tf.name_scope('biases'):
            b = tf.Variable(tf.zeros([1, out_size]) + 0.1, name = 'bias')
        with tf.name_scope('wx_b'):
            Wx_plus_b = tf.matmul(tf.cast(inputs,tf.float32),W) + b
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
    return outputs

# define placeholder for inputs to network
with tf.name_scope('inputs'):
    xs = tf.placeholder(tf.float64, [None,1], name = 'x_input')
    ys = tf.placeholder(tf.float64, [None,1], name='y_input')


# 生成数据
x_data = np.linspace(-1,1,300)[:,np.newaxis] # 创建一个从-1到1并含有300个数的等差数列,转化为列向量
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# 注意这里输入维度是1不是300
l1 = add_layer(x_data, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)

# reduction_indices= [1]  # 按行求和
# reduction_indices= [0]  # 按列求和
with tf.name_scope('loss'):
    loss = tf.reduce_sum(tf.reduce_mean(tf.square(y_data - prediction), reduction_indices=[1]))
with tf.name_scope('train'):
    train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 0.1是学习率

init = tf.initialize_all_variables()
sess = tf.Session()
writer = tf.summary.FileWriter('logs/', sess.graph)
sess.run(init)

在logs文件夹的上级文件夹中打开cmd,输入tensorboard --logdir=logs,然后在浏览器中输入:localhost:6006
对于 with tf.name_scope(‘inputs’) 这句,将会形成如下的一个框框:
1

可视化变量的变化

对于每一个想要观看其变化的变量,都加一个tf.summary.histogram
对于loss,使用tf.summary.scalar
用tf.summary.merge_all合并所有summary,记得summary也要run

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(n_layer, inputs, in_size, out_size, activation_function=None): # 默认没有激活函数,即线性
    layer_name = 'layer%s' % n_layer
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            W = tf.Variable(tf.random_uniform([in_size, out_size]),name='weight') # 生成随机变量的话可能比初始为0效果好
            tf.summary.histogram(layer_name + '/weight', W)
        with tf.name_scope('biases'):
            b = tf.Variable(tf.zeros([1, out_size]) + 0.1, name = 'bias')
            tf.summary.histogram(layer_name + '/bias', b)
        with tf.name_scope('wx_b'):
            Wx_plus_b = tf.matmul(tf.cast(inputs,tf.float32),W) + b
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
        tf.summary.histogram(layer_name + '/output', outputs)
    return outputs

# define placeholder for inputs to network
with tf.name_scope('inputs'):
    xs = tf.placeholder(tf.float32, [None,1], name = 'x_input')
    ys = tf.placeholder(tf.float32, [None,1], name='y_input')


# 生成数据
x_data = np.linspace(-1,1,300)[:,np.newaxis] # 创建一个从-1到1并含有300个数的等差数列,转化为列向量
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# 注意这里输入维度是1不是300
l1 = add_layer(1, xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(2, l1, 10, 1, activation_function=None)

# reduction_indices= [1]  # 按行求和
# reduction_indices= [0]  # 按列求和
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
    tf.summary.scalar('loss', loss)
with tf.name_scope('train'):
    train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 0.1是学习率

init = tf.initialize_all_variables()
sess = tf.Session()
# 把所有的summary合并打包放进summaryWriter里
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('logs/', sess.graph)

sess.run(init)
for i in range(1000):
    # 训练
    sess.run(train, feed_dict={xs: x_data, ys: y_data})
    if i%50 == 0:
        result = sess.run(merged, feed_dict={xs:x_data,ys:y_data})
        writer.add_summary(result,i) # 每隔五十步记录一次

打开localhost:6006,可以看到如下loss曲线:
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值