資源簡介
1.TensorFlow安裝、技巧、教材
2.TensorFlow官方文檔
3.TensorFlow圖像識別應用
4.深度學習基礎教程
代碼片段和文件信息
#?Copyright?2015?The?TensorFlow?Authors.?All?Rights?Reserved.
#
#?Licensed?under?the?Apache?License?Version?2.0?(the?“License“);
#?you?may?not?use?this?file?except?in?compliance?with?the?License.
#?You?may?obtain?a?copy?of?the?License?at
#
#?????http://www.apache.org/licenses/LICENSE-2.0
#
#?Unless?required?by?applicable?law?or?agreed?to?in?writing?software
#?distributed?under?the?License?is?distributed?on?an?“AS?IS“?BASIS
#?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND?either?express?or?implied.
#?See?the?License?for?the?specific?language?governing?permissions?and
#?limitations?under?the?License.
#?==============================================================================
“““Simple?end-to-end?LeNet-5-like?convolutional?MNIST?model?example.
This?should?achieve?a?test?error?of?0.7%.?Please?keep?this?model?as?simple?and
linear?as?possible?it?is?meant?as?a?tutorial?for?simple?convolutional?models.
Run?with?--self_test?on?the?command?line?to?execute?a?short?self-test.
“““
from?__future__?import?absolute_import
from?__future__?import?division
from?__future__?import?print_function
import?gzip
import?os
import?sys
import?time
import?numpy
from?six.moves?import?urllib
from?six.moves?import?xrange??#?pylint:?disable=redefined-builtin
import?tensorflow?as?tf
SOURCE_URL?=?‘http://yann.lecun.com/exdb/mnist/‘
WORK_DIRECTORY?=?‘data‘
IMAGE_SIZE?=?28
NUM_CHANNELS?=?1
PIXEL_DEPTH?=?255
NUM_LABELS?=?10
VALIDATION_SIZE?=?5000??#?Size?of?the?validation?set.
SEED?=?66478??#?Set?to?None?for?random?seed.
BATCH_SIZE?=?64
NUM_EPOCHS?=?10
EVAL_BATCH_SIZE?=?64
EVAL_FREQUENCY?=?100??#?Number?of?steps?between?evaluations.
tf.app.flags.DEFINE_boolean(“self_test“?False?“True?if?running?a?self?test.“)
tf.app.flags.DEFINE_boolean(‘use_fp16‘?False
????????????????????????????“Use?half?floats?instead?of?full?floats?if?True.“)
FLAGS?=?tf.app.flags.FLAGS
def?data_type():
??“““Return?the?type?of?the?activations?weights?and?placeholder?variables.“““
??if?FLAGS.use_fp16:
????return?tf.float16
??else:
????return?tf.float32
def?maybe_download(filename):
??“““Download?the?data?from?Yann‘s?website?unless?it‘s?already?here.“““
??if?not?tf.gfile.Exists(WORK_DIRECTORY):
????tf.gfile.MakeDirs(WORK_DIRECTORY)
??filepath?=?os.path.join(WORK_DIRECTORY?filename)
??if?not?tf.gfile.Exists(filepath):
????filepath?_?=?urllib.request.urlretrieve(SOURCE_URL?+?filename?filepath)
????with?tf.gfile.GFile(filepath)?as?f:
??????size?=?f.size()
????print(‘Successfully?downloaded‘?filename?size?‘bytes.‘)
??return?filepath
def?extract_data(filename?num_images):
??“““Extract?the?images?into?a?4D?tensor?[image?index?y?x?channels].
??Values?are?rescaled?from?[0?255]?down?to?[-0.5?0.5].
??“““
??print(‘Extracting‘?filename)
??with?gzip.open(filename)?as?bytestream:
????bytestream.read(16)
????buf?=?bytestream.read(IMAGE_SIZE?*?IMAGE_SIZE?*?num_images?*?NUM_CHANNELS)
????data?=?numpy.frombuffer(buf?dtype=numpy.uint8).astype(numpy.float32)
????data?=?(data?-?(PIXEL_DEPTH?/
評論
共有 條評論