直接给出一般的通用编程模型,并通过3个例子来看看使用情况。

#tensorflow编程模型
import tensorflow as tf# define the training loop operations
def inference(X):# compute inference model over data X and return the result return def loss(X, Y):# compute loss over training data X and expected values Yreturn def inputs():# read/generate input training data X and expected outputs Yreturn def train(total_loss):# train / adjust model parameters according to computed total lossreturn def evaluate(sess, X, Y):# evaluate the resulting trained modelreturn # Launch the graph in a session, setup boilerplate
with tf.Session() as sess:tf.global_variables_initializer().run()#tf.initialize_all_variables().run()X, Y = inputs()total_loss = loss(X, Y)train_op = train(total_loss)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)# actual training looptraining_steps = 1000for step in range(training_steps):sess.run([train_op])# for debugging and learning purposes, see how the loss gets decremented thru training stepsif step % 10 == 0:print( "loss: ", sess.run([total_loss]))evaluate(sess, X, Y)coord.request_stop()coord.join(threads)sess.close()

1. 线性回归,用来预测体重年龄与 血脂含量的关系

#Linear Regression
import tensorflow as tf# Explicitly create a Graph object
W = tf.Variable(tf.zeros([2, 1]), name="weights")
b = tf.Variable(0., name="bias")# define the training loop operations
def inference(X):# compute inference model over data X and return the result return tf.matmul(X,W) + b         #矩阵相乘def loss(X, Y):# compute loss over training data X and expected values YY_predicted = inference(X)return tf.reduce_sum(tf.squared_difference(Y,Y_predicted))def inputs():# read/generate input training data X and expected outputs Yweight_age = [[84, 46], [73, 20], [65, 52], [70, 30], [76, 57], [69, 25], [63, 28], [72, 36],[79,51],[75,50],[82,34],[59,46],[67,23],[85,37],[55,40],[63,30]]blood_fat_content = [354, 190, 405, 263, 451, 302, 288, 385, 402, 365, 209, 290, 346, 254, 395,434,220,374,308,220,311,181,274,303,244]return tf.to_float(weight_age), tf.to_float(blood_fat_content) #with GradientDescentOptimizer
def train(total_loss):learning_rate = 0.0000001return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss) def evaluate(sess, X, Y):# evaluate the resulting trained modelprint(sess.run(inference([[80., 25.]]))) # ~ 303print(sess.run(inference([[65., 25.]])))with tf.Session() as sess:tf.global_variables_initializer().run()X, Y = inputs()print(X)print(Y)total_loss = loss(X, Y)train_op = train(total_loss)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)#tf.train.start_queue_runners 这个函数将会启动输入管道的线程,填充样本到队列中,#以便出队操作可以从队列中拿到样本。这种情况下最好配合使用一个tf.train.Coordinator,这样可以在发生错误的情况下正确地关闭这些线程。#actual training looptraining_steps = 1000for step in range(training_steps):sess.run([train_op])#for debugging and learning purposes, see how the loss gets decremented thru training stepsif step % 10 == 0:print("loss: ", sess.run([total_loss]))evaluate(sess, X, Y)coord.request_stop()coord.join(threads)sess.close()

2. 使用逻辑回归LR,本地读取文件数据,然后预测泰坦尼克生存者

import tensorflow as tf# same params and variables initialization as log reg.
W = tf.Variable(tf.zeros([5, 1]), name="weights")
b = tf.Variable(0., name="bias")# former inference is now used for combining inputs
def combine_inputs(X):return tf.matmul(X, W) + b# new inferred value is the sigmoid applied to the former
def inference(X):return tf.sigmoid(combine_inputs(X))   #逻辑回归  LR#calculating cross entropy     交叉熵
def loss(X, Y):return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=combine_inputs(X), logits=Y))def read_csv(batch_size, file_name, record_defaults):#filename_queue = tf.train.string_input_producer([os.path.dirname(__file__) + "/" + file_name])filename_queue = tf.train.string_input_producer(["E:\\testData\\taitannike\\" + file_name])reader = tf.TextLineReader(skip_header_lines=1)key, value = reader.read(filename_queue)# decode_csv will convert a Tensor from type string (the text line) in# a tuple of tensor columns with the specified defaults, which also# sets the data type for each columndecoded = tf.decode_csv(value, record_defaults=record_defaults)# batch actually reads the file and loads "batch_size" rows in a single tensorreturn tf.train.shuffle_batch(decoded,batch_size=batch_size,capacity=batch_size * 50,min_after_dequeue=batch_size)def inputs():passenger_id, survived, pclass, name, sex, age, sibsp, parch, ticket, fare, cabin, embarked =read_csv(100, "train.csv", [[0.0], [0.0], [0], [""], [""], [0.0], [0.0], [0.0], [""], [0.0],[""], [""]])# convert categorical datais_first_class = tf.to_float(tf.equal(pclass, [1]))is_second_class = tf.to_float(tf.equal(pclass, [2]))is_third_class = tf.to_float(tf.equal(pclass, [3]))gender = tf.to_float(tf.equal(sex, ["female"]))# Finally we pack all the features in a single matrix;# We then transpose to have a matrix with one example per row and one feature per column.features = tf.transpose(tf.stack([is_first_class, is_second_class, is_third_class, gender, age]))survived = tf.reshape(survived, [100, 1])return features, survived
def train(total_loss):learning_rate = 0.01return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)
def evaluate(sess, X, Y):predicted = tf.cast(inference(X) > 0.5, tf.float32)print(sess.run(tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32))))
with tf.Session() as sess:tf.global_variables_initializer().run()#tf.initialize_all_variables().run()X, Y = inputs()total_loss = loss(X, Y)train_op = train(total_loss)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)# actual training looptraining_steps = 1000for step in range(training_steps):sess.run([train_op])# for debugging and learning purposes, see how the loss gets decremented thru training stepsif step % 10 == 0:print("loss: ", sess.run([total_loss]))evaluate(sess, X, Y)coord.request_stop()coord.join(threads)sess.close()

3. 使用softmax的分类,对水仙花数据分类

#softmax C-classify ,Iris-data
import tensorflow as tf# this time weights form a matrix, not a column vector, one "weight vector" per class.
W = tf.Variable(tf.zeros([4, 3]), name="weights")
# so do the biases, one per class.
b = tf.Variable(tf.zeros([3], name="bias"))# former inference is now used for combining inputs
def combine_inputs(X):return tf.matmul(X, W) + bdef inference(X):return tf.nn.softmax(combine_inputs(X))def loss(X, Y):return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=Y, logits=combine_inputs(X)))
def read_csv(batch_size, file_name, record_defaults):filename_queue = tf.train.string_input_producer(["E:\\testData\\taitannike\\" + file_name])#filename_queue = tf.train.string_input_producer([os.path.dirname(__file__) + "/" + file_name])reader = tf.TextLineReader(skip_header_lines=1)key, value = reader.read(filename_queue)# decode_csv will convert a Tensor from type string (the text line) in# a tuple of tensor columns with the specified defaults, which also# sets the data type for each columndecoded = tf.decode_csv(value, record_defaults=record_defaults)# batch actually reads the file and loads "batch_size" rows in a single tensorreturn tf.train.shuffle_batch(decoded,batch_size=batch_size,capacity=batch_size * 50,min_after_dequeue=batch_size)def inputs():sepal_length, sepal_width, petal_length, petal_width, label =\read_csv(100, "iris.data", [[0.0], [0.0], [0.0], [0.0], [""]])# convert class names to a 0 based class index.label_number = tf.to_int32(tf.argmax(tf.to_int32(tf.stack([tf.equal(label, ["Iris-setosa"]),tf.equal(label, ["Iris-versicolor"]),tf.equal(label, ["Iris-virginica"])])), 0))# Pack all the features that we care about in a single matrix;# We then transpose to have a matrix with one example per row and one feature per column.features = tf.transpose(tf.stack([sepal_length, sepal_width, petal_length, petal_width]))return features, label_numberdef train(total_loss):learning_rate = 0.01return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)def evaluate(sess, X, Y):predicted = tf.cast(tf.arg_max(inference(X), 1), tf.int32)print(sess.run(tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32))))# Launch the graph in a session, setup boilerplate
with tf.Session() as sess:tf.global_variables_initializer().run()#tf.initialize_all_variables().run()X, Y = inputs()total_loss = loss(X, Y)train_op = train(total_loss)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)# actual training looptraining_steps = 10000for step in range(training_steps):sess.run([train_op])# for debugging and learning purposes, see how the loss gets decremented thru training stepsif step % 10 == 0:print("loss: ", sess.run([total_loss]))evaluate(sess, X, Y)coord.request_stop()coord.join(threads)sess.close()

TensorFlow学习笔记(二十一) tensorflow机器学习模型相关推荐

  1. tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数

    tensorflow学习笔记二--建立一个简单的神经网络 2016-09-23 16:04 2973人阅读 评论(2) 收藏 举报  分类: tensorflow(4)  目录(?)[+] 本笔记目的 ...

  2. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0. zfill()方法语法: ...

  3. Mr.J-- jQuery学习笔记(二十一)--模拟微博页面

    先看之前的节点操作方法:Mr.J-- jQuery学习笔记(二十)--节点操作方法 Mr.J-- jQuery学习笔记(五)--属性及属性节点 Mr.J-- jQuery学习笔记(十一)--事件委托  ...

  4. TensorFlow学习笔记Day01-安装TensorFlow

    知识经济的时代,数据为王的时代,互联网的世界,什么东西都在不断的更新中,为此,我们自己也必须前行,不前行就会遭到淘汰.TensorFlow作为Google推出的便捷框架,已经受到了许多技术开发者的使用 ...

  5. TensorFlow学习笔记01:TensorFlow入门

    文章目录 一.TensorFlow基本概念 1.TensorFlow的Hello World 2.TensorFlow的概念 3.计算图&#

  6. kvm虚拟化学习笔记(二十一)之KVM性能优化学习笔记

    本学习笔记系列都是采用CentOS6.x操作系统,KVM虚拟机的管理也是采用virsh方式,网上的很多的文章都基于ubuntu高版本内核下,KVM的一些新的特性支持更好,本文只是记录了CentOS6. ...

  7. tensorflow学习笔记:查看tensorflow可配置运算资源以及配置使用GPU运算

    查看tensorflow可配置运算资源以及配置使用GPU运算 因为还用不到分布式的tensorflow,自己没有尝试过所以就不写分布式tensorflow的使用了(等自己用上了再说),这里记录一下在跑 ...

  8. TensorFlow学习笔记(十一)读取自己的数据进行训练

    1. 线性关系 数据csv文件读取 x,y 1,2 4,5 6,11 3,6 4,7 5,12 7,13 10,21 11,23 24,50 45,89 50,101 55,111 60,123 70 ...

  9. TensorFlow学习笔记之一(TensorFlow基本介绍)

    文章目录 TensorFlow计算模型---计算图 计算图的使用 TensorFlow数据模型---张量 TensorFlow运算模型---会话 使用tf.InteractiveSession在交互式 ...

  10. linux驱动开发学习笔记二十一:异步通知

    一.异步通知简介 我们首先来回顾一下"中断",中断是处理器提供的一种异步机制,我们配置好中断以后就可以让处理器去处理其他的事情了,当中断发生以后会触发我们事先设置好的中断服务函数, ...

最新文章

  1. linux中的shell有printf吗,Linux Shell系列教程之(八)Shell printf命令详解
  2. 深入理解SpringCloud之配置刷新
  3. Cpp 11 / 线程库 / 可以做为线程的执行对象有哪些?
  4. 使用 Azure PowerShell 管理 Azure 虚拟网络和 Windows 虚拟机
  5. Morse理论:拓扑不变性特征匹配原理
  6. python如何正则匹配浮点值_Python正则表达式字符串数组到浮点数组
  7. Highcharts教程--把js代码从html中抽离出来,放到单独的一个js文件中。由html页面调用...
  8. mysql主键设置after_mysql如何改变主键属性
  9. bv值是什么意思_BVR电线是什么意思BVR电线电缆规格型号
  10. 母函数——找单词(hdu2082)
  11. python itertools combination_Python itertools.combinations 和 itertools.permutations 等价代码实现...
  12. 线性查找算法(BFPRT)
  13. 自己编写的一个代码统计的小工具
  14. 服务器运维监控知识体系
  15. 人机工程学产品设计案例_【设计案例】一组电子产品设计的合辑
  16. 一封来信,你的一封来信,一封Ta的来信,爆火的匿名信H5源码功能开发和分析,表白祝福道歉短信发送系统
  17. android 跑马灯速度,自定义TextView跑马灯效果可控制启动/停止/速度/焦点
  18. 付永刚计算机信息安全技术课后答案
  19. 小米怎么和计算机连接网络设置密码,小米电视与电脑共享要用户名和密码怎么办...
  20. 输入等值线参数绘制等值线图python_ArcGIS绘图—空气质量站点数据插值绘制等值线图...

热门文章

  1. 在word中给公式添加序号
  2. 为什么delete表,还会占磁盘空间?
  3. Python程序开发——第六章 类与对象
  4. 远程图片保存到服务器 php,保存远程图片到本地服务器几种方法[php,asp]网
  5. redis key存在则删除_Redis加锁的几种实现
  6. mysql 关联查询_响应时间长?MySQL查询优化教程来了!
  7. sTC8G1K08+通过串口显示内部电压_基于51单片机的数字电流电压表
  8. Tomcat服务安装与部署(安装与优化)
  9. python顺序控制语句_Python学习之 流程控制语句
  10. 用c语言编辑一个通讯录,C语言实现一个通讯录