当前位置:   article > 正文

神经网络完成分类(正则化)_rdmxnp

rdmxnp

了解Tensorflow入门非常推荐MOOC网上的这个课程:

人工智能——Tensorflow笔记

将蓝点、红点用一条曲线分隔开开。

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Aug 1 20:06:08 2019
  4. @author: ushop
  5. """
  6. '''
  7. 正则化缓解过拟合:
  8. 正则化在损失函数中引入模型复杂度指标
  9. 利用给W加权值,弱化了数据噪声(一般正则化b)
  10. loss = loss(y-y_)+REGULARIZER * loss(W)
  11. loss(y_-y):模型中所有参数的损失函数(交叉熵、均方误差)
  12. REGULARIZER:给出参数W在loss中的比例,正则化的权重
  13. loss(l1)(W) = sum(|Wi|)
  14. loss(W)=tf.contrib.layers.l1_regularizer(REGULARIZER)(W)
  15. loss(l2)(W) = sum(|Wi|^2)
  16. loss(W)=tf.contrib.layers.l2_regularizer(REGULARIZER)(W)
  17. #把内容加到集合对应位置做加法
  18. tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(REGULARIZER)(W)
  19. loss = cem + tf.add_n(tf.get_collection('losses'))
  20. '''
  21. import tensorflow as tf
  22. import pylab
  23. import numpy as np
  24. BATCH_SIZE = 30
  25. seed =2
  26. #基于seed产生随机数
  27. rdm = np.random.RandomState(seed)
  28. #随机数返回300*2的矩阵 作为输入数据[X0,X1]
  29. X = rdm.randn(300,2)
  30. #计算出标准答案
  31. Y_ = [int(X0*X0+X1*X1<2) for (X0,X1) in X]
  32. #遍历Y_中的值 1-'red' 0-'blue'
  33. Y_c = [['red' if y else 'blue']for y in Y_]
  34. #print(Y_c)
  35. #将维度操作
  36. #print(np.squeeze(Y_c))
  37. #对数据集X和标签Y进行shape处理,第一个元素为-1,随第二个元素计算得
  38. X = np.vstack(X).reshape(-1,2)
  39. Y_ = np.vstack(Y_).reshape(-1,1)
  40. #print(X,Y_)
  41. #用pylab.scatter绘制出数据姐X的离散点(X0,X1),用Y_c中的颜色对应
  42. pylab.scatter(X[:,0], X[:,1], c = np.squeeze(Y_c))
  43. pylab.show()
  44. #定义神经网络的输入、参数、输出,定义向前传播过程
  45. def get_weight(shape, regularizer):
  46. w = tf.Variable(tf.random_normal(shape), dtype=tf.float32)
  47. tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
  48. return w
  49. def get_bias(shape):
  50. b = tf.Variable(tf.constant(0.01,shape=shape))
  51. return b
  52. #创建输入变量
  53. x = tf.placeholder(tf.float32, shape=(None,2))
  54. y_ = tf.placeholder(tf.float32, shape=(None,1))
  55. #生成隐藏层
  56. w1 = get_weight([2,11], 0.01)
  57. b1 = get_bias([11])
  58. y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
  59. w2 = get_weight([11, 1], 0.01)
  60. b2 = get_bias([1])
  61. #输出层不激活
  62. y = tf.matmul(y1, w2) + b2
  63. #定义损失函数
  64. loss_mse = tf.reduce_mean(tf.square(y - y_))
  65. loss_total = loss_mse + tf.add_n(tf.get_collection('losses'))
  66. #定义反向传播方法:不含正则化
  67. train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_mse)
  68. with tf.Session() as sess:
  69. init_op = tf.global_variables_initializer()
  70. sess.run(init_op)
  71. STEPS = 40000
  72. for i in range(STEPS):
  73. start = (i*BATCH_SIZE) % 300
  74. end = start + BATCH_SIZE
  75. sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]})
  76. if i%2000==0:
  77. loss_mse_v = sess.run(loss_mse, feed_dict={x:X,y_:Y_})
  78. print("After %d steps, loss is %f" %(i,loss_mse_v))
  79. #三维作图
  80. #xx在-3到3之间以步长为0.01,yy在-3到3之间以步长为0.01,生成二维网格坐标点
  81. xx,yy = np.mgrid[-3:3:0.01, -3:3:0.01]
  82. #将xx,yy拉直,并合成一个2列的矩阵 得到一个网格坐标点的集合
  83. grid = np.c_[xx.ravel(),yy.ravel()]
  84. #将网格坐标点喂入神经网络 probs为输出
  85. probs = sess.run(y, feed_dict={x:grid})
  86. #prods的shape调整成xx的样子
  87. probs = probs.reshape(xx.shape)
  88. #print("w1:\n",sess.run(w1))
  89. #print("b1:\n",sess.run(b1))
  90. #print("w2:\n",sess.run(w2))
  91. #print("b2:\n",sess.run(b2))
  92. pylab.scatter(X[:,0],X[:,1],c=np.squeeze(Y_c))
  93. pylab.contour(xx, yy ,probs, levels=[.5])
  94. pylab.show()
  95. #定义反向传播方向:正则化
  96. train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_total)
  97. with tf.Session() as sess:
  98. init_op = tf.global_variables_initializer()
  99. sess.run(init_op)
  100. STEPS = 40000
  101. for i in range(STEPS):
  102. start = (i*BATCH_SIZE) % 300
  103. end = start + BATCH_SIZE
  104. sess.run(train_step, feed_dict={x:X[start:end],y_:Y_[start:end]})
  105. if i % 2000 == 0:
  106. loss_total_v = sess.run(loss_total, feed_dict={x:X, y_:Y_})
  107. print("After %d stpes loss_total is %f" % (i,loss_total_v))
  108. xx,yy = np.mgrid[-3:3:0.01, -3:3:0.01]
  109. grid = np.c_[xx.ravel(), yy.ravel()]
  110. probs = sess.run(y, feed_dict={x:grid})
  111. probs = probs.reshape(xx.shape)
  112. #print("w1:\n",sess.run(w1))
  113. #print("b1:\n",sess.run(b1))
  114. #print("w2:\n",sess.run(w2))
  115. #print("b2:\n",sess.run(b2))
  116. pylab.scatter(X[:,0],X[:,1],c=np.squeeze(Y_c))
  117. pylab.contour(xx,yy,probs, levels=[.5])
  118. pylab.show()

非正则化结果                                    

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/663619
推荐阅读
相关标签
  

闽ICP备14008679号