即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Tensorflow Code2

编程语言 zeroQiaoba 16℃ 0评论

参考资料,莫烦的tensorflow讲解传送门,以下是对他讲解的总结。


代码2

# View more python tutorial on my Youtube and Youku channel!!!

# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial

"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""

from __future__ import print_function
import tensorflow as tf
import numpy as np

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()
### create tensorflow structure end ###

sess = tf.Session()

sess.run(init)

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))

tensorflow的代码需要定义图结构


再init,采用方法如下

init = tf.global_variables_initializer()

之后建立一个Session,再对于Session.run(init),再Session.run(train)才算是真正开始运行程序了。

转载请注明:CodingBlog » Tensorflow Code2

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情