tensorflow练习

python程序生成一些三维数据,然后用一个平面去拟合它

 1 #! /usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 
 4 
 5 
 6 import tensorflow as tf
 7 import numpy as np
 8 
 9 # 使用 NumPy 生成假数据(phony data), 总共 100 个点.
10 x_data = np.float32(np.random.rand(2, 100)) # 随机输入
11 y_data = np.dot([0.100, 0.200], x_data) + 0.300
12 
13 # 构造一个线性模型
14 # 
15 b = tf.Variable(tf.zeros([1]))
16 W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
17 y = tf.matmul(W, x_data) + b
18 
19 # 最小化方差
20 loss = tf.reduce_mean(tf.square(y - y_data))
21 optimizer = tf.train.GradientDescentOptimizer(0.5)
22 train = optimizer.minimize(loss)
23 
24 # 初始化变量
25 init = tf.initialize_all_variables()
26 
27 # 启动图 (graph)
28 sess = tf.Session()
29 sess.run(init)
30 
31 # 拟合平面
32 for step in xrange(0, 201):
33     sess.run(train)
34     if step % 20 == 0:
35         print step, sess.run(W), sess.run(b)
View Code

结果如下:

原文地址:https://www.cnblogs.com/Catherinezhilin/p/8035240.html