Tensorflow1.0 第一集: 最简单的神经网络

Tensorflow 1.0 第一集

(希望实习结束的时候就算我没学会tableau,也能学会Tensorflow吧!)

定义会话

import tensorflow as tf

m1 = tf.constant([[3,3]])
m2 = tf.constant([[2],[3]])
product = tf.matmul(m1, m2)
print(product)

# 定义一个会话,启动默认图
Sess = tf.Session()
print(Sess.run(product))
Sess.close()

变量的使用

要做全局初始化

import tensorflow as tf

x = tf.Variable([1,2])
a = tf.constant([3,3])

sub = tf.subtract(x, a)
add = tf.add(x, sub)

init = tf.global_variables_initializer() ## 变量的全局初始化
with tf.Session() as sess: #sess = tf.Session()
    sess.run(init)
    print(sess.run(sub))
    print(sess.run(add))

举个栗子

state = tf.Variable(0, name='counter')
new_value = tf.add(state, 1)
update = tf.assign(state, new_value) ## tensorflow里的赋值方法,不能用等号

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))

Fetch 和 feed的用法

feed很重要!

import tensorflow as tf
# Fetch: 在会话里运行多个op,得到结果
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

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

with tf.Session() as sess:
    result = sess.run([mu1, add]) # Fetch 运行了两个op
    print(result)

# feed: 最后传入赋值
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.], input2: [2.]})) #运行时传入,且传入必须为字典形式

再举个栗子

import tensorflow as tf
import numpy as np

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

# 使用tensorflow优化拟合函数
# 二次损失
loss = tf.reduce_mean(tf.square(y_data-y))
# 定义梯度下降法作为训练的优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.2)
# 定义最小化损失函数
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for step in range(201):
        sess.run(train)
        if step % 20 == 0:
            print(step, sess.run([k,b]))

非线性模型的小栗子 - 很简单的神经网络了

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

x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis]
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise

x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])

# 构建神经网络的中间层
Weight_L1 = tf.Variable(tf.random_normal([1, 10])) # 输入层1个神经元 中间层10个神经元
bias_L1 = tf.Variable(tf.zeros([1, 10]))
Wx_plus_b_L1 = tf.matmul(x, Weight_L1) + bias_L1
L1 = tf.nn.tanh(Wx_plus_b_L1) # 中间层的输出

# 构建神经网络的输出层
Weight_L2 = tf.Variable(tf.random_normal([10, 1]))
bias_L2 = tf.Variable(tf.zeros([1, 1]))
Wx_plus_b_L2 = tf.matmul(L1, Weight_L2) + bias_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)

## 定义损失函数、梯度下降法
loss = tf.reduce_mean(tf.square(y - prediction))
train_step = tf.train.GradientDescentOptimizer(learning_rate=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 = sess.run(prediction, feed_dict={x: x_data})
    plt.figure()
    plt.scatter(x_data, y_data)
    plt.plot(x_data, prediction, 'r-', lw = 5)
    plt.show()
上一篇:TensorFlow小记


下一篇:回溯算法(复杂度高)