import matplotlib
.pyplot
as plt
import numpy
as np
import csv
import tensorflow
as tf
sess
=tf
.Session
()
birth_weight_file
= 'birth.csv'
birth_data
= []
with open(birth_weight_file
, newline
='') as csvfile
:
csv_reader
= csv
.reader
(csvfile
)
birth_header
= next(csv_reader
)
for row
in csv_reader
:
birth_data
.append
(row
)
birth_data
= [[float(x
) for x
in row
] for row
in birth_data
]
y_vals
= np
.array
([x
[10] for x
in birth_data
])
cols_of_interest
= ['AGE', 'LWT', 'RACE', 'SMOKE', 'PTL', 'HT', 'UI','PTV']
x_vals
= np
.array
(
[[x
[ix
] for ix
, feature
in enumerate(birth_header
) if feature
in cols_of_interest
] for x
in birth_data
])
seed
=3
tf
.set_random_seed
(seed
)
batch_size
=100
train_indices
=np
.random
.choice
(len(x_vals
),round(len(x_vals
)*0.8),replace
=False)
test_indices
=np
.array
(list(set(range(len(x_vals
)))-set(train_indices
)))
x_vals_train
=x_vals
[train_indices
]
x_vals_test
=x_vals
[test_indices
]
y_vals_train
=y_vals
[train_indices
]
y_vals_test
=y_vals
[test_indices
]
def normalize_cols(m
):
col_max
=m
.max(axis
=0)
col_min
=m
.min(axis
=0)
return(m
-col_min
)/(col_max
-col_min
)
x_vals_train
=np
.nan_to_num
(normalize_cols
(x_vals_train
))
x_vals_test
=np
.nan_to_num
(normalize_cols
(x_vals_test
))
def init_weight(shape
,st_dev
):
weight
=tf
.Variable
(tf
.random_normal
(shape
,stddev
=st_dev
))
return(weight
)
def init_bias(shape
,st_dev
):
bias
=tf
.Variable
(tf
.random_normal
(shape
,stddev
=st_dev
))
return(bias
)
x_data
=tf
.placeholder
(shape
=[None,8],dtype
=tf
.float32
)
y_target
=tf
.placeholder
(shape
=[None,1],dtype
=tf
.float32
)
def fully_connected(input_layer
,weights
,biases
):
layer
=tf
.add
(tf
.matmul
(input_layer
,weights
),biases
)
return(tf
.nn
.relu
(layer
))
weight_1
=init_weight
(shape
=[8,25],st_dev
=10.0)
bias_1
=init_bias
(shape
=[25],st_dev
=10.0)
layer_1
=fully_connected
(x_data
,weight_1
,bias_1
)
weight_2
=init_weight
(shape
=[25,10],st_dev
=10.0)
bias_2
=init_bias
(shape
=[10],st_dev
=10.0)
layer_2
=fully_connected
(layer_1
,weight_2
,bias_2
)
weight_3
=init_weight
(shape
=[10,3],st_dev
=10.0)
bias_3
=init_bias
(shape
=[3],st_dev
=10.0)
layer_3
=fully_connected
(layer_2
,weight_3
,bias_3
)
weight_4
=init_weight
(shape
=[3,1],st_dev
=10.0)
bias_4
=init_bias
(shape
=[1],st_dev
=10.0)
final_output
=fully_connected
(layer_3
,weight_4
,bias_4
)
loss
=tf
.reduce_mean
(tf
.abs(y_target
- final_output
))
my_opt
=tf
.train
.AdamOptimizer
(0.05)
train_step
=my_opt
.minimize
(loss
)
init
=tf
.global_variables_initializer
()
sess
.run
(init
)
loss_vec
=[]
test_loss
=[]
for i
in range(200):
rand_index
=np
.random
.choice
(len(x_vals_train
),size
=batch_size
)
rand_x
=x_vals_train
[rand_index
]
rand_y
=np
.transpose
([y_vals_train
[rand_index
]])
sess
.run
(train_step
,feed_dict
={x_data
:rand_x
,y_target
:rand_y
})
temp_loss
=sess
.run
(loss
,feed_dict
={x_data
:rand_x
,y_target
:rand_y
})
loss_vec
.append
(temp_loss
)
test_temp_loss
=sess
.run
(loss
,feed_dict
={x_data
:x_vals_test
,y_target
:np
.transpose
([y_vals_test
])})
test_loss
.append
(test_temp_loss
)
if(i
+1)%25==0:
print("Generation : "+str(i
+1) +' Loss = '+str(temp_loss
))
转载请注明原文地址: https://mac.8miu.com/read-485772.html