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

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.

主站蜘蛛池模板: 皋兰县| 神农架林区| 临泽县| 桑日县| 甘孜县| 黑龙江省| 改则县| 贵溪市| 西丰县| 广东省| 梁山县| 喀什市| 乳源| 盐池县| 谢通门县| 鸡西市| 巴马| 扬中市| 漳州市| 罗山县| 花垣县| 海安县| 阳城县| 定日县| 婺源县| 舟曲县| 荔波县| 上思县| 绥芬河市| 循化| 新泰市| 银川市| 乐安县| 湄潭县| 黄平县| 镇远县| 商洛市| 遂溪县| 修武县| 花莲县| 丹江口市|