软培
機(jī)器學(xué)習(xí)的概念
有 監(jiān) 督 學(xué) 習(xí)(Supervised Learning):
\[x, y\]
輸入高維向量(Vector)并同時(shí)輸入其標(biāo)簽(Label),通過(guò)建模(Modeling),使得機(jī)器能夠自動(dòng)在模型中計(jì)算最為合適的參數(shù)值(Parameter)。最終,使得我們獲得一個(gè)參數(shù)確定的模型,并在未來(lái)輸入新的高維向量時(shí)可以預(yù)測(cè)其Label值,使得Label的誤差在所允許的范圍之內(nèi)。
無(wú) 監(jiān) 督 學(xué) 習(xí)(Non-Supervised Learning):
\[x\]
輸入高維向量(Vector)并,通過(guò)建模(Modeling),使得機(jī)器能夠計(jì)算出模型中相應(yīng)的待定參數(shù)值(Parameter)。
典型的監(jiān)督學(xué)習(xí)
貝葉斯概率
概率是一種事情發(fā)生可能性的描述.
1.1 重要原則:
- 結(jié)論來(lái)源于觀測(cè)
- 定量描述優(yōu)于定性描述
- 結(jié)論條件明確,敘述簡(jiǎn)潔,有好的泛化性
- 高度自洽性,高度可驗(yàn)證性
1.2 古典概型(靜態(tài)概型)
- 試驗(yàn)只有有限個(gè)基本結(jié)果
- 試驗(yàn)的每個(gè)基本結(jié)果出現(xiàn)的可能性是一樣的
1.3 基于統(tǒng)計(jì)的概率
- 在一定條件下,重復(fù)做\(n\)次試驗(yàn)(對(duì)一個(gè)事情觀測(cè)\(n\)次),一個(gè)事件\(A\)發(fā)生的概率\(P(A)\)
\[P(A) = \frac{A發(fā)生次數(shù)}{總觀測(cè)次數(shù)}\]
- \(A與B正相關(guān), P(A)<P(A|B)\)
- \(A與B負(fù)相關(guān), P(A)>P(A|B)\)
樸素貝葉斯分類(lèi)器
維基百科:所有樸素貝葉斯分類(lèi)器都假定樣本每個(gè)特征與其他特征都不相關(guān)。
一個(gè)普通的規(guī)則就是選出最有可能的那個(gè):這就是大家熟知的最大后驗(yàn)概率(MAP)決策準(zhǔn)則。
\[classify\{f_1,\dots,f_n\} = \underset{c}{\operatorname{argmax}} \ p(C=c) \displaystyle\prod_{i=1}^n p(F_i=f_i\vert C=c)\]
高斯樸素貝葉斯
如果要處理的是連續(xù)數(shù)據(jù)一種通常的假設(shè)是這些連續(xù)數(shù)值為高斯分布。
例如,假設(shè)訓(xùn)練集中有一個(gè)連續(xù)屬性,\(x\)。我們首先對(duì)數(shù)據(jù)根據(jù)類(lèi)別分類(lèi),然后計(jì)算每個(gè)類(lèi)別中\(x\)的均值和方差。令\(\mu_c\)表示為\(x\)在''c''類(lèi)上的均值,令\(\sigma^2_c\)為 \(x\)在''c''類(lèi)上的方差。在給定類(lèi)中某個(gè)值的概率,\(P(x=v|c)\),可以通過(guò)將\(v\)表示為均值為\(\mu_c\)方差為\(\sigma^2_c\)正態(tài)分布計(jì)算出來(lái)。如下,
\[ P(x=v|c)=\tfrac{1}{\sqrt{2\pi\sigma^2_c}}\,e^{ -\frac{(v-\mu_c)^2}{2\sigma^2_c} } \]
處理連續(xù)數(shù)值問(wèn)題的另一種常用的技術(shù)是通過(guò)離散化連續(xù)數(shù)值的方法。通常,當(dāng)訓(xùn)練樣本數(shù)量較少或者是精確的分布已知時(shí),通過(guò)概率分布的方法是一種更好的選擇。在大量樣本的情形下離散化的方法表現(xiàn)更優(yōu),因?yàn)榇罅康臉颖究梢詫W(xué)習(xí)到數(shù)據(jù)的分布。由于樸素貝葉斯是一種典型的用到大量樣本的方法(越大計(jì)算量的模型可以產(chǎn)生越高的分類(lèi)精確度),所以樸素貝葉斯方法都用到離散化方法,而不是概率分布估計(jì)的方法。
hello.py
import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) a = tf.constant(10) b = tf.constant(32) print(sess.run(a + b)) b'Hello, TensorFlow!' 42實(shí)驗(yàn)指導(dǎo):
實(shí)現(xiàn)一個(gè)線性回歸的例子
# ex-01.py import tensorflow as tf import numpy as np#create some training data x_data = np.random.rand(100).astype(np.float32) y_data = x_data*1 + 3print ("x_data:") print (x_data)print ("y_data:") print( y_data)#create the weights and bias variables weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) print ("weights before initializing:") print (weights)biases = tf.Variable(tf.zeros([1])) print ("bias before initializing:") print (biases)#predict (fit) value y = weights*x_data + biases#loss function loss = tf.reduce_mean(tf.square(y - y_data))#optimizer definition optimizer = tf.train.GradientDescentOptimizer(0.1)#train definition train = optimizer.minimize(loss)#initialiing the variables init = tf.initialize_all_variables()#session definition and active sess = tf.Session() sess.run(init)#train the model for step in range(501):sess.run(train)if step % 10 == 0:print (step,sess.run(weights),sess.run(biases)) x_data: [ 0.89402199 0.10053575 0.55701882 0.53727293 0.36683112 0.442012130.42864946 0.33498201 0.55696607 0.40338814 0.99002725 0.233939480.40767717 0.20491761 0.10732751 0.64065552 0.95823038 0.369610490.43446437 0.05964461 0.39571118 0.36884421 0.92716092 0.859387990.61906868 0.28850925 0.33652243 0.34363723 0.19535853 0.122284320.87395531 0.94348276 0.58827281 0.42699504 0.68471229 0.491384240.60810924 0.59445798 0.48714778 0.84556079 0.71666372 0.730189740.45137393 0.89610088 0.23163974 0.57911086 0.59919071 0.146428320.16170129 0.93183321 0.03960106 0.02113333 0.26829427 0.054383320.26300624 0.25510544 0.47461808 0.42821345 0.13820758 0.617614450.95918888 0.57477629 0.80746269 0.9087519 0.29300612 0.22989190.47188538 0.76722085 0.5297811 0.20937359 0.86967862 0.110506950.41093281 0.43827012 0.63228905 0.29376149 0.35848793 0.443831120.11717488 0.82729691 0.06545471 0.2652818 0.00641522 0.543565930.69357717 0.9950943 0.72574335 0.77202976 0.6673649 0.336639970.93204492 0.86043173 0.88384038 0.27974007 0.33629051 0.649073240.77879322 0.63549143 0.0160666 0.56776083] y_data: [ 3.89402199 3.10053587 3.55701876 3.53727293 3.36683106 3.442012073.42864943 3.33498192 3.55696607 3.40338802 3.99002719 3.233939413.40767717 3.20491767 3.10732746 3.64065552 3.9582305 3.369610553.43446445 3.0596447 3.39571118 3.36884427 3.92716098 3.859387873.61906862 3.28850937 3.33652234 3.34363723 3.19535851 3.122284413.87395525 3.94348288 3.58827281 3.42699504 3.68471241 3.491384273.60810924 3.5944581 3.48714781 3.84556079 3.71666384 3.73018983.45137405 3.896101 3.23163986 3.57911086 3.59919071 3.146428353.1617012 3.93183327 3.03960109 3.02113342 3.26829433 3.054383283.26300621 3.2551055 3.47461796 3.42821336 3.13820767 3.617614513.95918894 3.57477617 3.80746269 3.90875196 3.29300618 3.229891783.47188544 3.76722097 3.5297811 3.20937347 3.8696785 3.110507013.41093278 3.43827009 3.63228893 3.29376149 3.35848784 3.443831213.11717486 3.82729697 3.06545472 3.26528168 3.00641513 3.543565993.69357729 3.9950943 3.72574329 3.77202988 3.66736484 3.336639883.93204498 3.86043167 3.88384032 3.2797401 3.3362906 3.649073123.77879333 3.63549137 3.01606655 3.56776094] weights before initializing: <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> bias before initializing: <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 0 [ 0.90101707] [ 0.64157164] 10 [ 1.73624611] [ 2.48117089] 20 [ 1.6977222] [ 2.62674093] 30 [ 1.62073231] [ 2.67402244] 40 [ 1.55015469] [ 2.7114203] 50 [ 1.48748767] [ 2.74431014] 60 [ 1.43195248] [ 2.77343965] 70 [ 1.3827436] [ 2.79925013] 80 [ 1.33914053] [ 2.82212019] 90 [ 1.30050492] [ 2.84238434] 100 [ 1.266271] [ 2.86034036] 110 [ 1.23593676] [ 2.87625074] 120 [ 1.2090584] [ 2.8903482] 130 [ 1.18524206] [ 2.9028399] 140 [ 1.16413891] [ 2.91390872] 150 [ 1.14543998] [ 2.92371631] 160 [ 1.12887108] [ 2.93240666] 170 [ 1.11418998] [ 2.94010711] 180 [ 1.10118115] [ 2.94693041] 190 [ 1.08965421] [ 2.95297623] 200 [ 1.07944047] [ 2.95833325] 210 [ 1.07039058] [ 2.96307993] 220 [ 1.06237149] [ 2.96728611] 230 [ 1.0552659] [ 2.97101283] 240 [ 1.04896998] [ 2.97431517] 250 [ 1.04339111] [ 2.97724128] 260 [ 1.03844786] [ 2.97983384] 270 [ 1.03406775] [ 2.98213148] 280 [ 1.03018677] [ 2.9841671] 290 [ 1.0267477] [ 2.98597097] 300 [ 1.02370059] [ 2.98756886] 310 [ 1.02100062] [ 2.98898506] 320 [ 1.01860821] [ 2.99023986] 330 [ 1.01648819] [ 2.99135184] 340 [ 1.01460981] [ 2.99233723] 350 [ 1.01294541] [ 2.99321008] 360 [ 1.01147056] [ 2.99398375] 370 [ 1.01016378] [ 2.99466896] 380 [ 1.00900602] [ 2.99527621] 390 [ 1.00797999] [ 2.99581456] 400 [ 1.00707078] [ 2.9962914] 410 [ 1.00626528] [ 2.99671388] 420 [ 1.00555134] [ 2.99708843] 430 [ 1.00491893] [ 2.99742007] 440 [ 1.00435853] [ 2.99771404] 450 [ 1.0038619] [ 2.9979744] 460 [ 1.00342214] [ 2.99820518] 470 [ 1.00303233] [ 2.99840927] 480 [ 1.00268698] [ 2.99859095] 490 [ 1.00238085] [ 2.9987514] 500 [ 1.00210965] [ 2.9988935]實(shí)現(xiàn)一個(gè)矩陣乘法計(jì)算
# ex-02.py import tensorflow as tfmatrix1 = tf.constant([[3,3]])matrix2 = tf.constant([[1],[2]])#matrix multiply product = tf.matmul(matrix1, matrix2)#method1 #sess = tf.Session() #result = sess.run(product) #print "result:",result #sess.close()#method2 with tf.Session() as sess:result2 = sess.run(product)print ("result2:",result2) result2: [[9]]常量/變量/賦值
# ex-03.py import tensorflow as tfstate = tf.Variable(10, name='counter')print( state.name)one = tf.constant(1)new_value = tf.add(state, one)update = tf.assign(state, new_value)#this initializing is very important init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)for x in range(10):sess.run(update)# must do sess.run to operate the variablesprint(sess.run(state)) counter_1:0 11 12 13 14 15 16 17 18 19 20Placeholder用法
# ex-04.py import tensorflow as tfinput1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32)output = input1 * input2with tf.Session() as sess:print(sess.run(output, feed_dict={input1: 4, input2: 2}))print(sess.run(output, feed_dict={input1: [4,2], input2: [2,7]})) 8.0 [ 8. 14.] # ex-05.py import tensorflow as tf import numpy as npdef add_layer(inputs, in_size, out_size, activation_function=None):weights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1,out_size]))wx_b = tf.matmul(inputs, weights) + biasesif activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b)return outputs#make some input value x_data = np.linspace(-1, 1, 300)[:, np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) + noisexs = tf.placeholder(tf.float32, [None,1]) ys = tf.placeholder(tf.float32, [None,1])layer1 = add_layer(xs, 1, 10, activation_function=tf.nn.sigmoid) prediction = add_layer(layer1, 10, 1, activation_function=None)loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)init = tf.global_variables_initializer()sess = tf.Session() sess.run(init)for x in range(1000):sess.run(train, feed_dict = { xs: x_data, ys: y_data})if x % 10==0:print (sess.run(loss, feed_dict={xs:x_data, ys: y_data})) 1.84078 0.633829 0.382142 0.309708 0.274133 0.248449 0.227182 0.208942 0.19316 0.179466 0.167569 0.157221 0.148213 0.140364 0.13352 0.127546 0.122329 0.117768 0.113778 0.110283 0.107221 0.104534 0.102175 0.100101 0.0982751 0.0966661 0.095246 0.0939907 0.0928792 0.0918932 0.0910167 0.0902358 0.0895383 0.0889138 0.0883529 0.0878477 0.087391 0.0869768 0.0865996 0.0862548 0.0859384 0.0856467 0.0853765 0.0851253 0.0848907 0.0846704 0.0844628 0.0842661 0.0840792 0.0839006 0.0837294 0.0835647 0.0834056 0.0832514 0.0831015 0.0829553 0.0828125 0.0826725 0.082535 0.0823997 0.0822664 0.0821347 0.0820044 0.0818755 0.0817476 0.0816208 0.0814948 0.0813695 0.0812449 0.0811209 0.0809973 0.0808742 0.0807514 0.080629 0.0805069 0.080385 0.0802633 0.0801419 0.0800206 0.0798994 0.0797783 0.0796574 0.0795365 0.0794157 0.079295 0.0791743 0.0790537 0.0789332 0.0788126 0.0786921 0.0785716 0.0784511 0.0783306 0.0782102 0.0780897 0.0779693 0.0778488 0.0777284 0.0776079 0.0774875可視化
tensorboard --logdir='logs/'
注意:不能在notebook直接運(yùn)行。
ex-06-vis.py
import time import tensorflow as tf import numpy as np import matplotlib.pyplot as pltdef add_layer(inputs, in_size, out_size, activation_function=None):weights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1,out_size]))wx_b = tf.matmul(inputs, weights) + biasesif activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b)return outputs#make some input value x_data = np.linspace(-1, 1, 300)[:, np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) + noisexs = tf.placeholder(tf.float32, [None,1]) ys = tf.placeholder(tf.float32, [None,1])layer1 = add_layer(xs, 1, 10, activation_function=tf.nn.sigmoid) prediction = add_layer(layer1, 10, 1, activation_function=None)loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)init = tf.global_variables_initializer()sess = tf.Session() sess.run(init)#added coding fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(x_data, y_data) plt.ion() plt.show()for x in range(1000):sess.run(train, feed_dict = { xs: x_data, ys: y_data})if x % 10==0:# print (sess.run(loss, feed_dict={xs:x_data, ys: y_data}))try:ax.lines.remove(lines[0])except Exception:passprediction_value = sess.run(prediction, feed_dict={xs:x_data})lines = ax.plot(x_data, prediction_value, 'r-', lw=5)plt.pause(0.1) import time for i in [1,2,34,5]:print(i)time.sleep(0.1) 1 2 34 5ex-09-tb.py
# from ex-05 import tensorflow as tf import numpy as npdef add_layer(inputs, in_size, out_size, n_layer, activation_function=None):layer_name = 'layer%s' % n_layerwith tf.name_scope('layer'):with tf.name_scope('weights'):weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')tf.summary.histogram(layer_name + '/weights', weights)with tf.name_scope('bias'):biases = tf.Variable(tf.zeros([1,out_size]), name='b')tf.summary.histogram(layer_name + '/biases', biases)with tf.name_scope('wx_plus_b'):wx_b = tf.add(tf.matmul(inputs, weights), biases)if activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b, name='output')tf.summary.histogram(layer_name + '/outputs', outputs)return outputs#make some input value x_data = np.linspace(-1, 1, 300)[:, np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) + noisewith tf.name_scope('inputs'):xs = tf.placeholder(tf.float32, [None,1], name='x_input')ys = tf.placeholder(tf.float32, [None,1], name='y_input')layer1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.sigmoid) prediction = add_layer(layer1, 10, 1, n_layer=2, activation_function=None)with tf.name_scope('loss'):loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))tf.summary.scalar('loss', loss)with tf.name_scope('train'):train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)init = tf.global_variables_initializer()sess = tf.Session() sess.run(init)merged = tf.summary.merge_all()writer = tf.summary.FileWriter("logs/", sess.graph)for x in range(50000):sess.run(train, feed_dict = { xs: x_data, ys: y_data})if x % 50==0:sess.run(loss, feed_dict={xs:x_data, ys:y_data})result = sess.run(merged, feed_dict={xs:x_data, ys:y_data})writer.add_summary(result, x) import tensorflow as tfa = tf.constant(5, name="input_a") b = tf.constant(3, name="input_b") c = tf.multiply(a, b, name="mul_c") d = tf.add(a, b, name="add_d") e = tf.add(c, d, name="add_e")sess = tf.Session() sess.run(e)writer = tf.summary.FileWriter("E:/tensorflow/graph", tf.get_default_graph()) writer.close()ex-10.py
import tensorflow as tf import gzip import numpy import collections from tensorflow.python.framework import random_seed from tensorflow.python.framework import dtypes #from tensorflow.examples.tutorials.mnist import input_dataDatasets = collections.namedtuple('Datasets', ['train', 'validation', 'test'])def _read32(bytestream):dt = numpy.dtype(numpy.uint32).newbyteorder('>')return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]def dense_to_one_hot(labels_dense, num_classes):"""Convert class labels from scalars to one-hot vectors."""num_labels = labels_dense.shape[0]index_offset = numpy.arange(num_labels) * num_classeslabels_one_hot = numpy.zeros((num_labels, num_classes))labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1return labels_one_hotdef extract_images(f):"""Extract the images into a 4D uint8 numpy array [index, y, x, depth].Args:f: A file object that can be passed into a gzip reader.Returns:data: A 4D uint8 numpy array [index, y, x, depth].Raises:ValueError: If the bytestream does not start with 2051."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2051:raise ValueError('Invalid magic number %d in MNIST image file: %s' %(magic, f.name))num_images = _read32(bytestream)rows = _read32(bytestream)cols = _read32(bytestream)buf = bytestream.read(rows * cols * num_images)data = numpy.frombuffer(buf, dtype=numpy.uint8)data = data.reshape(num_images, rows, cols, 1)return datadef extract_labels(f, one_hot=False, num_classes=10):"""Extract the labels into a 1D uint8 numpy array [index].Args:f: A file object that can be passed into a gzip reader.one_hot: Does one hot encoding for the result.num_classes: Number of classes for the one hot encoding.Returns:labels: a 1D uint8 numpy array.Raises:ValueError: If the bystream doesn't start with 2049."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2049:raise ValueError('Invalid magic number %d in MNIST label file: %s' %(magic, f.name))num_items = _read32(bytestream)buf = bytestream.read(num_items)labels = numpy.frombuffer(buf, dtype=numpy.uint8)if one_hot:return dense_to_one_hot(labels, num_classes)return labelsdef read_data_sets(train_dir,fake_data=False,one_hot=False,dtype=tf.float32,reshape=True,validation_size=5000,seed=None):'''if fake_data:def fake():return DataSet([], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed)train = fake()validation = fake()test = fake()return base.Datasets(train=train, validation=validation, test=test)'''TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'TEST_IMAGES = 't10k-images-idx3-ubyte.gz'TEST_LABELS = 't10k-labels-idx1-ubyte.gz'local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TRAIN_IMAGESwith open(local_file, 'rb') as f:train_images = extract_images(f)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TRAIN_LABELSwith open(local_file, 'rb') as f:train_labels = extract_labels(f, one_hot=one_hot)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TEST_IMAGESwith open(local_file, 'rb') as f:test_images = extract_images(f)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TEST_LABELSwith open(local_file, 'rb') as f:test_labels = extract_labels(f, one_hot=one_hot)if not 0 <= validation_size <= len(train_images):raise ValueError('Validation size should be between 0 and {}. Received: {}.'.format(len(train_images), validation_size))validation_images = train_images[:validation_size]validation_labels = train_labels[:validation_size]train_images = train_images[validation_size:]train_labels = train_labels[validation_size:]options = dict(dtype=dtype, reshape=reshape, seed=seed)train = DataSet(train_images, train_labels, **options)validation = DataSet(validation_images, validation_labels, **options)test = DataSet(test_images, test_labels, **options)return Datasets(train=train, validation=validation, test=test)class DataSet(object):def __init__(self,images,labels,fake_data=False,one_hot=False,dtype=tf.float32,reshape=True,seed=None):"""Construct a DataSet.one_hot arg is used only if fake_data is true. `dtype` can be either`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into`[0, 1]`. Seed arg provides for convenient deterministic testing."""seed1, seed2 = random_seed.get_seed(seed)# If op level seed is not set, use whatever graph level seed is returnednumpy.random.seed(seed1 if seed is None else seed2)dtype = dtypes.as_dtype(dtype).base_dtypeif dtype not in (dtypes.uint8, dtypes.float32):raise TypeError('Invalid image dtype %r, expected uint8 or float32' %dtype)if fake_data:self._num_examples = 10000self.one_hot = one_hotelse:assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape, labels.shape))self._num_examples = images.shape[0]# Convert shape from [num examples, rows, columns, depth]# to [num examples, rows*columns] (assuming depth == 1)if reshape:assert images.shape[3] == 1images = images.reshape(images.shape[0],images.shape[1] * images.shape[2])if dtype == dtypes.float32:# Convert from [0, 255] -> [0.0, 1.0].images = images.astype(numpy.float32)images = numpy.multiply(images, 1.0 / 255.0)self._images = imagesself._labels = labelsself._epochs_completed = 0self._index_in_epoch = 0@propertydef images(self):return self._images@propertydef labels(self):return self._labels@propertydef num_examples(self):return self._num_examples@propertydef epochs_completed(self):return self._epochs_completeddef next_batch(self, batch_size, fake_data=False, shuffle=True):"""Return the next `batch_size` examples from this data set."""if fake_data:fake_image = [1] * 784if self.one_hot:fake_label = [1] + [0] * 9else:fake_label = 0return [fake_image for _ in xrange(batch_size)], [fake_label for _ in xrange(batch_size)]start = self._index_in_epoch# Shuffle for the first epochif self._epochs_completed == 0 and start == 0 and shuffle:perm0 = numpy.arange(self._num_examples)numpy.random.shuffle(perm0)self._images = self.images[perm0]self._labels = self.labels[perm0]# Go to the next epochif start + batch_size > self._num_examples:# Finished epochself._epochs_completed += 1# Get the rest examples in this epochrest_num_examples = self._num_examples - startimages_rest_part = self._images[start:self._num_examples]labels_rest_part = self._labels[start:self._num_examples]# Shuffle the dataif shuffle:perm = numpy.arange(self._num_examples)numpy.random.shuffle(perm)self._images = self.images[perm]self._labels = self.labels[perm]# Start next epochstart = 0self._index_in_epoch = batch_size - rest_num_examplesend = self._index_in_epochimages_new_part = self._images[start:end]labels_new_part = self._labels[start:end]return numpy.concatenate((images_rest_part, images_new_part), axis=0) , numpy.concatenate((labels_rest_part, labels_new_part), axis=0)else:self._index_in_epoch += batch_sizeend = self._index_in_epochreturn self._images[start:end], self._labels[start:end]################################################################ #from here to operate the networkmnist = read_data_sets('MNIST_data', one_hot=True)def add_layer(inputs, in_size, out_size, activation_function=None):weights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)wx_b = tf.matmul(inputs, weights) + biasesif activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b,)return outputsxs = tf.placeholder(tf.float32, [None, 28*28]) ys = tf.placeholder(tf.float32, [None, 10])def compute_accuracy(v_xs, v_ys):global predictiony_pre = sess.run(prediction, feed_dict={xs: v_xs})correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})return resultprediction = add_layer(xs, 784, 10, activation_function = tf.nn.softmax)cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)sess = tf.Session()sess.run(tf.initialize_all_variables())for i in range(10000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train_step, feed_dict = {xs: batch_xs, ys: batch_ys})if i % 50 ==0:print "step:",i,", ",compute_accuracy(mnist.test.images, mnist.test.labels) File "<ipython-input-12-d56c6b11bc6b>", line 279print "step:",i,", ",compute_accuracy(mnist.test.images, mnist.test.labels)^ SyntaxError: invalid syntax # ex-10-ml.py# import tensorflow as tf import gzip import numpy import collections from tensorflow.python.framework import random_seed from tensorflow.python.framework import dtypes #from tensorflow.examples.tutorials.mnist import input_dataDatasets = collections.namedtuple('Datasets', ['train', 'validation', 'test' ])def _read32(bytestream):dt = numpy.dtype(numpy.uint32).newbyteorder('>')return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]def dense_to_one_hot(labels_dense, num_classes):"""Convert class labels from scalars to one-hot vectors."""num_labels = labels_dense.shape[0]index_offset = numpy.arange(num_labels) * num_classeslabels_one_hot = numpy.zeros((num_labels, num_classes))labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1return labels_one_hotdef extract_images(f):"""Extract the images into a 4D uint8 numpy array [index, y, x, depth].Args:f: A file object that can be passed into a gzip reader.Returns:data: A 4D uint8 numpy array [index, y, x, depth].Raises:ValueError: If the bytestream does not start with 2051."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2051:raise ValueError('Invalid magic number %d in MNIST image file: %s' %(magic, f.name))num_images = _read32(bytestream)rows = _read32(bytestream)cols = _read32(bytestream)buf = bytestream.read(rows * cols * num_images)data = numpy.frombuffer(buf, dtype=numpy.uint8)data = data.reshape(num_images, rows, cols, 1)return datadef extract_labels(f, one_hot=False, num_classes=10):"""Extract the labels into a 1D uint8 numpy array [index].Args:f: A file object that can be passed into a gzip reader.one_hot: Does one hot encoding for the result.num_classes: Number of classes for the one hot encoding.Returns:labels: a 1D uint8 numpy array.Raises:ValueError: If the bystream doesn't start with 2049."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2049:raise ValueError('Invalid magic number %d in MNIST label file: %s' %(magic, f.name))num_items = _read32(bytestream)buf = bytestream.read(num_items)labels = numpy.frombuffer(buf, dtype=numpy.uint8)if one_hot:return dense_to_one_hot(labels, num_classes)return labelsdef read_data_sets(train_dir,fake_data=False,one_hot=False,dtype=tf.float32,reshape=True,validation_size=5000,seed=None):'''if fake_data:def fake():return DataSet([], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed)train = fake()validation = fake()test = fake()return base.Datasets(train=train, validation=validation, test=test)'''TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'TEST_IMAGES = 't10k-images-idx3-ubyte.gz'TEST_LABELS = 't10k-labels-idx1-ubyte.gz'local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TRAIN_IMAGESwith open(local_file, 'rb') as f:train_images = extract_images(f)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TRAIN_LABELSwith open(local_file, 'rb') as f:train_labels = extract_labels(f, one_hot=one_hot)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TEST_IMAGESwith open(local_file, 'rb') as f:test_images = extract_images(f)local_file = '/home/gao/dl_learning/py-example/MNIST_data/' + TEST_LABELSwith open(local_file, 'rb') as f:test_labels = extract_labels(f, one_hot=one_hot)if not 0 <= validation_size <= len(train_images):raise ValueError('Validation size should be between 0 and {}. Received: {}.'.format(len(train_images), validation_size))validation_images = train_images[:validation_size]validation_labels = train_labels[:validation_size]train_images = train_images[validation_size:]train_labels = train_labels[validation_size:]options = dict(dtype=dtype, reshape=reshape, seed=seed)train = DataSet(train_images, train_labels, **options)validation = DataSet(validation_images, validation_labels, **options)test = DataSet(test_images, test_labels, **options)return Datasets(train=train, validation=validation, test=test)class DataSet(object):def __init__(self,images,labels,fake_data=False,one_hot=False,dtype=tf.float32,reshape=True,seed=None):"""Construct a DataSet.one_hot arg is used only if fake_data is true. `dtype` can be either`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into`[0, 1]`. Seed arg provides for convenient deterministic testing."""seed1, seed2 = random_seed.get_seed(seed)# If op level seed is not set, use whatever graph level seed is returnednumpy.random.seed(seed1 if seed is None else seed2)dtype = dtypes.as_dtype(dtype).base_dtypeif dtype not in (dtypes.uint8, dtypes.float32):raise TypeError('Invalid image dtype %r, expected uint8 or float32' %dtype)if fake_data:self._num_examples = 10000self.one_hot = one_hotelse:assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape, labels.shape))self._num_examples = images.shape[0]# Convert shape from [num examples, rows, columns, depth]# to [num examples, rows*columns] (assuming depth == 1)if reshape:assert images.shape[3] == 1images = images.reshape(images.shape[0],images.shape[1] * images.shape[2])if dtype == dtypes.float32:# Convert from [0, 255] -> [0.0, 1.0].images = images.astype(numpy.float32)images = numpy.multiply(images, 1.0 / 255.0)self._images = imagesself._labels = labelsself._epochs_completed = 0self._index_in_epoch = 0@propertydef images(self):return self._images@propertydef labels(self):return self._labels@propertydef num_examples(self):return self._num_examples@propertydef epochs_completed(self):return self._epochs_completeddef next_batch(self, batch_size, fake_data=False, shuffle=True):"""Return the next `batch_size` examples from this data set."""if fake_data:fake_image = [1] * 784if self.one_hot:fake_label = [1] + [0] * 9else:fake_label = 0return [fake_image for _ in xrange(batch_size)], [fake_label for _ in xrange(batch_size)]start = self._index_in_epoch# Shuffle for the first epochif self._epochs_completed == 0 and start == 0 and shuffle:perm0 = numpy.arange(self._num_examples)numpy.random.shuffle(perm0)self._images = self.images[perm0]self._labels = self.labels[perm0]# Go to the next epochif start + batch_size > self._num_examples:# Finished epochself._epochs_completed += 1# Get the rest examples in this epochrest_num_examples = self._num_examples - startimages_rest_part = self._images[start:self._num_examples]labels_rest_part = self._labels[start:self._num_examples]# Shuffle the dataif shuffle:perm = numpy.arange(self._num_examples)numpy.random.shuffle(perm)self._images = self.images[perm]self._labels = self.labels[perm]# Start next epochstart = 0self._index_in_epoch = batch_size - rest_num_examplesend = self._index_in_epochimages_new_part = self._images[start:end]labels_new_part = self._labels[start:end]return numpy.concatenate((images_rest_part, images_new_part), axis=0) , numpy.concatenate((labels_rest_part, labels_new_part), axis=0)else:self._index_in_epoch += batch_sizeend = self._index_in_epochreturn self._images[start:end], self._labels[start:end]################################################################mnist = read_data_sets('MNIST_data', one_hot=True)def add_layer(inputs, in_size, out_size, activation_function=None):weights = tf.Variable(tf.random_normal([in_size, out_size]))#biases = tf.Variable(tf.zeros([1, out_size]) + 100)biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)wx_b = tf.matmul(inputs, weights) + biasesif activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b,)return outputsxs = tf.placeholder(tf.float32, [None, 28*28]) ys = tf.placeholder(tf.float32, [None, 10])def compute_accuracy(v_xs, v_ys):global predictiony_pre = sess.run(prediction, feed_dict={xs: v_xs})correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})return result#layer1 = add_layer(xs, 784, 50, activation_function = tf.nn.tanh) #layer1 = add_layer(xs, 784, 30, activation_function = tf.nn.sigmoid) layer1 = add_layer(xs, 784, 50, activation_function = tf.nn.relu) layer2 = add_layer(layer1, 50, 50, activation_function = tf.nn.tanh) layer3 = add_layer(layer2, 50, 50, activation_function = tf.nn.tanh) layer4 = add_layer(layer3, 50, 50, activation_function = tf.nn.tanh)prediction = add_layer(layer4, 50, 10, activation_function = tf.nn.softmax)cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)sess = tf.Session()sess.run(tf.initialize_all_variables())for i in range(100000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(layer1, feed_dict = {xs: batch_xs, ys: batch_ys})sess.run(train_step, feed_dict = {xs: batch_xs, ys:batch_ys})if i % 100 ==0:print "step:",i,", ",compute_accuracy(mnist.test.images, mnist.test.labels) File "<ipython-input-13-eaeb1867aa2d>", line 286print "step:",i,", ",compute_accuracy(mnist.test.images, mnist.test.labels)^ SyntaxError: invalid syntax # ex-11.py import tensorflow as tf from sklearn.datasets import load_digits from sklearn.cross_validation import train_test_split from sklearn.preprocessing import LabelBinarizerdigits = load_digits() x = digits.data y = digits.target #one-hot transform y = LabelBinarizer().fit_transform(y) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)def add_layer(inputs, in_size, out_size, layer_name, activation_function = None):weights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)wx_b = tf.matmul(inputs, weights) + biasesif activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b)tf.summary.histogram(layer_name + '/outputs', outputs)return outputsxs = tf.placeholder(tf.float32, [None, 8*8]) ys = tf.placeholder(tf.float32, [None, 10])layer1 = add_layer(xs, 64, 100, 'layer1', activation_function = tf.nn.tanh) prediction = add_layer(layer1, 100, 10, 'layer2', activation_function = tf.nn.softmax)cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))tf.summary.scalar('loss', cross_entropy)train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)sess = tf.Session() merged = tf.summary.merge_all()train_writer = tf.train.SummaryWriter("logs/train", sess.graph) test_writer = tf.train.SummaryWriter("logs/test", sess.graph)sess.run(tf.initialize_all_variables())for i in range(1000):sess.run(train_step, feed_dict = {xs: x_train, ys:y_train})if i % 100==0:train_result = sess.run(merged, feed_dict={xs:x_train, ys:y_train})test_result = sess.run(merged, feed_dict={xs:x_test, ys:y_test})train_writer.add_summary(train_result, i)test_writer.add_summary(test_result, i) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20."This module will be removed in 0.20.", DeprecationWarning)---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-14-212ae64f528b> in <module>()39 merged = tf.summary.merge_all()40 ---> 41 train_writer = tf.train.SummaryWriter("logs/train", sess.graph)42 test_writer = tf.train.SummaryWriter("logs/test", sess.graph)43 AttributeError: module 'tensorflow.python.training.training' has no attribute 'SummaryWriter' # ex-11-dropout.py import tensorflow as tf from sklearn.datasets import load_digits from sklearn.cross_validation import train_test_split from sklearn.preprocessing import LabelBinarizerdigits = load_digits() x = digits.data y = digits.target #one-hot transform y = LabelBinarizer().fit_transform(y) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)def add_layer(inputs, in_size, out_size, layer_name, activation_function = None):weights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)wx_b = tf.matmul(inputs, weights) + biases#drop out and keep a proportionwx_b = tf.nn.dropout(wx_b, keep_prob)if activation_function is None:outputs = wx_belse:outputs = activation_function(wx_b)tf.histogram_summary(layer_name + '/outputs', outputs)return outputskeep_prob = tf.placeholder(tf.float32) xs = tf.placeholder(tf.float32, [None, 8*8]) ys = tf.placeholder(tf.float32, [None, 10])layer1 = add_layer(xs, 64, 100, 'layer1', activation_function = tf.nn.tanh) prediction = add_layer(layer1, 100, 10, 'layer2', activation_function = tf.nn.softmax)cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))tf.scalar_summary('loss', cross_entropy)train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)sess = tf.Session() merged = tf.merge_all_summaries()train_writer = tf.train.SummaryWriter("logs/train", sess.graph) test_writer = tf.train.SummaryWriter("logs/test", sess.graph)sess.run(tf.initialize_all_variables())for i in range(1000):sess.run(train_step, feed_dict = {xs: x_train, ys:y_train, keep_prob:0.7})if i % 100==0:train_result = sess.run(merged, feed_dict={xs:x_train, ys:y_train, keep_prob: 0.7})test_result = sess.run(merged, feed_dict={xs:x_test, ys:y_test, keep_prob: 0.7})train_writer.add_summary(train_result, i)test_writer.add_summary(test_result, i) ---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-15-482298ce8d49> in <module>()30 ys = tf.placeholder(tf.float32, [None, 10])31 ---> 32 layer1 = add_layer(xs, 64, 100, 'layer1', activation_function = tf.nn.tanh)33 prediction = add_layer(layer1, 100, 10, 'layer2', activation_function = tf.nn.softmax)34 <ipython-input-15-482298ce8d49> in add_layer(inputs, in_size, out_size, layer_name, activation_function)23 else:24 outputs = activation_function(wx_b) ---> 25 tf.histogram_summary(layer_name + '/outputs', outputs)26 return outputs27 AttributeError: module 'tensorflow' has no attribute 'histogram_summary'input_data.py
# 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. # =============================================================================="""Functions for downloading and reading MNIST data.""" from __future__ import absolute_import from __future__ import division from __future__ import print_functionimport gzip import os import tempfileimport 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_setsmnist.py
# Copyright 2016 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. # =============================================================================="""Functions for downloading and reading MNIST data."""from __future__ import absolute_import from __future__ import division from __future__ import print_functionimport gzipimport numpy from six.moves import xrange # pylint: disable=redefined-builtinfrom tensorflow.contrib.learn.python.learn.datasets import base from tensorflow.python.framework import dtypes from tensorflow.python.framework import random_seed# CVDF mirror of http://yann.lecun.com/exdb/mnist/ SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/'def _read32(bytestream):dt = numpy.dtype(numpy.uint32).newbyteorder('>')return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]def extract_images(f):"""Extract the images into a 4D uint8 numpy array [index, y, x, depth].Args:f: A file object that can be passed into a gzip reader.Returns:data: A 4D uint8 numpy array [index, y, x, depth].Raises:ValueError: If the bytestream does not start with 2051."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2051:raise ValueError('Invalid magic number %d in MNIST image file: %s' %(magic, f.name))num_images = _read32(bytestream)rows = _read32(bytestream)cols = _read32(bytestream)buf = bytestream.read(rows * cols * num_images)data = numpy.frombuffer(buf, dtype=numpy.uint8)data = data.reshape(num_images, rows, cols, 1)return datadef dense_to_one_hot(labels_dense, num_classes):"""Convert class labels from scalars to one-hot vectors."""num_labels = labels_dense.shape[0]index_offset = numpy.arange(num_labels) * num_classeslabels_one_hot = numpy.zeros((num_labels, num_classes))labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1return labels_one_hotdef extract_labels(f, one_hot=False, num_classes=10):"""Extract the labels into a 1D uint8 numpy array [index].Args:f: A file object that can be passed into a gzip reader.one_hot: Does one hot encoding for the result.num_classes: Number of classes for the one hot encoding.Returns:labels: a 1D uint8 numpy array.Raises:ValueError: If the bystream doesn't start with 2049."""print('Extracting', f.name)with gzip.GzipFile(fileobj=f) as bytestream:magic = _read32(bytestream)if magic != 2049:raise ValueError('Invalid magic number %d in MNIST label file: %s' %(magic, f.name))num_items = _read32(bytestream)buf = bytestream.read(num_items)labels = numpy.frombuffer(buf, dtype=numpy.uint8)if one_hot:return dense_to_one_hot(labels, num_classes)return labelsclass DataSet(object):def __init__(self,images,labels,fake_data=False,one_hot=False,dtype=dtypes.float32,reshape=True,seed=None):"""Construct a DataSet.one_hot arg is used only if fake_data is true. `dtype` can be either`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into`[0, 1]`. Seed arg provides for convenient deterministic testing."""seed1, seed2 = random_seed.get_seed(seed)# If op level seed is not set, use whatever graph level seed is returnednumpy.random.seed(seed1 if seed is None else seed2)dtype = dtypes.as_dtype(dtype).base_dtypeif dtype not in (dtypes.uint8, dtypes.float32):raise TypeError('Invalid image dtype %r, expected uint8 or float32' %dtype)if fake_data:self._num_examples = 10000self.one_hot = one_hotelse:assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape, labels.shape))self._num_examples = images.shape[0]# Convert shape from [num examples, rows, columns, depth]# to [num examples, rows*columns] (assuming depth == 1)if reshape:assert images.shape[3] == 1images = images.reshape(images.shape[0],images.shape[1] * images.shape[2])if dtype == dtypes.float32:# Convert from [0, 255] -> [0.0, 1.0].images = images.astype(numpy.float32)images = numpy.multiply(images, 1.0 / 255.0)self._images = imagesself._labels = labelsself._epochs_completed = 0self._index_in_epoch = 0@propertydef images(self):return self._images@propertydef labels(self):return self._labels@propertydef num_examples(self):return self._num_examples@propertydef epochs_completed(self):return self._epochs_completeddef next_batch(self, batch_size, fake_data=False, shuffle=True):"""Return the next `batch_size` examples from this data set."""if fake_data:fake_image = [1] * 784if self.one_hot:fake_label = [1] + [0] * 9else:fake_label = 0return [fake_image for _ in xrange(batch_size)], [fake_label for _ in xrange(batch_size)]start = self._index_in_epoch# Shuffle for the first epochif self._epochs_completed == 0 and start == 0 and shuffle:perm0 = numpy.arange(self._num_examples)numpy.random.shuffle(perm0)self._images = self.images[perm0]self._labels = self.labels[perm0]# Go to the next epochif start + batch_size > self._num_examples:# Finished epochself._epochs_completed += 1# Get the rest examples in this epochrest_num_examples = self._num_examples - startimages_rest_part = self._images[start:self._num_examples]labels_rest_part = self._labels[start:self._num_examples]# Shuffle the dataif shuffle:perm = numpy.arange(self._num_examples)numpy.random.shuffle(perm)self._images = self.images[perm]self._labels = self.labels[perm]# Start next epochstart = 0self._index_in_epoch = batch_size - rest_num_examplesend = self._index_in_epochimages_new_part = self._images[start:end]labels_new_part = self._labels[start:end]return numpy.concatenate((images_rest_part, images_new_part), axis=0) , numpy.concatenate((labels_rest_part, labels_new_part), axis=0)else:self._index_in_epoch += batch_sizeend = self._index_in_epochreturn self._images[start:end], self._labels[start:end]def read_data_sets(train_dir,fake_data=False,one_hot=False,dtype=dtypes.float32,reshape=True,validation_size=5000,seed=None):if fake_data:def fake():return DataSet([], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed)train = fake()validation = fake()test = fake()return base.Datasets(train=train, validation=validation, test=test)TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'TEST_IMAGES = 't10k-images-idx3-ubyte.gz'TEST_LABELS = 't10k-labels-idx1-ubyte.gz'local_file = base.maybe_download(TRAIN_IMAGES, train_dir,SOURCE_URL + TRAIN_IMAGES)with open(local_file, 'rb') as f:train_images = extract_images(f)local_file = base.maybe_download(TRAIN_LABELS, train_dir,SOURCE_URL + TRAIN_LABELS)with open(local_file, 'rb') as f:train_labels = extract_labels(f, one_hot=one_hot)local_file = base.maybe_download(TEST_IMAGES, train_dir,SOURCE_URL + TEST_IMAGES)with open(local_file, 'rb') as f:test_images = extract_images(f)local_file = base.maybe_download(TEST_LABELS, train_dir,SOURCE_URL + TEST_LABELS)with open(local_file, 'rb') as f:test_labels = extract_labels(f, one_hot=one_hot)if not 0 <= validation_size <= len(train_images):raise ValueError('Validation size should be between 0 and {}. Received: {}.'.format(len(train_images), validation_size))validation_images = train_images[:validation_size]validation_labels = train_labels[:validation_size]train_images = train_images[validation_size:]train_labels = train_labels[validation_size:]options = dict(dtype=dtype, reshape=reshape, seed=seed)train = DataSet(train_images, train_labels, **options)validation = DataSet(validation_images, validation_labels, **options)test = DataSet(test_images, test_labels, **options)return base.Datasets(train=train, validation=validation, test=test)def load_mnist(train_dir='MNIST-data'):return read_data_sets(train_dir)轉(zhuǎn)載于:https://www.cnblogs.com/q735613050/p/7745962.html
總結(jié)
- 上一篇: Linux下C编程入门(7)
- 下一篇: easyui tree:根据属性格式化树