tensorflow逻辑回归框架

- 日理万妓 2023-07-11 08:49 74阅读 0赞

小彩笔的摸爬滚打之路orz

  1. import numpy as np
  2. import tensorflow as tf
  3. import matplotlib.pyplot as plt
  4. import input_data
  5. mnist = input_data.read_data_sets('data/', one_hot=True)
  6. trainimg = mnist.train.images
  7. trainlabel = mnist.train.labels
  8. testimg = mnist.test.images
  9. testlabel = mnist.test.labels
  10. print ("MNIST loaded")
  11. print (trainimg.shape)
  12. print (trainlabel.shape)
  13. print (testimg.shape)
  14. print (testlabel.shape)
  15. #print (trainimg)
  16. print (trainlabel[0])

在这里插入图片描述

  1. x = tf.placeholder("float", [None, 784])
  2. y = tf.placeholder("float", [None, 10]) # None is for infinite
  3. W = tf.Variable(tf.zeros([784, 10]))
  4. b = tf.Variable(tf.zeros([10]))
  5. # LOGISTIC REGRESSION MODEL
  6. actv = tf.nn.softmax(tf.matmul(x, W) + b)
  7. # COST FUNCTION
  8. cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
  9. # OPTIMIZER
  10. learning_rate = 0.01
  11. optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
  12. # PREDICTION
  13. pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
  14. # ACCURACY
  15. accr = tf.reduce_mean(tf.cast(pred, "float"))
  16. # INITIALIZER
  17. init = tf.global_variables_initializer()
  18. # argmax函数讲解
  19. sess = tf.InteractiveSession()
  20. arr = np.array([[31, 23, 4, 24, 27, 34],
  21. [18, 3, 25, 0, 6, 35],
  22. [28, 14, 33, 22, 20, 8],
  23. [13, 30, 21, 19, 7, 9],
  24. [16, 1, 26, 32, 2, 29],
  25. [17, 12, 5, 11, 10, 15]])
  26. #tf.rank(arr).eval() 矩阵维度
  27. #tf.shape(arr).eval()
  28. #tf.argmax(arr, 0).eval() 求最大值索引,0对应每一列,1对应每一行,返回的都是所在的列号
  29. # 0 -> 31 (arr[0, 0])
  30. # 3 -> 30 (arr[3, 1])
  31. # 2 -> 33 (arr[2, 2])
  32. tf.argmax(arr, 1).eval()
  33. # 5 -> 34 (arr[0, 5])
  34. # 5 -> 35 (arr[1, 5])
  35. # 2 -> 33 (arr[2, 2])

在这里插入图片描述

  1. training_epochs = 50
  2. batch_size = 100
  3. display_step = 5
  4. # SESSION
  5. sess = tf.Session()
  6. sess.run(init)
  7. # MINI-BATCH LEARNING
  8. for epoch in range(training_epochs):
  9. avg_cost = 0.
  10. num_batch = int(mnist.train.num_examples/batch_size)
  11. for i in range(num_batch):
  12. batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  13. sess.run(optm, feed_dict={ x: batch_xs, y: batch_ys})
  14. feeds = { x: batch_xs, y: batch_ys}
  15. avg_cost += sess.run(cost, feed_dict=feeds)/num_batch
  16. # DISPLAY
  17. if epoch % display_step == 0:
  18. feeds_train = { x: batch_xs, y: batch_ys}
  19. feeds_test = { x: mnist.test.images, y: mnist.test.labels}
  20. train_acc = sess.run(accr, feed_dict=feeds_train)
  21. test_acc = sess.run(accr, feed_dict=feeds_test)
  22. print ("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f"
  23. % (epoch, training_epochs, avg_cost, train_acc, test_acc))
  24. print ("DONE")

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,74人围观)

还没有评论,来说两句吧...

相关阅读

    相关 逻辑回归

    逻辑回归 内容分三块,第一部分逻辑回归模型,第二部分是策略,第三部分是学习算法。 1. 构造分类函数 > Logistic回归虽然名字里带“回归”,但是它实际上是

    相关 逻辑回归

    继续机器学习系列基础算法,逻辑回归 定义问题 首先我们依然是定义问题,逻辑回归是解决分类问题,而且是基本的二分类问题,比如经典的垃圾邮件判定,根据疾病的特征预测死亡率。

    相关 逻辑回归

    逻辑回归 逻辑回归和线性回归很像,差别在于逻辑回归在线性回归的基础上加了一个激活函数。逻辑回归所使用的激活函数也分两种,一个是sigmoid函数,一个是softmax函数,

    相关 逻辑回归

    1.定义 逻辑回归属于一种分类算法,因变量为分类变量。 2.假设函数 通常令假设函数(此处为一个非线性假设)为(无非就是套在了激励函数里面了): ![88