Python两层神经网络
参考
神经网络15分钟入门——使用python从零开始写一个两层神经网络_Mr.看海的博客-CSDN博客_神经网络入门python
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 参考 https://blog.csdn.net/fengzhuqiaoqiu/article/details/102810048 import numpy as np ## 正向传播函数 # H = x*w + b def affine_formard(x,w,b): out = None N = x.shape[0] x_row = x.reshape(N,-1) out = np.dot(x_row, w) + b cache = (x, w, b) return out, cache ## 反向传播函数 def affine_backward(dout,cache): x, w, b = cache dx, dw, db = None, None, None dx = np.dot(dout, w.T) dx = np.reshape(dx,x.shape) x_row = x.reshape(x.shape[0],-1) dw = np.dot(x_row.T, dout) db = np.sum(dout, axis=0, keepdims=True) return dx, dw, db X = np.array([[2,1],[-1,1],[-1,-1],[1,-1]]) t = np.array([0,1,2,3]) np.random.seed(1) input_dim = X.shape[1] ## 输入参数的维度 num_classes = t.shape[0] ## 输出参数的维度 hidden_dim = 50 ## 隐藏层维度 reg = 0.001 ## 正则化强度 epsilon = 0.001 ## 梯度下降的学习率 W1 = np.random.randn(input_dim, hidden_dim) W2 = np.random.randn(hidden_dim, num_classes) b1 = np.zeros((1, hidden_dim)) b2 = np.zeros((1, num_classes)) for j in range(10000): H, fc_cache = affine_formard(X,W1,b1) H = np.maximum(0, H) relu_cache = H Y, cachey = affine_formard(H, W2, b2) ## Softmax 层计算 probs = np.exp(Y - np.max(Y, axis=1, keepdims=True)) probs /= np.sum(probs, axis=1, keepdims=True) ## 计算 loss N = Y.shape[0] # print(probs[np.arange(N),t]) loss = -np.sum(np.log(probs[np.arange(N), t]))/N print('loss:', loss) dx = probs.copy() dx[np.arange(N), t] -= 1 dx /= N dh1, dW2, db2 = affine_backward(dx, cachey) dh1[relu_cache <=0 ] = 0 dX, dW1, db1 = affine_backward(dh1, fc_cache) dW2 += reg * W2 dW1 += reg * W1 W2 += -epsilon * dW2 b2 += -epsilon * db2 W1 += -epsilon * dW1 b1 += -epsilon * db1 test = np.array([[2,2],[-2,2],[-2,-2],[2,-2]]) H, fc_cache = affine_formard(test, W1, b1) H = np.maximum(0,H) relu_cache = H Y, cachey = affine_formard(H, W2, b2) ## softmax probs = np.exp(Y - np.max(Y, axis=1, keepdims=True)) probs /= np.sum(probs, axis=1, keepdims=True) print(probs) for k in range(4): print(test[k,:],'所在的象限为', np.argmax(probs[k,:])+1)
python神经网络实例_TensorFlow神经网络函数拟合实例(基于Python)_weixin_39616367的博客-CSDN博客
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import time def add_layer(inputs, in_size, out_size, activation_function=None): weights = tf.Variable(tf.random_normal([in_size, out_size])) biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) wx_plus_b = tf.matmul(inputs, weights) + biases if activation_function is None: outputs = wx_plus_b else: outputs = activation_function(wx_plus_b) return outputs # 数据集生成 x_data = np.linspace(-1,2,num=300)[:,np.newaxis] # 添加噪音 noise = np.random.normal(0,0.05,x_data.shape) # 计算Y轴的值 y_data = np.square(x_data) - 0.5 + noise # 添加占位符 xs = tf.placeholder(tf.float32,[None,1]) ys = tf.placeholder(tf.float32,[None,1]) # 添加隐藏层 l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) # 添加输出层 prediction = add_layer(l1, 10, 1, activation_function=None) # 由此生成了两层神经网络 # 计算误差,并用梯度下降使得误差最小 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(x_data, y_data) plt.ion() plt.show() lines = None time.sleep(4) for i in range(1000): sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) try: ax.lines.remove(lines[0]) except Exception: pass prediction_value = sess.run(prediction, feed_dict={xs:x_data}) lines = ax.plot(x_data, prediction_value, 'r-', lw=5) plt.pause(0.1) writer = tf.train.SummaryWriter('',sess.graph)