tensorflow前向传播过程的实践

作者: shaneZhang 分类: 机器学习的实践 发布时间: 2018-12-23 22:24

代码如下:

#coding:utf-8
import  tensorflow as tf

#定义输入和参数
x = tf.placeholder(tf.float32,shape=(1,2))
#stddev为均方差 seed为随机种子
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 in this python is:\n",sess.run(y,feed_dict={x:[[0.7,0.5]]})


# 用 placeholder 实现输入定义(sess.run 中喂入多组数据)的情况
# 第一组喂体积 0.7、重量 0.5,第二组喂体积 0.2、重量 0.3,第三组喂体积 0.3 、 重量 0.4,第四组喂体积 0.4、重量 0.5.
#定义输入和参数
x = tf.placeholder(tf.float32,shape=(None,2))
#stddev为均方差 seed为随机种子
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 in this python is:\n",sess.run(y,feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]})

输出的结果如下:

y in this python is:
[[3.0904665]]
y in this python is:
[[3.0904665]
 [1.2236414]
 [1.7270732]
 [2.2305048]]

本页面支持繁体中文友好显示:tensorflow前向传播过程的实践

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复