【机器学习】Tensorflow学习笔记

Niki23T 8年前

来自: http://blog.csdn.net//chenriwei2/article/details/50615769


构建网络模型

基本的MLP网络结构

基本的感知机模型,没有加入b

模型:

Y=W(WX)

import tensorflow as tf  import numpy as np  import input_data    # 初始化权重 w  def init_weights(shape):      return tf.Variable(tf.random_normal(shape, stddev=0.01))    # 定义网络模型,只是基本的mlp模型,堆叠两层的逻辑回归  def model(X, w_h, w_o):      h = tf.nn.sigmoid(tf.matmul(X, w_h))       return tf.matmul(h, w_o) #这里没有用softmax    # 加载数据  mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels    # 定义占位符  X = tf.placeholder("float", [None, 784])  Y = tf.placeholder("float", [None, 10])    # 初始化模型参数  w_h = init_weights([784, 625])   w_o = init_weights([625, 10])    # 定义模型  py_x = model(X, w_h, w_o)    # 定义损失函数  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))   # 定义训练操作  train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer  # 定义测试操作  predict_op = tf.argmax(py_x, 1)    # 定义并初始化会话  sess = tf.Session()  init = tf.initialize_all_variables()  sess.run(init)    # 训练测试  for i in range(100):      for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):          sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})      print i, np.mean(np.argmax(teY, axis=1) ==                       sess.run(predict_op, feed_dict={X: teX, Y: teY}))

构建多层网络

模型:
多层(3层模型)

import tensorflow as tf  import numpy as np  import input_data    # 初始化权重  def init_weights(shape):      return tf.Variable(tf.random_normal(shape, stddev=0.01))    # 定义模型,2层的隐藏层+ 3层的dropout  def model(X, w_h, w_h2, w_o, p_drop_input, p_drop_hidden):       X = tf.nn.dropout(X, p_drop_input) # 输入就开始用dropout      h = tf.nn.relu(tf.matmul(X, w_h))        h = tf.nn.dropout(h, p_drop_hidden) # dropout      h2 = tf.nn.relu(tf.matmul(h, w_h2))        h2 = tf.nn.dropout(h2, p_drop_hidden) # dropout        return tf.matmul(h2, w_o)    # 加载数据  mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels    # 定义占位符+ 初始化变量  X = tf.placeholder("float", [None, 784])  Y = tf.placeholder("float", [None, 10])    w_h = init_weights([784, 625])  w_h2 = init_weights([625, 625])  w_o = init_weights([625, 10])    # dropout 的概率  p_keep_input = tf.placeholder("float")  p_keep_hidden = tf.placeholder("float")    # 模型  py_x = model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden)    # 损失函数  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))  train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)  predict_op = tf.argmax(py_x, 1)    sess = tf.Session()  init = tf.initialize_all_variables()  sess.run(init)    for i in range(100):      for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):          sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],                                        p_keep_input: 0.8, p_keep_hidden: 0.5})      print i, np.mean(np.argmax(teY, axis=1) ==                       sess.run(predict_op, feed_dict={X: teX, Y: teY,                                                       p_keep_input: 1.0,                                                       p_keep_hidden: 1.0}))

卷积神经网络

模型:

import tensorflow as tf  import numpy as np  import input_data      def init_weights(shape):      return tf.Variable(tf.random_normal(shape, stddev=0.01))    # 定义卷积神经网络模型  def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):      l1a = tf.nn.relu(tf.nn.conv2d(X, w, [1, 1, 1, 1], 'SAME'))      l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],                          strides=[1, 2, 2, 1], padding='SAME')      l1 = tf.nn.dropout(l1, p_keep_conv)        l2a = tf.nn.relu(tf.nn.conv2d(l1, w2, [1, 1, 1, 1], 'SAME'))      l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],                          strides=[1, 2, 2, 1], padding='SAME')      l2 = tf.nn.dropout(l2, p_keep_conv)        l3a = tf.nn.relu(tf.nn.conv2d(l2, w3, [1, 1, 1, 1], 'SAME'))      l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],                          strides=[1, 2, 2, 1], padding='SAME')      l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])      l3 = tf.nn.dropout(l3, p_keep_conv)        l4 = tf.nn.relu(tf.matmul(l3, w4))      l4 = tf.nn.dropout(l4, p_keep_hidden)        pyx = tf.matmul(l4, w_o)      return pyx    # 加载数据  mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels  trX = trX.reshape(-1, 28, 28, 1)  teX = teX.reshape(-1, 28, 28, 1)    X = tf.placeholder("float", [None, 28, 28, 1])  Y = tf.placeholder("float", [None, 10])    w = init_weights([3, 3, 1, 32])  w2 = init_weights([3, 3, 32, 64])  w3 = init_weights([3, 3, 64, 128])  w4 = init_weights([128 * 4 * 4, 625])  w_o = init_weights([625, 10])    p_keep_conv = tf.placeholder("float")  p_keep_hidden = tf.placeholder("float")  py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)    # 损失函数  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))  # 训练操作  train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)  # 测试操作  predict_op = tf.argmax(py_x, 1)    sess = tf.Session()  init = tf.initialize_all_variables()  sess.run(init)    for i in range(100):      for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):          sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],                                        p_keep_conv: 0.8, p_keep_hidden: 0.5})        test_indices = np.arange(len(teX)) # Get A Test Batch      np.random.shuffle(test_indices)      test_indices = test_indices[0:256]        print i, np.mean(np.argmax(teY[test_indices], axis=1) ==                       sess.run(predict_op, feed_dict={X: teX[test_indices],                                                       Y: teY[test_indices],                                                       p_keep_conv: 1.0,                                                       p_keep_hidden: 1.0}))
</div>