TensorFlow与简单的神经网络的实现


以下所有的代码片都经过测试,可以直接粘贴到开发环境中,建议使用Pycharm。代码片2,3,4中,输入层都由随机函数生成,所以每次运行结果都不相同。

代码片1

import tensorflow as tf

# 指定从输入层到隐藏第一层的权值
w1 = tf.constant([[0.2, 0.1, 0.4], [0.3, -0.5, 0.2]])

# 指定从隐藏第一层到输出层的权值
w2 = tf.constant([[0.6], [0.1], [-0.2]])

x = tf.constant([[0.7, 0.9]])

a = tf.matmul(x, w1)

y = tf.matmul(a, w2)

# 调用会话输出结果
sess = tf.Session()

print(sess.run(y))
sess.close()

代码片1运行结果


代码片2

import tensorflow as tf


'''
三层简单的神经网络程序,3.4.2,教材p55
'''


# 随机生成从输入层到隐藏第一层的权值
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))


# 随机生成从隐藏第一层到输出层的权值
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))


# 输入层的值(即Tensor)
x = tf.constant([[0.7, 0.9]])


# 定义向前传播的神经网络
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)


# 调用会话输出结果
sess = tf.Session()
sess.run(w1.initializer)
sess.run(w2.initializer)
print(sess.run(y))
sess.close()


代码片3

import tensorflow as tf


'''
三层简单的神经网络程序,3.4.4,教材p60
'''


# 定义两个变量
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1))


x = tf.placeholder(tf.float32, shape=(3, 2), name="input")
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)


sess = tf.Session()
# 使用tf.global_variables_initializer()来初始化所有的变量
init_op = tf.global_variables_initializer()
sess.run(init_op)


print(sess.run(y, feed_dict={x: [[0.7, 0.9],[0.1, 0.4],[0.5, 0.8]]}))


代码片4

import tensorflow as tf



# 定义两个变量
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1))
x = tf.placeholder(tf.float32, shape=(1, 2), name="input")


# 定义向前传播的神经网络
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)


# 调用会话输出结果
sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)


print(sess.run(y, feed_dict={x: [[0.7, 0.9]]}))


代码片5

import tensorflow as tf
from numpy.random import RandomState

'''
课本P62页代码:完整的神经网络样例程序
'''
# 1. 定义神经网络的参数,输入和输出节点。
batch_size = 8
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_ = tf.placeholder(tf.float32, shape=(None, 1), name='y-input')

# 2. 定义前向传播过程,损失函数及反向传播算法。
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

# 3. 生成模拟数据集。
rdm = RandomState(1)
X = rdm.rand(128,2)
Y = [[int(x1+x2 < 1)] for (x1, x2) in X]

# 4. 创建一个会话来运行TensorFlow程序。
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    # 输出目前(未经训练)的参数取值。
    print("w1:\n", sess.run(w1))
    print("w2:\n", sess.run(w2))
    print("\n")


    # 训练模型。
    STEPS = 5000
    for i in range(STEPS):
        start = (i * batch_size) % 128
        end = (i * batch_size) % 128 + batch_size
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
        if i % 1000 == 0:
            total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})
            print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy))

    # 输出训练后的参数取值。
    print("\n")
    print("w1:\n", sess.run(w1))
    print("w2:\n", sess.run(w2))



猜你喜欢

转载自blog.csdn.net/horacehe16/article/details/80542226