官术网_书友最值得收藏!

Code execution

Let's run this code for h_size of 128, standard deviation of 0.1, and sgd_step of 0.01:

def run(h_size, stddev, sgd_step):
...

def main():
run(128,0.1,0.01)

if __name__ == '__main__':
main()

The preceding code outputs the following graph, which plots the steps versus the test and train accuracy:

Let's compare the change in SGD steps and its effect on training accuracy. The following code is very similar to the previous code example, but we will rerun it for multiple SGD steps to see how SGD steps affect accuracy levels.

def run(h_size, stddev, sgd_steps):
....
test_accs = []
train_accs = []
time_taken_summary = []
for sgd_step in sgd_steps:
start_time = time.time()
updates_sgd = tf.train.GradientDescentOptimizer(sgd_step).minimize(cost)
sess = tf.Session()
init = tf.initialize_all_variables()
steps = 50
sess.run(init)
x = np.arange(steps)
test_acc = []
train_acc = []

print("Step, train accuracy, test accuracy")


for step in range(steps):
# Train with each example
for i in range(len(train_x)):
sess.run(updates_sgd, feed_dict={X: train_x[i: i + 1],
y: train_y[i: i + 1]})

train_accuracy = np.mean(np.argmax(train_y, axis=1) ==
sess.run(predict,
feed_dict={X: train_x, y: train_y}))
test_accuracy = np.mean(np.argmax(test_y, axis=1) ==
sess.run(predict,
feed_dict={X: test_x, y: test_y}))

print("%d, %.2f%%, %.2f%%"
% (step + 1, 100. * train_accuracy, 100. * test_accuracy))
#x.append(step)
test_acc.append(100. * test_accuracy)
train_acc.append(100. * train_accuracy)
end_time = time.time()
diff = end_time -start_time
time_taken_summary.append((sgd_step,diff))
t = [np.array(test_acc)]
t.append(train_acc)
train_accs.append(train_acc)

Output of the preceding code will be an array with training and test accuracy for each SGD step value. In our example, we called the function sgd_steps for an SGD step value of [0.01, 0.02, 0.03]:

def main():
sgd_steps = [0.01,0.02,0.03]
run(128,0.1,sgd_steps)

if __name__ == '__main__':
main()

This is the plot showing how training accuracy changes with sgd_steps. For an SGD value of 0.03, it reaches a higher accuracy faster as the step size is larger.

主站蜘蛛池模板: 湖口县| 彭山县| 彝良县| 平乡县| 景洪市| 万安县| 临清市| 张北县| 乌什县| 老河口市| 长海县| 贡觉县| 南召县| 建平县| 万山特区| 绥江县| 会昌县| 报价| 灵武市| 余姚市| 南涧| 来安县| 景洪市| 满城县| 讷河市| 中江县| 东港市| 湄潭县| 城固县| 曲松县| 清涧县| 越西县| 苗栗市| 青冈县| 兰坪| 蒙自县| 武陟县| 织金县| 沂源县| 黎川县| 潍坊市|