MNIST 資料 分類

2022-09-01 18:12:14 字數 2081 閱讀 2000

分類和回歸的區別在於輸出變數的型別上。 通俗理解定量輸出是回歸,或者說是連續變數**; 定性輸出是分類,

或者說是離散變數**。如**房價這是乙個回歸任務; 把東西分成幾類, 比如貓狗豬牛,就是乙個分類任務。

1

import

tensorflow as tf

2import

numpy as np

3from tensorflow.examples.tutorials.mnist import

input_data #首先準備資料

4 mnist =input_data.read_data_sets('

mnist_data

',one_hot=true)

#呼叫add_layer函式搭建乙個最簡單的訓練網路結構,只有輸入層和輸出層。

5def add_layer(inputs, in_size, out_size,activation_function=none):

6 weights =tf.variable(tf.random_normal([in_size, out_size]))

7 biases = tf.variable(tf.zeros([1, out_size]) + 0.1)

8 wx_plus_b = tf.matmul(inputs, weights) +biases

9if activation_function is

none:

10 outputs =wx_plus_b

11else

:12 outputs =activation_function(wx_plus_b)

13return

outputs

14def

compute_accuracy(v_xs, v_ys):

15global

prediction

16 y_pre = sess.run(prediction, feed_dict=)

17 correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))

18 accuracy =tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

19 result = sess.run(accuracy, feed_dict=)

20return

result

21 #其中輸入資料是784個特徵,輸出資料是10個特徵,激勵採用softmax函式

22 xs=tf.placeholder(tf.float32,[none,784]) #

28*28

23 ys=tf.placeholder(tf.float32,[none,10])

24 prediction=add_layer(xs,784,10,activation_function=tf.nn.softmax)25#

loss

26 cross_entropy=tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))

2728 train_step=tf.train.gradientdescentoptimizer(0.5).minimize(cross_entropy)

2930 init =tf.global_variables_initializer()

31 sess=tf.session()

32sess.run(init)

33for i in range(1000):

34 batch_xs,batch_ys=mnist.train.next_batch(100)

35 sess.run(train_step,feed_dict=)

36if i%50==0:

37print

(compute_accuracy(

38 mnist.test.images,mnist.test.labels))

3 3實現MNIST資料集分類

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data 載入資料集 mnist input data.read data sets mnist data one hot true 定義每個批...

Mnist資料集分類簡單版本

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data 載入資料集 mnist input data.read data sets mnist data one hot true 每個批次的...

MNIST資料集分類簡單版本

from tensorflow.examples.tutorials.mnist import input data 載入資料集mnist input data.read data sets data stu05 mnist data one hot true extracting data stu...