-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBP_tensor
More file actions
42 lines (34 loc) · 1.5 KB
/
Copy pathBP_tensor
File metadata and controls
42 lines (34 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import tensorflow as tf
import numpy as np
#添加层
def addLayer(inputData,inSize,outSize,activity_function = None):
Weights = tf.Variable(tf.random_normal([inSize,outSize]))
basis = tf.Variable(tf.zeros([1,outSize])+0.1)
weights_plus_b = tf.matmul(inputData,Weights)+basis
if activity_function is None:
ans = weights_plus_b
else:
ans = activity_function(weights_plus_b)
return ans
#训练的数据
x_data = np.linspace(-1,1,300)[:,np.newaxis] # 转为列向量
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]) # 样本数未知,特征数为1,占位符最后要以字典形式在运行中填入
ys = tf.placeholder(tf.float32,[None,1])
#定义神经层:隐藏层和预测层
l1 = addLayer(xs,1,10,activity_function=tf.nn.relu) #relu作为激活函数
l2 = addLayer(l1,10,1,activity_function=None)
#定义 loss 表达式
loss = tf.reduce_mean(tf.reduce_sum(tf.square((ys-l2)),reduction_indices = [1]))#需要向相加索引号,redeuc执行跨纬度操作
#优化loss
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 选择梯度下降法
#开始训练
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(10000):
sess.run(train,feed_dict={xs:x_data,ys:y_data})
if i%50 == 0:
print sess.run(loss,feed_dict={xs:x_data,ys:y_data}) flowflow