搭建一个简单的神经网络(向前传播)



代码实现1:

#两层简单神经网络(全连接)
import tensorflow as tf

#定义输入和参数
x=tf.constant([[0.7,0.5]])#一组X,表示体积和重量
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))#两行三列的正态分布随机数组成的矩阵
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))

#定义向前传播过程
a=tf.matmul(x,w1)
y=tf.matmul(a,w2)

#用会话计算结果
with tf.Session() as sess:
         init_op=tf.global_variables_initializer()
         sess.run(init_op)
         print("y is :",sess.run(y))

输出结果:

 RESTART: C:/Users/lenovo/AppData/Local/Programs/Python/Python36/simplenn.py 

y is : [[3.0904665]]


代码实现2:

#两层简单神经网络
import tensorflow as tf

#定义输入和参数
#用placeholder实现输入定义(sess.run中喂一组数据)
x=tf.placeholder(tf.float32,shape=(1,2))#一组X,表示体积和重量
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))#两行三列的正态分布随机数组成的矩阵
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))

#定义向前传播过程
a=tf.matmul(x,w1)
y=tf.matmul(a,w2)

#用会话计算结果
with tf.Session() as sess:
         init_op=tf.global_variables_initializer()
         sess.run(init_op)
         print("y is :",sess.run(y,feed_dict={x:[[0.7,0.5]]}))

输出结果:

RESTART: C:/Users/lenovo/AppData/Local/Programs/Python/Python36/simplenn2.py 

y is : [[3.0904665]]


代码实现3:

#两层简单神经网络(全连接)
import tensorflow as tf

#定义输入和参数
x=tf.placeholder(tf.float32,shape=(None,2))
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))#两行三列的正态分布随机数组成的矩阵
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))

#定义向前传播过程
a=tf.matmul(x,w1)
y=tf.matmul(a,w2)

#用会话计算结果
with tf.Session() as sess:
         init_op=tf.global_variables_initializer()
         sess.run(init_op)
         print("y is :",sess.run(y,feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]}))

输出结果:

 RESTART: C:/Users/lenovo/AppData/Local/Programs/Python/Python36/simplenn3.py 
y is : [[3.0904665]
 [1.2236414]
 [1.7270732]
 [2.2305048]]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值