-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward.py
65 lines (55 loc) · 1.72 KB
/
forward.py
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
#!/usr/bin/env python
# coding=UTF-8
'''
@Description: the Sea shrimp price forecast's neural networks' forwoard
@version: 0.0.1
@Company: Student
@Author: StdKe
@LastEditors: StdKe
@Date: 2019-03-15 09:20:42
@LastEditTime: 2019-03-16 18:42:57
'''
import tensorflow as tf
INPUT_NODE = 5 # five features
OUTPUT_NODE = 1 # output is the price
LAYER1_NODE = 5 # first layer nodes
LAYER2_NODE = 5 # second layer nodes
'''
@name: get_weight
@description: generateing the weight "w"
@msg: The parameter satisfies the truncated normal distribution and use the regularizer
@param {node_number}{shape},{tf_regularizer}{regularizer}
@return: w
'''
def get_weight(shape,regularizer):
w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
if regularizer != None: tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(w))
return w
'''
@name: get_bias
@description: generate bias term
@msg:
@param: {node_number}{bias}
@return: b
'''
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
'''
@name: forwoard
@description: Forward propagation of neural networks ,Contains two hidden layers
@msg: Except for the last layer of the output layer, all other layer outputs are passed the activation function rule
@param {matrix} {x} ,{tf_regularizer} {regularizer}
@return: y
'''
def forwoard(x,regularizer):
w1 = get_weight([INPUT_NODE,LAYER1_NODE],regularizer)
b1 = get_bias([LAYER1_NODE])
y1 = tf.nn.relu(tf.matmul(x,w1)+b1)
w2 = get_weight([LAYER1_NODE,LAYER2_NODE],regularizer)
b2 = get_bias([LAYER2_NODE])
y2 = tf.nn.relu(tf.matmul(y1,w2) + b2)
w3 = get_weight([LAYER2_NODE,OUTPUT_NODE],regularizer)
b3 = get_bias([OUTPUT_NODE])
y = tf.matmul(y2,w3) + b3
return y