-- coding: utf-8 --
"""
Created on Sat Oct 13 22:42:35 2018
使用tensorflow框架完成一个神经网络模型
@author: ltx
"""
import tensorflow as tf
from tensorflow.python.framework import ops
import numpy as np
import matplotlib.pyplot as plt
import tf_utils
创建热键函数
def create_placeholders(n_x,n_y):
X=tf.placeholder(tf.float32,[n_x,None],name="X")
Y=tf.placeholder(tf.float32,[n_y,None],name="Y")
return X,Y
---------读取数据集------------
train_x,train_y,test_x,test_y,classes=tf_utils.load_dataset()
m=np.shape(train_x)[0]
print("m,n="+str(np.shape(train_y)))
plt.imshow(train_x[11])
print("m,n="+str(np.shape(train_x[11])))
print("Y="+str(train_y[0,11]))
print("Y="+str(np.squeeze(train_y[0,11])))
-----------扁平化图像数据---------------------
X_train_flatten=train_x.reshape(train_x.shape[0],-1).T
X_test_flatten=test_x.reshape(test_x.shape[0],-1).T
归一化数据
X_train=X_train_flatten/255
X_test=X_test_flatten/255
Y_train=tf_utils.convert_to_one_hot(train_y,6)
Y_test=tf_utils.convert_to_one_hot(test_y,6)
初始化模型参数xavier
def initialparameters():
tf.set_random_seed(1)
W1=tf.get_variable("W1",[25,12288],initializer=tf.contrib.layers.xavier_initializer(seed=1))
b1=tf.get_variable("b1",[25,1],initializer=tf.zeros_initializer)
W2=tf.get_variable("W2",[12,25],initializer=tf.contrib.layers.xavier_initializer(seed=1))
b2=tf.get_variable("b2",[12,1],initializer=tf.zeros_initializer)
W3=tf.get_variable("W3",[6,12],initializer=tf.contrib.layers.xavier_initializer(seed=1))
b3=tf.get_variable("b3",[6,1],initializer=tf.zeros_initializer)
parameters={"W1":W1,
"b1":b1,
"W2":W2,
"b2":b2,
"W3":W3,
"b3":b3
}
return parameters
-------向前传播---------------
def forward(X,parameters):
W1=parameters["W1"]
b1=parameters["b1"]
W2=parameters["W2"]
b2=parameters["b2"]
W3=parameters["W3"]
b3=parameters["b3"]
print("W1="+str(np.shape(W1)))
print("X="+str(np.shape(X)))
print("b1="+str(np.shape(b1)))
Z1=tf.matmul(W1,X)+b1
A1=tf.nn.relu(Z1)
Z2=tf.matmul(W2,A1)+b2
A2=tf.nn.relu(Z2)
Z3=tf.matmul(W3,A2)+b3
return Z3
----------计算成本--------------
def compute_cost(Z3,Y):
cost=tf.nn.softmax_cross_entropy_with_logits(logits=tf.transpose(Z3),labels=tf.transpose(Y))
cost=tf.reduce_mean(cost)
return cost
----------------向后传播-----------------------
def back(cost,learning_rate):
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
return optimizer
--------------构建自己的模型-----------------
def model(X_train,Y_train,epoches=1500,learning_rate=0.0001,batch_size=32,is_plot=True):
ops.reset_default_graph()
costs=[]
seed=3
#定义X,Y热键,可以先不用赋值。
n_x=X_train.shape[0]
n_y=Y_train.shape[0]
X,Y=create_placeholders(n_x,n_y)
#初始化模型参数
parameters=initialparameters()
#向前传播
Z3=forward(X,parameters)
#计算cost
cost=compute_cost(Z3,Y)
#向后传播,优化参数
optimizer=back(cost,learning_rate)
#初始化所有参数
inits=tf.global_variables_initializer()
#使用minibatch循环更新parameters
with tf.Session() as sess:
#首先初始化所有变量
sess.run(inits)
for epoch in range(epoches):
seed=seed+1
epcost=0
batchNum=int(m/batch_size)
batches=tf_utils.random_mini_batches(X_train,Y_train,batch_size,seed)
for batch in batches:
(batch_x,batch_y)=batch
_,minibatch_cost=sess.run([optimizer,cost],feed_dict={X:batch_x,Y:batch_y})
epcost=epcost+minibatch_cost
# sess.run(optimizer,feed_dict={X:batch_x, Y:batch_y})
# epcost=epcost+sess.run(cost,feed_dict={X:batch_x, Y:batch_y})
epcost=epcost/batchNum
if(epoch%5==0):
costs.append(epcost)
if(epoch % 100==0):
print("epcost="+str(epcost))
#是否绘制图谱
if is_plot:
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
#计算模型的准确度
accurate=tf.reduce_mean(sess.run(Z3)-Y_train)
print("accurate"+str(accurate))
return parameters
model(X_train=X_train,Y_train=Y_train)
-----------------------实验结果------------------------------
网友评论