神经网络模拟思路:
1 构建数据集 :通过np.linspace(a,b,c)来构建一个从a到b中取c个数。再用np.reshape(a,(b,c))改变输入数据x的矩阵形状,a表示的是哪个数据被改变,(b,c)是将其改变成这样形状的矩阵
2 创建神经网络模型参数部分 :首先定好参数,再将神经元中的权值和偏重全部定义好
3 进行训练 : 调用生成函数,建立占位符(一般在train函数上生成占位符),调用函数梯度方法修正函数。
mport tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def gen_data():
x = np.linspace(0,2*np.pi,100)
x = np.reshape(x,(len(x),1))
y = np.sin(x)
return x,y
# 创建神经网络模型
def inference(x):
w1 = tf.Variable(tf.truncated_normal(shape=[INPUT_NODE,HIDDEN_NODE],stddev=0.1,dtype=tf.float32),name='w1')
b1 = tf.Variable(tf.constant(0,dtype=tf.float32,shape=[HIDDEN_NODE]))
a1 = tf.nn.sigmoid(tf.matmul(x,w1))
w2 = tf.Variable(tf.truncated_normal(shape=[HIDDEN_NODE, OUTPUT_NODE], stddev=0.1, dtype=tf.float32), name='w2')
b2 = tf.Variable(tf.constant(0, dtype=tf.float32, shape=[OUTPUT_NODE]))
y_ = tf.matmul(a1,w2) + b2
return y_
def train():
train_x,train_y = gen_data()
x = tf.placeholder(dtype=tf.float32, shape=[None, INPUT_NODE], name='input_x')
y_ = tf.placeholder(dtype=tf.float32, shape=[None, OUTPUT_NODE], name='input_y')
y = inference(x)
loss = tf.reduce_mean(tf.square(y_ - y))
train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)
np.random.seed(200)
shuffle_index = np.random.permutation(train_x.shape[0])
shuffled_X = train_x[shuffle_index]
shuffled_y = train_y[shuffle_index]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(train_x,train_y,lw=5,c='r')
plt.ion()
plt.show()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(50000):
feed_dic = {x:shuffled_X,y_:shuffled_y}
_,l = sess.run([train_step,loss],feed_dict=feed_dic)
if(i+1)%50 == 0:
print("### loss on train: ", l)
try:
ax.lines.remove(lines[0])
except Exception:
pass
y_pre = sess.run(y, feed_dict={x: train_x})
lines = ax.plot(train_x, y_pre, c='black')
plt.pause(0.1)
if __name__ == '__main__':
# x = np.linspace(1,50)
# x = np.reshape(x,(5,5,2))
# print(x)
INPUT_NODE = 1
OUTPUT_NODE = 1
HIDDEN_NODE = 50
LEARNING_RATE = 0.1
# a = np.random.permutation(x)
# print(a)
# print(x)
train()
这里需要注意的是:
1 np.random.permutataion的用法:这个可以打乱数组的顺序,.shape[0]的意思是数据的index,意思是将index打乱。
2 占位符一般写在train()函数里面