MNIST機器學習入門

2021-08-19 16:07:57 字數 1710 閱讀 8822

路漫漫其修遠兮

環境:python + tensorflow

from __future__ import absolute_import

from __future__ import division

from __future__ import print_function

# pylint: disable=unused-import

import gzip

import os

import tempfile

import numpy

from six.moves import urllib

from six.moves import xrange # pylint: disable=redefined-builtin

import tensorflow as tf

from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

# pylint: enable=unused-import

2. softmax模型訓練

import input_data

import tensorflow as tf

#建立互動會話類

sess = tf.interactivesession()

x = tf.placeholder("float", shape = [none, 784]) #輸入資料,28*28 = 784

y_ = tf.placeholder("float", shape = [none, 10]) #標籤資料,表示類別 0~9

w = tf.variable(tf.zeros([784,10])) #weight

b = tf.variable(tf.zeros([10])) #bias

sess.run(tf.global_variables_initializer()) #初始化變數

#**模型 softmax

y = tf.nn.softmax(tf.matmul(x,w) + b)

#損失函式 交叉熵

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

#梯度下降

train_step = tf.train.gradientdescentoptimizer(0.01).minimize(cross_entropy)

#訓練1000次

for _ in range(1000):

batch = mnist_data.train.next_batch(50)

train_step.run(feed_dict = )

#評估模型

correct_prediction = tf.equal(tf.arg_max(y,1), tf.arg_max(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print(accuracy.eval(feed_dict = ))

sess.close()

手牽手 一步兩步三步四步望著天 看星星 一顆兩顆三顆四顆連成線

MNIST機器學習入門

使用mnist 資料集,用softmax回歸 標籤 使用交叉熵損失函式計算損失值 使用梯度下降法優化引數 coding utf 8 import tensorflow as tf import tensorflow.examples.tutorials.mnist.input data as inp...

MNIST機器學習入門 程式與操作

1.ubuntu terminal 2.建立乙個code目錄 mkdir tensorflow code 3.進入tensorflow code cd tensorflow code 4.建立tensorflow mnist 檔案test mnist.py touch test mnist.py 5...

TensorFlow入門 MNIST深入

1 load mnist data 2import tensorflow.examples.tutorials.mnist.input data as input data 3 mnist input data.read data sets mnist data one hot true 45 st...