用下面代码测试安装:
1 #! /usr/bin/python 2 # -*- coding: utf-8 -*- 3 4 import tensorflow as tf 5 import numpy 6 import matplotlib.pyplot as plt 7 rng = numpy.random 8 9 learning_rate = 0.01 10 training_epochs = 1000 11 display_step = 50 12 #数据集x 13 train_X = numpy.asarray([3.3,4.4,5.5,7.997,5.654,.71,6.93,4.168,9.779,6.182,7.59,2.167, 14 7.042,10.791,5.313,9.27,3.1]) 15 #数据集y 16 train_Y = numpy.asarray([1.7,2.76,3.366,2.596,2.53,1.221,1.694,1.573,3.465,1.65,2.09, 17 2.827,3.19,2.904,2.42,2.94,1.3]) 18 n_samples = train_X.shape[0] 19 X = tf.placeholder("float") 20 Y = tf.placeholder("float") 21 22 W = tf.Variable(rng.randn(), name="weight") 23 b = tf.Variable(rng.randn(), name="bias") 24 25 pred = tf.add(tf.multiply(X, W), b) 26 27 cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples) 28 29 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 30 31 init = tf.initialize_all_variables() 32 with tf.Session() as sess: 33 sess.run(init) 34 35 # 训练数据 36 for epoch in range(training_epochs): 37 for (x, y) in zip(train_X, train_Y): 38 sess.run(optimizer, feed_dict={X: x, Y: y}) 39 40 print "优化完成!" 41 training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) 42 print "Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n' 43 44 #可视化显示 45 plt.plot(train_X, train_Y, 'ro', label='Original data') 46 plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') 47 plt.legend() 48 plt.show()
test
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:在ubuntu 16.04上安装tensorflow,并测试成功 - Python技术站