为什么80%的码农都做不了架构师?>>>   

例子1

先从helloworld开始:

t@ubuntu:~$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello=tf.constant('hello,tensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
hello,tensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(122)
>>> print sess.run(a+b)
132

接下去两个步骤:1,学python;2,看ts;

例子2

手写数字识别,在ubuntu中安装部署好环境;

代码源自https://github.com/niektemme/tensorflow-mnist-predict

创建训练用python代码

# Copyright 2016 Niek Temme.
# Adapted form the on the MNIST biginners tutorial by Google.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================="""A very simple MNIST classifier.
Documentation at
http://niektemme.com/ @@to doThis script is based on the Tensoflow MNIST beginners tutorial
See extensive documentation for the tutorial at
https://www.tensorflow.org/versions/master/tutorials/mnist/beginners/index.html
"""#import modules
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data#import data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)# init_op = tf.global_variables_initializer() 看版本,使用该行还是使用下面那行
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()# Train the model and save the model to disk as a model.ckpt file
# file is stored in the same directory as this python script is started
"""
The use of 'with tf.Session() as sess:' is taken from the Tensor flow documentation
on on saving and restoring variables.
https://www.tensorflow.org/versions/master/how_tos/variables/index.html
"""
with tf.Session() as sess:sess.run(init_op)for i in range(1000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})save_path = saver.save(sess, "/tmp/model.ckpt")print ("Model saved in file: ", save_path)

测试代码

# Copyright 2016 Niek Temme.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================="""Predict a handwritten integer (MNIST beginners).Script requires
1) saved model (model.ckpt file) in the same location as the script is run from.
(requried a model created in the MNIST beginners tutorial)
2) one argument (png file location of a handwritten integer)Documentation at:
http://niektemme.com/ @@to do
"""#import modules
import sys
import tensorflow as tf
from PIL import Image,ImageFilterdef predictint(imvalue):"""This function returns the predicted integer.The imput is the pixel values from the imageprepare() function."""# Define the model (same as when creating the model file)x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x, W) + b)init_op = tf.global_variables_initializer()saver = tf.train.Saver()"""Load the model.ckpt filefile is stored in the same directory as this python script is startedUse the model to predict the integer. Integer is returend as list.Based on the documentatoin athttps://www.tensorflow.org/versions/master/how_tos/variables/index.html"""with tf.Session() as sess:sess.run(init_op)saver.restore(sess, "/tmp/model.ckpt")#print ("Model restored.")prediction=tf.argmax(y,1)return prediction.eval(feed_dict={x: [imvalue]}, session=sess)def imageprepare(argv):"""This function returns the pixel values.The imput is a png file location."""im = Image.open(argv).convert('L')width = float(im.size[0])height = float(im.size[1])newImage = Image.new('L', (28, 28), (255)) #creates white canvas of 28x28 pixelsif width > height: #check which dimension is bigger#Width is bigger. Width becomes 20 pixels.nheight = int(round((20.0/width*height),0)) #resize height according to ratio widthif (nheigth == 0): #rare case but minimum is 1 pixelnheigth = 1  # resize and sharpenimg = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)wtop = int(round(((28 - nheight)/2),0)) #caculate horizontal pozitionnewImage.paste(img, (4, wtop)) #paste resized image on white canvaselse:#Height is bigger. Heigth becomes 20 pixels. nwidth = int(round((20.0/height*width),0)) #resize width according to ratio heightif (nwidth == 0): #rare case but minimum is 1 pixelnwidth = 1# resize and sharpenimg = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)wleft = int(round(((28 - nwidth)/2),0)) #caculate vertical pozitionnewImage.paste(img, (wleft, 4)) #paste resized image on white canvas#newImage.save("sample.png")tv = list(newImage.getdata()) #get pixel values#normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.tva = [ (255-x)*1.0/255.0 for x in tv] return tva#print(tva)def main(argv):"""Main function."""imvalue = imageprepare(argv)predint = predictint(imvalue)print (predint[0]) #first value in listif __name__ == "__main__":main(sys.argv[1])

运行结果:

矩阵-线性代数-http://www2.edu-edu.com.cn/lesson_crs78/self/j_0022/soft/ch0605.html

这本书不错:超智能体https://yjango.gitbooks.io/superorganism/content/dai_ma_yan_shi_2.html

转载于:https://my.oschina.net/u/856051/blog/869692

机器学习-tensorflow相关推荐

  1. 机器学习Tensorflow基于MNIST数据集识别自己的手写数字(读取和测试自己的模型)

    机器学习Tensorflow基于MNIST数据集识别自己的手写数字(读取和测试自己的模型)

  2. 吴恩达机器学习tensorflow问题

    吴恩达机器学习tensorflow问题 module tensorflow has no attribute autograph No module named tensorflow.keras 等问 ...

  3. 机器学习 tensorflow 2 的安装

    本文介绍tensorflow 2 在windows (windows 10) 下的安装.2种安装方法,安装成功的验证过程. 方法1:直接python 网站下载的python 我本来安装有python3 ...

  4. linearregression_机器学习-TensorFlow建模过程 Linear Regression线性拟合应用

    TensorFlow是咱们机器学习领域非常常用的一个组件,它在数据处理,模型建立,模型验证等等关于机器学习方面的领域都有很好的表现,前面的一节我已经简单介绍了一下TensorFlow里面基础的数据结构 ...

  5. 机器学习Tensorflow基础知识、张量与变量

    TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组, ...

  6. 人工智能python3+tensorflow人脸识别_机器学习tensorflow object detection 实现人脸识别...

    object detection是Tensorflow很常用的api,功能强大,很有想象空间,人脸识别,花草识别,物品识别等.下面是我做实验的全过程,使用自己收集的胡歌图片,实现人脸识别,找出胡歌. ...

  7. 机器学习Tensorflow基本操作:线程队列图像

    一.线程和队列 在使用TensorFlow进行异步计算时,队列是一种强大的机制. 为了感受一下队列,让我们来看一个简单的例子.我们先创建一个"先入先出"的队列(FIFOQueue) ...

  8. 推出 TensorFlow 中文视频:机器学习从零到一,Google官方发布视频,有美女主持,美女声音甜美特别好,有B站地址,超级高清看美女费颖教你学机器学习TensorFlow,只有码农才懂的。

    关于机器学习地址 https://mp.weixin.qq.com/s/dIt3pv0UPItk4tVVeRsWDQ 美女声音甜美.大家赶紧去学习. 直接通过微信的地址打开视频是没有广告的. http ...

  9. 九、(机器学习)-Tensorflow算法之全连接层

    Tensorflow,cnn,dnn中的全连接层的理解 上一篇我们讲了使用cnn实现cifar10图像分类,模型经过隐藏层中的卷积.归一化.激活.池化之后提取到主要的特征变量,就会到全连接层,那么全连 ...

最新文章

  1. intellij idea 如何一键清除所有断点
  2. 第19课:Spark高级排序彻底解密
  3. cnn卷积中padding作用
  4. 实现视频和音频的零延迟是标准的零和博弈
  5. HashMap面试指南
  6. cv2.error: opencv(4.4.0)_【OpenCV 4开发详解】图像连通域分析
  7. 测试mysql的查询速度很慢_求助,mysql统计实时数据信息的,查询速度很慢?
  8. 使用XLinq.XElement读取带Namespace(命名空间)的XML
  9. 未来5-10年计算机视觉发展趋势,RACV2019观点集锦
  10. 计算机管理服务哪个应启动,在局域网共享服务里哪个启动项需要启动?
  11. Linux中查看bz2压缩文件大小,Linux bz2文件解压与压缩之bzip2命令
  12. windows版本redis搭建集群步骤
  13. 下采样downsample和decimate
  14. 计算机硬盘加密的原理,对硬盘加密的加密技术是什么?
  15. 【科研】ET-BERT代码分析
  16. word文档在线预览
  17. 字蛛(FontSpider,中文字体压缩器)网页自由引入中文字体
  18. OJ old1226 算法提高 质数的后代
  19. 国产服务器(麒麟操作系统),springboot应用并发访问redis数据错乱解决方案
  20. 百度地图POI数据获取

热门文章

  1. hp-ux锁定用户密码_UX设计101:用户研究-入门需要了解的一切
  2. Java 面向对象的程序设计(二)
  3. iOS开发之打包上传报错: ERROR ITMS-90087/ERROR ITMS-90125
  4. 记录工作中遇到的问题
  5. 一行代码实现底部导航栏TabLayout
  6. 思科设备snmp配置。
  7. oracle 创建view时,授权给用户
  8. JavaScript 省市级联效果
  9. 10月31日,面试题小结
  10. OK335xS psplash make-image-header.sh hacking