tensorflow示例代码注释1

#!/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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值