tensorflow 基础定义
来源:互联网 发布:电话销售数据 编辑:程序博客网 时间:2024/06/06 18:47
作为TensorFlow的小白,还有很多东西要学的。
(1) node
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)# also tf.float32 implicitly
print(node1, node2)
To actuallyevaluate the nodes, we must run the computational graph within a session.
sess = tf.Session()print(sess.run([node1, node2]))
[3.0,4.0]
node3 = tf.add(node1, node2)print("node3: ", node3)print("sess.run(node3): ",sess.run(node3))
node3: Tensor("Add_2:0", shape=(), dtype=float32)sess.run(node3): 7.0
(2) to accept externalinputs, known as placeholders.
a = tf.placeholder(tf.float32)b = tf.placeholder(tf.float32)adder_node = a + b # + provides a shortcut for tf.add(a, b)
tospecify Tensors that provide concrete values to these placeholders:
print(sess.run(adder_node,{a:3, b:4.5}))print(sess.run(adder_node,{a:[1,3], b:[2,4]}))
输出:7.5[ 3. 7.]
add_and_triple = adder_node *3.print(sess.run(add_and_triple,{a:3, b:4.5}))
(3) Variables
W = tf.Variable([.3], tf.float32)b = tf.Variable([-.3], tf.float32)x = tf.placeholder(tf.float32)linear_model = W * x + b
Toinitialize all the variables in a TensorFlow program, you must explicitly calla special operation as follows:
init = tf.global_variables_initializer()sess.run(init)
print(sess.run(linear_model,{x:[1,2,3,4]}))
[0. 0.30000001 0.60000002 0.90000004]
(4) loss function
y = tf.placeholder(tf.float32)squared_deltas = tf.square(linear_model - y)loss = tf.reduce_sum(squared_deltas)print(sess.run(loss,{x:[1,2,3,4], y:[0,-1,-2,-3]}))
(5) improve w,b
fixW = tf.assign(W,[-1.])fixb = tf.assign(b, [1.])sess.run([fixW, fixb])print(sess.run(loss,{x:[1,2,3,4], y:[0,-1,-2,-3]}))
loss结果为:
0.0
(6)TensorFlow provides optimizers that slowly change each variable inorder to minimize the loss function. The simplest optimizer is gradientdescent.
optimizer = tf.train.GradientDescentOptimizer(0.01)train = optimizer.minimize(loss)
sess.run(init)# reset values to incorrect defaults.for i in range(1000): sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})print(sess.run([W, b]))
输出结果:
[array([-0.9999969], dtype=float32), array([0.99999082], dtype=float32)]
总结 node,placeholder
1 0
- tensorflow 基础定义
- TensorFlow基础
- Tensorflow基础
- Tensorflow基础
- Tensorflow 基础
- Tensorflow基础
- tensorflow基础
- Tensorflow基础
- TensorFlow基础
- tensorflow基础
- Tensorflow基础
- Tensorflow 损失函数定义
- tensorflow学习:定义变量
- tensorflow 变量定义路径//问题
- tensorflow基础学习笔记
- Tensorflow的基础用法
- 1、TensorFlow基础
- tensorflow入门基础
- ulimit -a详解
- 面向对象
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- Hadoop中RPC使用
- 设计模式学习之代理模式
- tensorflow 基础定义
- C++ socket 通信客户端和服务器端
- C语言中的字符串处理函数重写
- DataSetObserver使用——自定义View之一
- PDU格式短信解析
- web项目过滤器与拦截器小细节
- 剑指offer:和为S的两个数字
- Android开发线程间的交互之异步任务(AsyncTask)
- 设计模式学习之策略模式