#coding:utf-8
#0导入模块 ，生成模拟数据集
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import opt4_8_generateds
import opt4_8_forward

STEPS = 40000
BATCH_SIZE = 30 
LEARNING_RATE_BASE = 0.001
LEARNING_RATE_DECAY = 0.999
REGULARIZER = 0.01

def backward():
	x = tf.placeholder(tf.float32, shape=(None, 2))
	y_ = tf.placeholder(tf.float32, shape=(None, 1))

	X, Y_, Y_c = opt4_8_generateds.generateds()

	y = opt4_8_forward.forward(x, REGULARIZER)
	
	global_step = tf.Variable(0,trainable=False)	

	learning_rate = tf.train.exponential_decay(
		LEARNING_RATE_BASE,
		global_step,
		300/BATCH_SIZE,
		LEARNING_RATE_DECAY,
		staircase=True)


	#定义损失函数
	loss_mse = tf.reduce_mean(tf.square(y-y_))
	loss_total = loss_mse + tf.add_n(tf.get_collection('losses'))
	
	#定义反向传播方法：包含正则化
	train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total)

	with tf.Session() as sess:
		init_op = tf.global_variables_initializer()
		sess.run(init_op)
		for i in range(STEPS):
			start = (i*BATCH_SIZE) % 300
			end = start + BATCH_SIZE
			sess.run(train_step, feed_dict={x: X[start:end], y_:Y_[start:end]})
			if i % 2000 == 0:
				loss_v = sess.run(loss_total, feed_dict={x:X,y_:Y_})
				print("After %d steps, loss is: %f" %(i, loss_v))

		xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
		grid = np.c_[xx.ravel(), yy.ravel()]
		probs = sess.run(y, feed_dict={x:grid})
		probs = probs.reshape(xx.shape)
	
	plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c)) 
	plt.contour(xx, yy, probs, levels=[.5])
	plt.show()
	
if __name__=='__main__':
	backward()

[((280, 323), 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '(None, 2)'}), '(tf.float32, shape=(None, 2))\n', (294, 323), True, 'import tensorflow as tf\n'), ((330, 373), 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '(None, 1)'}), '(tf.float32, shape=(None, 1))\n', (344, 373), True, 'import tensorflow as tf\n'), ((389, 419), 'opt4_8_generateds.generateds', 'opt4_8_generateds.generateds', ([], {}), '()\n', (417, 419), False, 'import opt4_8_generateds\n'), ((426, 464), 'opt4_8_forward.forward', 'opt4_8_forward.forward', (['x', 'REGULARIZER'], {}), '(x, REGULARIZER)\n', (448, 464), False, 'import opt4_8_forward\n'), ((482, 513), 'tensorflow.Variable', 'tf.Variable', (['(0)'], {'trainable': '(False)'}), '(0, trainable=False)\n', (493, 513), True, 'import tensorflow as tf\n'), ((532, 650), 'tensorflow.train.exponential_decay', 'tf.train.exponential_decay', (['LEARNING_RATE_BASE', 'global_step', '(300 / BATCH_SIZE)', 'LEARNING_RATE_DECAY'], {'staircase': '(True)'}), '(LEARNING_RATE_BASE, global_step, 300 /\n    BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True)\n', (558, 650), True, 'import tensorflow as tf\n'), ((1457, 1497), 'matplotlib.pyplot.contour', 'plt.contour', (['xx', 'yy', 'probs'], {'levels': '[0.5]'}), '(xx, yy, probs, levels=[0.5])\n', (1468, 1497), True, 'import matplotlib.pyplot as plt\n'), ((1498, 1508), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1506, 1508), True, 'import matplotlib.pyplot as plt\n'), ((694, 711), 'tensorflow.square', 'tf.square', (['(y - y_)'], {}), '(y - y_)\n', (703, 711), True, 'import tensorflow as tf\n'), ((873, 885), 'tensorflow.Session', 'tf.Session', ([], {}), '()\n', (883, 885), True, 'import tensorflow as tf\n'), ((907, 940), 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ([], {}), '()\n', (938, 940), True, 'import tensorflow as tf\n'), ((745, 772), 'tensorflow.get_collection', 'tf.get_collection', (['"""losses"""'], {}), "('losses')\n", (762, 772), True, 'import tensorflow as tf\n'), ((807, 844), 'tensorflow.train.AdamOptimizer', 'tf.train.AdamOptimizer', (['learning_rate'], {}), '(learning_rate)\n', (829, 844), True, 'import tensorflow as tf\n'), ((1438, 1453), 'numpy.squeeze', 'np.squeeze', (['Y_c'], {}), '(Y_c)\n', (1448, 1453), True, 'import numpy as np\n')]