香港科技大学TensorFlow速成(1)

来源:互联网 发布:html5上传文件表单php 编辑:程序博客网 时间:2024/06/09 20:00

Tensor Flow:
TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。
GitHub深度学习领域最受欢迎的开源软件
1. TensorFlow™ is an open source software library for numerical computation using data flow graphs.
2. Python!
Data Flow Graph:
1. Nodes in the graph represent mathematical operations
2. Edges represent the multidimensional data arrays (tensors) communicated between them.
安装
sudo -H pip install –upgrade tensorflow
使用流程
1. Build graph (tensors) using TensorFlow operations
2. feed data and run graph (operation)
 sess.run (op)
3. update variables in the graph (and return values)
Mark
1. placeholder:feed_dict
2. Everything is Tensor: scalar, vector, matrix, 3-Tensor, n-Tensor
机器学习基础
1.线性回归

#!/usr/bin/pythonimport tensorflow as tf x_train = [1, 2, 3]y_train = [1, 2, 3]W = tf.Variable(tf.random_normal([1]), name='weight')b = tf.Variable(tf.random_normal([1]), name='bias')hypothesis = x_train * W + bcost = tf.reduce_mean(tf.square(hypothesis - y_train))optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01)train = optimizer.minimize(cost)sess  = tf.Session()sess.run(tf.global_variables_initializer())for step in range(2001):    sess.run(train)    if step % 20 == 0:        print(step, sess.run(cost), sess.run(W), sess.run(b))

2.二分类

import tensorflow as tf x_data = [[1,2],[2,3],[3,1],[4,3],[5,3],[6,2]]y_data = [[0],[0],[0],[1],[1],[1]]X = tf.placeholder(tf.float32, shape=[None,2])Y = tf.placeholder(tf.float32, shape=[None,1])W = tf.Variable(tf.random_normal([2,1]), name='weight')b = tf.Variable(tf.random_normal([1]), name='bias')hypothesis = tf.sigmoid(tf.matmul(X, W) + b)cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype = tf.float32))with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    for step in range(10001):        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})        if step % 200 == 0:            print(step, cost_val)    h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict = {X: x_data, Y: y_data})    print("\nHypothesis: ", h, "\nCorrect(Y): ", c, "\nAccuracy: ", a)

3.Softmax Classification
softmax is a generalization of logistic function that “squashes”(maps) a K-dimensional vector z of arbitrary real values to a K-dimensional vector σ(z) of real values in the range (0, 1) that add up to 1
它要解决的问题和上面的差不多,唯一的区别就是类别不局限于两类,而是多类了。要解决什么样的问题呢??假设有一训练样本集合X = {x1, x2, x3, ……},其中样本 xi 由一系列的属性表示即,xi = (a1, a2, a3,……),并且对于样本集合X中的样本属于类别C = {c1, c2, c3, ……}中的一种。现在呢,我们有一个测试样本x, 我们根椐上面的知识来推断: 样本x 属于哪种类别呢?

import tensorflow as tfx_data = [[1,2,1,1],[2,1,3,2],[3,1,3,4],[4,1,5,5],[1,7,5,5],[1,2,5,6],[1,6,6,6],[1,7,7,7]]y_data = [[0,0,1],[0,0,1],[0,0,1],[0,1,0],[0,1,0],[0,1,0],[1,0,0],[1,0,0]]X = tf.placeholder(tf.float32, shape=[None,4])Y = tf.placeholder(tf.float32, shape=[None,3])nb_classes = 3W = tf.Variable(tf.random_normal([4,nb_classes]), name='weight')b = tf.Variable(tf.random_normal([nb_classes]), name='bias')hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    for step in range(2001):        sess.run(optimizer, feed_dict={X:x_data, Y:y_data})        if step % 200 == 0:            print(step, sess.run(cost, feed_dict={X:x_data, Y:y_data}))