tensorflow
来源:互联网 发布:ps4网络 编辑:程序博客网 时间:2024/05/21 19:21
模型
编写三部曲:tensor,operator,session
使用图 (graph) 来表示计算任务,图中的节点被称为op,一个op获得0个或多个Tensor,每个Tensor是一个类型化的多维数组,使用 tensor 表示数据。
在会话 (Session) 的上下文 (context) 中执行图.
通过 变量 (Variable) 维护状态。
使用 feed 和 fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据.
第一步:构建图
构建图的第一步,是创建源op,一个op获得0个或多个Tensor,
第二步:启动Session,用Session来执行图。
在MNIST训练数据集中,mnist.train.images 是一个形状为 [60000, 784] 的张量,第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。标签0将表示成([1,0,0,0,0,0,0,0,0,0,0])。因此, mnist.train.labels 是一个 [60000, 10] 的数字矩阵。实现回归模型,如下:import tensorflow as tfx = tf.placeholder("float",[None, 784])#None表示此张量的第一个维度可以是任何长度的,输入任意数量的MNIST图像W = tf.Variable(tf.zeros([784,10]))# 一个Variable代表一个可修改的张量,用0来初始化W,b初值b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x,W) + b)#实现模型,x*W+b训练模型,利用交叉熵作为损失函数,如下y_ = tf.placeholder("float", [None,10])cross_entropy = -tf.reduce_sum(y_*tf.log(y))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)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}) 评估模型correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
tensor
它相当于N维的array或者list,某个定义好的tensor的数据类型是不变的,但是维数可以动态改变。
- 1阶张量:向量
- 2阶张量:二阶方阵
声明方法
- tf.name_scope + tf.Variable
- tf.variable_scope + tf.get_variable
- 共享变量
Session
- tf.Session.run(fetches, feed_dict)
fetches
取回tensor的输出
nput1 = tf.constant(3.0)input2 = tf.constant(2.0)input3 = tf.constant(5.0)intermed = tf.add(input2, input3)mul = tf.mul(input1, intermed)with tf.Session() as sess: result = sess.run([mul, intermed]) print result# 输出结果:# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
feed_dict
给Session输入数据,tf.placeholder(dtype, shape=None, name=None)
Inserts a placeholder for a tensor that will be always fed.
TF provides a placeholder operation that must be fed with data on execution.
input1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32)output = tf.mul(input1, input2)with tf.Session() as sess: print sess.run([output], feed_dict={input1:[7.], input2:[2.]})# 输出:# [array([ 14.], dtype=float32)]
语言模型
ptb_word_lm.py导入的rnn_cell.py位于./usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py,打印变量输出i = tf.Print(i,[i],'this is i',summarize=256)
- tensorflow
- TensorFlow
- TensorFlow
- tensorflow
- tensorflow
- tensorflow
- Tensorflow
- Tensorflow
- tensorFlow
- tensorflow
- Tensorflow
- TensorFlow
- tensorflow
- Tensorflow
- TensorFlow
- TensorFlow
- tensorflow
- TensorFlow
- (Android开发自测)在Mac OS 10.12 上安装配置appium
- apue源码使用
- 学习互联网架构第二课(脏读)
- IPC主题一:消息队列
- Android 手机屏幕一些属性
- tensorflow
- java set特点详解与set中元素比较问题
- 给linux主机添加主机路由
- 14001错误:由于应用程序配置不正确,应用程序未能启动:OD调试解决办法
- jQuery的deferred对象详解
- 如何把响应式网站的运营和优化推广做好呢?
- linux下定时执行php脚本
- java名词注释
- BZOJ 1833 数位DP 解题报告