tensorflow示例代码注释1
2016-06-28 10:01
316 查看
#!/usr/bin/env python
import tensorflow as tf
//导入numpy科学计算包
import numpy as np
//numpy.linspace(start,
stop, num=50, endpoint=True, retstep=False,
dtype=None),分配从-1到1中间的101个数,包含-1和1,构成一个线性数组trX
trX = np.linspace(-1, 1, 101)
//randn 返回一个样本,具有标准正态分布,ndarray.shape:数组的维度,numpy的array就是一个矩阵,不同于python中的list,tuple,dict,set
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is approximately linear but with some random noise
//placeholder,占位符
X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")
def model(X, w):
return tf.mul(X, w) # lr is just X*w so this model line is pretty simple
w = tf.Variable(0.0, name="weights") # create a shared variable (like theano.shared) for the weight matrix
y_model = model(X, w)
//方差
cost = tf.square(Y - y_model) # use square error for cost function
//梯度下降类,最小目标cost,学习率0.01
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # construct an optimizer to minimize cost and fit line to my data
# Launch the graph in a session
with tf.Session() as sess:
# you need to initialize variables (in this case just variable W)
tf.initialize_all_variables().run()
for i in range(100):
//python的zip函数,对矩阵求T,如([1,2],[3,4])---->([1,3],[2,4])
for (x, y) in zip(trX, trY):
sess.run(train_op, feed_dict={X: x, Y: y})
print(sess.run(w)) # It should be something around 2
import tensorflow as tf
//导入numpy科学计算包
import numpy as np
//numpy.linspace(start,
stop, num=50, endpoint=True, retstep=False,
dtype=None),分配从-1到1中间的101个数,包含-1和1,构成一个线性数组trX
trX = np.linspace(-1, 1, 101)
//randn 返回一个样本,具有标准正态分布,ndarray.shape:数组的维度,numpy的array就是一个矩阵,不同于python中的list,tuple,dict,set
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is approximately linear but with some random noise
//placeholder,占位符
X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")
def model(X, w):
return tf.mul(X, w) # lr is just X*w so this model line is pretty simple
w = tf.Variable(0.0, name="weights") # create a shared variable (like theano.shared) for the weight matrix
y_model = model(X, w)
//方差
cost = tf.square(Y - y_model) # use square error for cost function
//梯度下降类,最小目标cost,学习率0.01
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # construct an optimizer to minimize cost and fit line to my data
# Launch the graph in a session
with tf.Session() as sess:
# you need to initialize variables (in this case just variable W)
tf.initialize_all_variables().run()
for i in range(100):
//python的zip函数,对矩阵求T,如([1,2],[3,4])---->([1,3],[2,4])
for (x, y) in zip(trX, trY):
sess.run(train_op, feed_dict={X: x, Y: y})
print(sess.run(w)) # It should be something around 2
相关文章推荐
- RXJava——线程控制:Scheduler (二)
- JAVA 快递查询接口API调用-快递鸟接口
- C语言预处理条件语句的 与或运算
- Java中Properties类
- python 字典(dict)按键和值排序
- eclipse package,source folder,folder区别及相互转换
- Delphi反汇编内部字符串处理函数/过程不完全列表
- 代码管理_SVN_TortoiseSVN 冲突解决详细步骤 (图)
- 本地环境 XAMPP+phpStorm+XDebug+chrome 配置和断点调试
- 代码管理_SVN_TortoiseSVN使用教程
- JAVA 泛型,集合使用方法
- RTP协议全解析(H264码流和PS流)
- Spring实现AOP的方式
- 快递鸟物流查询接口API调用代码示例
- C++ - 蓝桥杯 - 算法提高 学霸的迷宫 (bfs+记录路径)
- java随机动态生成汉字验证码图片的实例代码分享
- java数据库操作
- matlab查看程序运行占用了多少空间
- C#用网易邮箱发送邮件(同步异步)
- java编程相关总结(一)