用tensorflow模仿BP神经网络执行过程

news/2024/11/24 3:36:53/

文章目录

  • 用矩阵运算仿真BP神经网络
          • y= relu ( (X․W ) + b )
          • y= sigmoid ( (X․W ) + b )
  • 以随机数产生Weight(W)与bais(b)
          • placeholder
  • 建立layer函数
  • 改进layer函数,使其能返回w和b

github地址https://github.com/fz861062923/TensorFlow

用矩阵运算仿真BP神经网络

import tensorflow as tf
import numpy as np
C:\Users\admin\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_converters
y= relu ( (X․W ) + b )
X = tf.Variable([[0.4,0.2,0.4]])W = tf.Variable([[-0.5,-0.2 ],[-0.3, 0.4 ],[-0.5, 0.2 ]])b = tf.Variable([[0.1,0.2]])XWb =tf.matmul(X,W)+by=tf.nn.relu(tf.matmul(X,W)+b)#引用ReLU激活函数with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)print('XWb:')    print(sess.run(XWb ))    print('y:')    print(sess.run(y ))
XWb:
[[-0.35999998  0.28      ]]
y:
[[0.   0.28]]
y= sigmoid ( (X․W ) + b )
X = tf.Variable([[0.4,0.2,0.4]])W = tf.Variable([[-0.5,-0.2 ],[-0.3, 0.4 ],[-0.5, 0.2 ]])b = tf.Variable([[0.1,0.2]])XWb=tf.matmul(X,W)+by=tf.nn.sigmoid(tf.matmul(X,W)+b)with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)print('XWb:')    print(sess.run(XWb))    print('y:')    print(sess.run(y ))
XWb:
[[-0.35999998  0.28      ]]
y:
[[0.41095957 0.5695462 ]]

以随机数产生Weight(W)与bais(b)

W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.Variable([[0.4,0.2,0.4]])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)print('b:')print(sess.run(b ))    print('W:')print(sess.run(W ))print('y:')print(sess.run(y ))    
b:
[[-0.1278446   0.15272076]]
W:
[[ 0.09724175 -0.70408934][ 1.4106061  -0.39071304][-0.74939483 -0.36333686]]
y:
[[0. 0.]]
W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.Variable([[0.4,0.2,0.4]])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)(_b,_W,_y)=sess.run((b,W,y))#等价于三次seprint('b:')print(_b)print('W:')print(_W)print('y:')print(_y)   
b:
[[-0.5877543  2.1299696]]
W:
[[ 1.0390263   0.5285081 ][-0.92886233 -0.5300881 ][ 0.4078475   0.684533  ]]
y:
[[0.        2.5091684]]
placeholder
W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.placeholder("float", [None,3])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)X_array = np.array([[0.4,0.2,0.4]])(_b,_W,_X,_y)=sess.run((b,W,X,y),feed_dict={X:X_array})print('b:')print(_b)    print('W:')print(_W)print('X:')print(_X)print('y:')print(_y)
b:
[[-1.00988698 -0.90781182]]
W:
[[ 0.77819425  0.74534345][ 0.62385881 -0.30757746][ 0.84864932  1.10149086]]
X:
[[ 0.40000001  0.2         0.40000001]]
y:
[[ 0.  0.]]
_y.shape
(1, 2)
ts_norm = tf.random_normal([1000])
with tf.Session() as session:norm_data=ts_norm.eval()
print(len(norm_data))
print(norm_data[:30])
1000
[-0.62594283 -1.53080451  0.20968008  0.48862299 -0.98033726  1.568721060.34392843 -0.32248533 -1.38410163 -0.8074798   0.06213726  0.41173849-0.79638833  0.07239912 -1.5461148  -1.4486984   0.5450505   0.37378398-0.23069905 -0.26489291 -1.30195487 -0.18677172  0.50207907 -1.007878420.56418502  0.51869804 -1.74017227 -2.36948991  0.98451078  0.93969965]
import matplotlib.pyplot as plt
plt.hist(norm_data)
plt.show()    

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.placeholder("float", [None,3])
y=tf.nn.sigmoid(tf.matmul(X,W)+b)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)X_array = np.array([[0.4,0.2 ,0.4],[0.3,0.4 ,0.5],[0.3,-0.4,0.5]])    (_b,_W,_X,_y)=sess.run((b,W,X,y),feed_dict={X:X_array})print('b:')print(_b)    print('W:')print(_W)print('X:')print(_X)print('y:')print(_y)
b:
[[ 1.3742691 -0.6307982]]
W:
[[-0.25237647  0.28716296][-0.24805067 -0.40259644][ 1.6787063  -1.9441847 ]]
X:
[[ 0.4  0.2  0.4][ 0.3  0.4  0.5][ 0.3 -0.4  0.5]]
y:
[[0.86934626 0.20195402][0.88479966 0.15738963][0.90353453 0.20493953]]

建立layer函数

功能是建立两层神经网络

def layer(output_dim,input_dim,inputs, activation=None):W = tf.Variable(tf.random_normal([input_dim, output_dim]))b = tf.Variable(tf.random_normal([1, output_dim]))XWb = tf.matmul(inputs, W) + bif activation is None:outputs = XWbelse:outputs = activation(XWb)return outputs
X = tf.placeholder("float", [None,4])y=layer(output_dim=3,input_dim=4,inputs=X,activation=tf.nn.relu)with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)X_array = np.array([[0.4,0.2 ,0.4,0.1],[0.3,0.4 ,0.5,0.3],[0.3,-0.4,0.5,0.2]])    (_X,_y)=sess.run((X,y),feed_dict={X:X_array})print('X:')print(_X)print('y:')print(_y)    
X:
[[ 0.4  0.2  0.4  0.1][ 0.3  0.4  0.5  0.3][ 0.3 -0.4  0.5  0.2]]
y:
[[0.         0.79015875 1.5285197 ][0.31821787 1.2630901  1.1021228 ][0.48398763 0.33778787 1.7573613 ]]
X = tf.placeholder("float", [None,4])
h=layer(output_dim=3,input_dim=4,inputs=X,activation=tf.nn.relu)
y=layer(output_dim=2,input_dim=3,inputs=h)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)X_array = np.array([[0.4,0.2 ,0.4,0.5]])    (layer_X,layer_h,layer_y)= \sess.run((X,h,y),feed_dict={X:X_array})print('input Layer X:')print(layer_X)print('hidden Layer h:')print(layer_h)print('output Layer y:')print(layer_y)
input Layer X:
[[0.4 0.2 0.4 0.5]]
hidden Layer h:
[[1.5489424  0.         0.63559824]]
output Layer y:
[[3.4448848  0.05538869]]

改进layer函数,使其能返回w和b

def layer_2(output_dim,input_dim,inputs, activation=None):W = tf.Variable(tf.random_normal([input_dim, output_dim]))b = tf.Variable(tf.random_normal([1, output_dim]))XWb = tf.matmul(inputs, W) + bif activation is None:outputs = XWbelse:outputs = activation(XWb)return outputs,W,b
X = tf.placeholder("float", [None,4])
h,W1,b1=layer_debug(output_dim=3,input_dim=4,inputs=X,activation=tf.nn.relu)
y,W2,b2=layer_debug(output_dim=2,input_dim=3,inputs=h)
with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)X_array = np.array([[0.4,0.2 ,0.4,0.5]])    (layer_X,layer_h,layer_y,W1,b1,W2,b2)= \sess.run((X,h,y,W1,b1,W2,b2),feed_dict={X:X_array})print('input Layer X:')print(layer_X)print('W1:')print(  W1)    print('b1:')print(  b1)    print('hidden Layer h:')print(layer_h)    print('W2:')print(  W2)    print('b2:')print(  b2)    print('output Layer y:')print(layer_y)
input Layer X:
[[0.4 0.2 0.4 0.5]]
W1:
[[-2.494698    0.10329538 -0.5353932 ][-1.022263   -1.1610479  -3.0859344 ][ 1.9652166   0.3464464   1.2346822 ][-1.0058508  -0.81840676 -0.9512821 ]]
b1:
[[ 0.1718771   0.93178105 -1.4650283 ]]
hidden Layer h:
[[0.        0.4702648 0.       ]]
W2:
[[ 0.80631006  1.5261457 ][ 0.24046504 -0.08329547][ 0.2570049   0.40859744]]
b2:
[[-0.16517083  0.45186767]]
output Layer y:
[[-0.05208859  0.41269675]]

http://www.ppmy.cn/news/1353763.html

相关文章

C++中的volatile:穿越编译器的屏障

C中的volatile:穿越编译器的屏障 在C编程中,我们经常会遇到需要与硬件交互或多线程环境下访问共享数据的情况。为了确保程序的正确性和可预测性,C提供了关键字volatile来修饰变量。本文将深入解析C中的volatile关键字,介绍其作用、…

java8-重构、测试、调试

8.1.1 改善代码的可读性 改善代码的可读性到底意味着什么?我们很难定义什么是好的可读性,因为这可能非常主观。通常的理解是,“别人理解这段代码的难易程度”。改善可读性意味着你要确保你的代码能非常容易地被包括自己在内的所有人理解和维护。为了确保…

LCR 127. 跳跃训练【简单】

LCR 127. 跳跃训练 题目描述: 今天的有氧运动训练内容是在一个长条形的平台上跳跃。平台有 num 个小格子,每次可以选择跳 一个格子 或者 两个格子。请返回在训练过程中,学员们共有多少种不同的跳跃方式。 结果可能过大,因此结果…

【STM32 CubeMX】I2C中断方式与DMA方式

文章目录 前言一、I2C中断方式1.1 CubeMX配置I2C中断1.2 I2C中断函数使用Master模式Mem模式 1.3 DMA方式发送和接收CubeMX配置IIC DMA方式Master模式Mem模式 总结 前言 在STM32 CubeMX环境中,I2C(Inter-Integrated Circuit)通信协议的实现可…

【C++】类和对象(五)友元、内部类、匿名对象

前言:前面我们说到类和对象是一个十分漫长的荆棘地,今天我们将走到终点,也就是说我们对于C算是正式的入门了。 💖 博主CSDN主页:卫卫卫的个人主页 💞 👉 专栏分类:高质量C学习 &…

BUGKU-WEB bp

题目描述 题目截图如下: 进入场景看看: 解题思路 提示说:弱密码top1000?z???(爆破?)先看看源码有没有提示 相关工具 Burp Suit 爆破top1000字典,点击下载 解题步骤 随便测试账号密码admin、admin 得到提…

docker (四)-docker网络

默认网络 docker会自动创建三个网络,bridge,host,none bridge桥接网络 如果不指定,新创建的容器默认将连接到bridge网络。 默认情况下,使用bridge网络,宿主机可以ping通容器ip,容器中也能ping通宿主机。 容器之间只…

【pandas 不同文件读取和存储】

文章目录 一、Pandas 文件读取和存储概览二、读取不同类型的文件1. CSV文件的读取与存储代码及解释: 2. Excel文件的读取与存储代码及解释: 3. JSON文件的读取与存储代码及解释: 4. SQL数据库的读取与存储代码及解释: 5. 其他格式…