TensorFlow 编译
tensorflow可以通过pip安装,也可以通过源码安装,其中pip安装直接
[ccei]pip3 install tensorflow[/ccei]
即可
基于源码的安装教程在https://www.tensorflow.org/install/install_sources 可找到,核心难点在于处理包的依赖问题
[ccei]sudo pip install six numpy wheel
brew install coreutils[/ccei]
运行./configure检查是否完全安装依赖库
TensorFlow 使用
使用tensorflow解决MNIST问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[cce_python] import input_data import tensorflow as tf mnist = input_data.read_data_sets("data/",one_hot=True) sess = tf.InteractiveSession() x = tf.placeholder("float", shape=[None, 784]) y_ = tf.placeholder("float", shape=[None, 10]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) sess.run(tf.global_variables_initializer()) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) for i in range(1000): batch = mnist.train.next_batch(50)#随机选择50个 train_step.run(feed_dict={x: batch[0], y_: batch[1]}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})) [/cce_python] |
具体每一个函数都是干嘛的太麻烦,就不写了,反正代码也不是我写的,网上一查一大堆……