-
Notifications
You must be signed in to change notification settings - Fork 0
/
overfitting.py
49 lines (40 loc) · 1.76 KB
/
overfitting.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
import tensorflow as tf
def layer(Inp, Inp_size, Neuron_num, lay_name, keep_prob=1, Act_function=None):
with tf.name_scope(str(lay_name)):
with tf.name_scope('Weight'):
Weight = tf.Variable(tf.random_normal([Inp_size,Neuron_num],dtype=tf.float32,seed=1),name='Weight')
tf.summary.histogram('Weight',Weight)
with tf.name_scope('Basic'):
Basic = tf.Variable(tf.random_normal([1,Neuron_num],dtype=tf.float32,seed=1),name='Basic')
tf.summary.histogram('Basic',Basic)
with tf.name_scope('Out_Put'):
Outputs_temp = tf.nn.dropout(tf.matmul(Inp,Weight)+ Basic,keep_prob)
if Act_function is None:
Outputs = Outputs_temp
else:
Outputs = Act_function(Outputs_temp)
tf.summary.histogram('Output',Outputs)
return Outputs
def loss_step(Z_data,Z_pre):
with tf.name_scope('Loss'):
loss_pre = tf.reduce_mean(tf.reduce_sum(tf.square(Z_data-Z_pre),reduction_indices=[1]))
tf.summary.scalar('Loss',loss_pre)
return loss_pre
def train_step(loss):
with tf.name_scope('Train'):
train = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
return train
def sess_step(mode = 1,proportion = 0.333):
if mode == 1:
#训练方式为指定使用一定比例的Gpu容量
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=proportion)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
elif mode == 2:
#训练方式为按使用增长所占Gpu容量
config=tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
else:
#使用cpu训练模型
sess = tf.Session()
return sess