神经网络--热压机温度预测

通过Tensorflow构建三层的简单神经网络,进行热压机温度预测

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import tensorflow as tf
import numpy as np
import tensorboard

fofdata = open('data.txt','r')
xof1 = []
for line in fofdata.readlines():
line = line.split('\n')
for a in line[0].split('\t'):
xof1.append(float(a))
fofdata.close()

xof1 = np.array(xof1)
xof1 = xof1.reshape(-1,5)

fofresult = open('result.txt','r')
yof1 = []
for line in fofresult.readlines():
line = line.split('\n')
for a in line[0].split('\t'):
yof1.append(float(a))
fofresult.close()

yof1 = np.array(yof1)
yof1 = yof1.reshape(-1,1)

with tf.name_scope('inputsoft'):
xoft = tf.placeholder(tf.float32,[None,5])
yoft = tf.placeholder(tf.float32,[None,1])

def add_layer(name,x,sizeofin,sizeofout,activation_function=None):
nameoflayer = "layer_{}".format(name)
with tf.name_scope(nameoflayer):
with tf.name_scope("weights"):
Weights = tf.Variable(tf.truncated_normal([sizeofin,sizeofout]),name="W")
tf.summary.histogram(nameoflayer+"/weights",Weights)
with tf.name_scope("bias"):
biases = tf.Variable(tf.random_normal([1,sizeofout]),name="b")
with tf.name_scope("Wx_add_b"):
Wx_add_b = tf.add(tf.matmul(x,Weights),biases)

if activation_function is None:
outputs = Wx_add_b
else :
outputs = activation_function(Wx_add_b,)
tf.summary.histogram(nameoflayer+"/outputs",outputs)
return outputs

#隐藏层,扩展到100个节点
layer1oft = add_layer("hiddenoft",xoft,5,15,activation_function=tf.nn.sigmoid)

#预测层,压缩到1个节点输出
predictionoft = add_layer("predictionoft",layer1oft,15,1,activation_function=None)

#定义温度网络的损失函数
with tf.name_scope('lossoft'):
lossoft = tf.reduce_mean(
tf.reduce_sum(tf.square(yoft-predictionoft),reduction_indices=[1])
)
tf.summary.scalar('lossoft',lossoft)

with tf.name_scope('trainoft'):
trainoft = tf.train.GradientDescentOptimizer(0.1).minimize(lossoft)

sessoft = tf.Session()
mergedoft = tf.summary.merge_all()

writeroft = tf.summary.FileWriter("logsofTempPrediciton/",sessoft.graph)

initofsess = tf.global_variables_initializer()
sessoft.run(initofsess)

for i in range(1000):
sessoft.run(trainoft,feed_dict={xoft: xof1,yoft: yof1})
if i % 20 == 0:
result = sessoft.run(mergedoft,feed_dict={xoft: xof1,yoft: yof1})
writeroft.add_summary(result, i)
if 1 < i < 100:
print(sessoft.run(lossoft,feed_dict={xoft:xof1,yoft:yof1}))
elif i % 50 == 0:
print(sessoft.run(lossoft,feed_dict={xoft:xof1,yoft:yof1}))
0%