轉(zhuǎn)自AIMaster
這一系列文章中秕岛,你將學(xué)到深度學(xué)習(xí)的一些基本概念以及TensorFlow的使用醋旦,并完成手寫體數(shù)字識別、圖像分類咒劲、遷移學(xué)習(xí)顷蟆、Deep Dream、風(fēng)格遷移和強(qiáng)化學(xué)習(xí)等項(xiàng)目腐魂。 github上的Python NoteBook也可以很方便的調(diào)試代碼帐偎。
by Magnus Erik Hvass Pedersen / GitHub / Videos on YouTube中文翻譯 thrillerist /Github
這份教程示范了在TensorFlow中使用一個(gè)簡單線性模型的工作流程。在載入稱為MNIST的手寫數(shù)字圖片數(shù)據(jù)集后蛔屹,我們在TensorFlow中定義并優(yōu)化了一個(gè)數(shù)學(xué)模型削樊。(我們)會畫出結(jié)果并展開討論。
你應(yīng)該熟悉基本的線性代數(shù),Python和Jupyter Notebook編輯器漫贞。如果你對機(jī)器學(xué)習(xí)和分類有基本的理解也很有幫助甸箱。
導(dǎo)入
%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
使用Python3.5(Anaconda)開發(fā),TensorFlow版本是
tf.__version__
'1.0.0-rc0'
載入數(shù)據(jù)
MNIST數(shù)據(jù)集大約有12MB迅脐,如果給定的地址沒有文件芍殖,它將自動(dòng)下載。
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets("data/MNIST/", one_hot=True)
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting data/MNIST/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting data/MNIST/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting data/MNIST/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/MNIST/t10k-labels-idx1-ubyte.gz
現(xiàn)在已經(jīng)載入了MNIST數(shù)據(jù)集谴蔑,它由70,000張圖像和對應(yīng)的標(biāo)簽(比如圖像的類別)組成豌骏。數(shù)據(jù)集分成三份互相獨(dú)立的子集。我們在教程中只用訓(xùn)練集和測試集隐锭。
print("Size of:")
print("- Training-set:\t\t{}".format(len(data.train.labels)))
print("- Test-set:\t\t{}".format(len(data.test.labels)))
print("- Validation-set:\t{}".format(len(data.validation.labels)))
Size of:
- Training-set: 55000
- Test-set: 10000
- Validation-set: 5000
One-Hot編碼
數(shù)據(jù)集以一種稱為One-Hot編碼的方式載入肯适。這意味著標(biāo)簽從一個(gè)單獨(dú)的數(shù)字轉(zhuǎn)換成一個(gè)長度等于所有可能類別數(shù)量的向量。向量中除了第$i$個(gè)元素是1成榜,其他元素都是0框舔,這代表著它的類別是$i$'。比如赎婚,前面五張圖像標(biāo)簽的One-Hot編碼為:
data.test.labels[0:5, :]
array([[ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
在不同的比較和度量性能時(shí)刘绣,我們也需要用單獨(dú)的數(shù)字表示類別,因此我們通過取最大元素的索引挣输,將One-Hot編碼的向量轉(zhuǎn)換成一個(gè)單獨(dú)的數(shù)字纬凤。需注意的是'class'在Python中是一個(gè)關(guān)鍵字,所以我們用'cls'代替它撩嚼。
data.test.cls = np.array([label.argmax() for label in data.test.labels])
data.test.cls[0:5]
現(xiàn)在我們可以看到測試集中前面五張圖像的類別停士。將這些與上面的One-Hot編碼的向量進(jìn)行比較。例如完丽,第一張圖像的類別是7恋技,對應(yīng)的在One-Hot編碼向量中,除了第7個(gè)元素其他都為零逻族。
array([7, 2, 1, 0, 4])
數(shù)據(jù)維度
在下面的源碼中蜻底,有很多地方用到了數(shù)據(jù)維度。在計(jì)算機(jī)編程中聘鳞,通常來說最好使用變量和常量薄辅,而不是在每次使用數(shù)值時(shí)寫硬代碼。這意味著數(shù)字只需要在一個(gè)地方改動(dòng)就行抠璃。這些最好能從讀取的數(shù)據(jù)中獲取站楚,但這里我們直接寫上數(shù)值。
# We know that MNIST images are 28 pixels in each dimension.
img_size = 28
# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size
# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)
# Number of classes, one class for each of 10 digits.
num_classes = 10
用來繪制圖像的幫助函數(shù)
這個(gè)函數(shù)用來在3x3的柵格中畫9張圖像搏嗡,然后在每張圖像下面寫出真實(shí)的和預(yù)測的類別窿春。
def plot_images(images, cls_true, cls_pred=None):
assert len(images) == len(cls_true) == 9
# Create figure with 3x3 sub-plots.
fig, axes = plt.subplots(3, 3)
fig.subplots_adjust(hspace=0.3, wspace=0.3)
for i, ax in enumerate(axes.flat):
# Plot image.
ax.imshow(images[i].reshape(img_shape), cmap='binary')
# Show true and predicted classes.
if cls_pred is None:
xlabel = "True: {0}".format(cls_true[i])
else:
xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])
ax.set_xlabel(xlabel)
# Remove ticks from the plot.
ax.set_xticks([])
ax.set_yticks([])
繪制幾張圖像來看看數(shù)據(jù)是否正確
# Get the first images from the test-set.
images = data.test.images[0:9]
# Get the true classes for those images.
cls_true = data.test.cls[0:9]
# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true)
TensorFlow圖
TensorFlow的全部目的就是使用一個(gè)稱之為計(jì)算圖(computational graph)的東西,它會比直接在Python中進(jìn)行相同計(jì)算量要高效得多。TensorFlow比Numpy更高效谁尸,因?yàn)門ensorFlow了解整個(gè)需要運(yùn)行的計(jì)算圖舅踪,然而Numpy只知道某個(gè)時(shí)間點(diǎn)上唯一的數(shù)學(xué)運(yùn)算。
TensorFlow也能夠自動(dòng)地計(jì)算需要優(yōu)化的變量的梯度良蛮,使得模型有更好的表現(xiàn)抽碌。這是由于Graph是簡單數(shù)學(xué)表達(dá)式的結(jié)合,因此整個(gè)圖的梯度可以用鏈?zhǔn)椒▌t推導(dǎo)出來决瞳。
TensorFlow還能利用多核CPU和GPU货徙,Google也為TensorFlow制造了稱為TPUs(Tensor Processing Units)的特殊芯片,它比GPU更快皮胡。
一個(gè)TensorFlow圖由下面幾個(gè)部分組成痴颊,后面會詳細(xì)描述:
- 占位符變量(Placeholder)用來改變圖的輸入。
- 模型變量(Model)將會被優(yōu)化屡贺,使得模型表現(xiàn)得更好蠢棱。
- 模型本質(zhì)上就是一些數(shù)學(xué)函數(shù),它根據(jù)Placeholder和模型的輸入變量來計(jì)算一些輸出甩栈。
- 一個(gè)cost度量用來指導(dǎo)變量的優(yōu)化泻仙。
- 一個(gè)優(yōu)化策略會更新模型的變量。
另外量没,TensorFlow圖也包含了一些調(diào)試狀態(tài)玉转,比如用TensorBoard打印log數(shù)據(jù),本教程不涉及這些殴蹄。
占位符 (Placeholder)變量
Placeholder是作為圖的輸入究抓,每次我們運(yùn)行圖的時(shí)候都可能會改變它們。將這個(gè)過程稱為feeding placeholder變量袭灯,后面將會描述它刺下。
首先我們?yōu)檩斎雸D像定義placeholder變量。這讓我們可以改變輸入到TensorFlow圖中的圖像妓蛮。這也是一個(gè)張量(tensor)怠李,代表一個(gè)多維向量或矩陣。數(shù)據(jù)類型設(shè)置為float32
蛤克,形狀設(shè)為[None, img_size_flat]
,None
代表tensor可能保存著任意數(shù)量的圖像夷蚊,每張圖象是一個(gè)長度為img_size_flat
的向量构挤。
x = tf.placeholder(tf.float32, [None, img_size_flat])
接下來我們?yōu)檩斎胱兞縳中的圖像所對應(yīng)的真實(shí)標(biāo)簽定義placeholder變量。變量的形狀是[None, num_classes]
惕鼓,這代表著它保存了任意數(shù)量的標(biāo)簽筋现,每個(gè)標(biāo)簽是長度為num_classes
的向量,本例中長度為10。
y_true = tf.placeholder(tf.float32, [None, num_classes])
最后我們?yōu)樽兞縳中圖像的真實(shí)類別定義placeholder變量矾飞。它們是整形一膨,并且這個(gè)變量的維度設(shè)為[None]
,代表placeholder變量是任意長的一維向量洒沦。
y_true_cls = tf.placeholder(tf.int64, [None])
需要優(yōu)化的變量
除了上面定義的那些給模型輸入數(shù)據(jù)的變量之外豹绪,TensorFlow還需要改變一些模型變量,使得訓(xùn)練數(shù)據(jù)的表現(xiàn)更好申眼。
第一個(gè)需要優(yōu)化的變量稱為權(quán)重weight
瞒津,TensorFlow變量需要被初始化為零,它的形狀是[img_size_flat, num_classes]
括尸,因此它是一個(gè)img_size_flat
行巷蚪、num_classes
列的二維張量(或矩陣)。
weights = tf.Variable(tf.zeros([img_size_flat, num_classes]))
第二個(gè)需要優(yōu)化的是偏差變量biases
濒翻,它被定義成一個(gè)長度為num_classes
的1維張量(或向量)屁柏。
biases = tf.Variable(tf.zeros([num_classes]))
模型
這個(gè)最基本的數(shù)學(xué)模型將placeholder變量x中的圖像與權(quán)重weight
相乘,然后加上偏差biases
有送。
結(jié)果是大小為[num_images, num_classes]
的一個(gè)矩陣淌喻,由于x的形狀是[num_images, img_size_flat]
并且 weights
的形狀是[img_size_flat, num_classes]
,因此兩個(gè)矩陣乘積的形狀是[num_images, num_classes]
娶眷,然后將biases
向量添加到矩陣每一行中似嗤。
logits = tf.matmul(x, weights) + biases
現(xiàn)在logits
是一個(gè)num_images
行num_classes
列的矩陣,第i
行第j
列的那個(gè)元素代表著第i
張輸入圖像有多大可能性是第j
個(gè)類別届宠。
然而烁落,這是很粗略的估計(jì)并且很難解釋,因?yàn)閿?shù)值可能很小或很大豌注,因此我們想要對它們做歸一化伤塌,使得logits
矩陣的每一行相加為1,每個(gè)元素限制在0到1之間轧铁。這是用一個(gè)稱為softmax
的函數(shù)來計(jì)算的每聪,結(jié)果保存在y_pred
中。
y_pred = tf.nn.softmax(logits)
可以從y_pred
矩陣中取每行最大元素的索引值齿风,來得到預(yù)測的類別药薯。
y_pred_cls = tf.argmax(y_pred, dimension=1)
優(yōu)化損失函數(shù)
為了使模型更好地對輸入圖像進(jìn)行分類,我們必須改變weights
和biases
變量救斑。首先我們需要比較模型的預(yù)測輸出y_pred
和期望輸出y_true
童本,來了解目前模型的性能如何。
交叉熵(cross-entropy)是一個(gè)在分類中使用的性能度量脸候。交叉熵是一個(gè)常為正值的連續(xù)函數(shù)穷娱,如果模型的預(yù)測值精準(zhǔn)地符合期望的輸出绑蔫,它就等于零。因此泵额,優(yōu)化的目的就是最小化交叉熵配深,通過改變模型中weights
和biases
的值,使交叉熵越接近零越好嫁盲。
TensorFlow有一個(gè)內(nèi)置的計(jì)算交叉熵的函數(shù)篓叶。需要注意的是它使用logits
的值,因?yàn)樵谒鼉?nèi)部也計(jì)算了softmax亡资。
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
labels=y_true)
現(xiàn)在澜共,我們已經(jīng)為每個(gè)圖像分類計(jì)算了交叉熵,所以有一個(gè)當(dāng)前模型在每張圖上的性能度量锥腻。但是為了用交叉熵來指導(dǎo)模型變量的優(yōu)化嗦董,我們需要一個(gè)額外的標(biāo)量值,因此我們簡單地利用所有圖像分類交叉熵的均值瘦黑。
cost = tf.reduce_mean(cross_entropy)
優(yōu)化方法
現(xiàn)在京革,我們有一個(gè)需要被最小化的損失度量,接著我們可以創(chuàng)建優(yōu)化器幸斥。在這種情況中匹摇,用的是梯度下降的基本形式,步長設(shè)為0.5甲葬。
優(yōu)化過程并不是在這里執(zhí)行廊勃。實(shí)際上,還沒計(jì)算任何東西经窖,我們只是往TensorFlow圖中添加了優(yōu)化器坡垫,以便之后的操作。
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cost)
性能度量
我們需要另外一些性能度量画侣,來向用戶展示這個(gè)過程冰悠。
這是一個(gè)布爾值向量,代表預(yù)測類型是否等于每張圖片的真實(shí)類型配乱。
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
上面先將布爾值向量類型轉(zhuǎn)換成浮點(diǎn)型向量溉卓,這樣子False就變成0,True變成1搬泥,然后計(jì)算這些值的平均數(shù)桑寨,以此來計(jì)算分類的準(zhǔn)確度。
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
運(yùn)行TensorFlow
創(chuàng)建TensorFlow會話(session)
一旦創(chuàng)建了TensorFlow圖忿檩,我們需要?jiǎng)?chuàng)建一個(gè)TensorFlow session西疤,用來運(yùn)行圖。
session = tf.Session()
初始化變量
我們需要在開始優(yōu)化weights
和biases
變量之前對它們進(jìn)行初始化休溶。
session.run(tf.global_variables_initializer())
用來優(yōu)化迭代的幫助函數(shù)
在訓(xùn)練集中有50,000張圖代赁。用這些圖像計(jì)算模型的梯度會花很多時(shí)間。因此我們利用隨機(jī)梯度下降的方法兽掰,它在優(yōu)化器的每次迭代里只用到了一小部分的圖像芭碍。
batch_size = 100
函數(shù)執(zhí)行了多次的優(yōu)化迭代來逐步地提升模型的weights
和biases
。在每次迭代中孽尽,從訓(xùn)練集中選擇一批新的數(shù)據(jù)窖壕,然后TensorFlow用這些訓(xùn)練樣本來執(zhí)行優(yōu)化器。
def optimize(num_iterations):
for i in range(num_iterations):
# Get a batch of training examples.
# x_batch now holds a batch of images and
# y_true_batch are the true labels for those images.
x_batch, y_true_batch = data.train.next_batch(batch_size)
# Put the batch into a dict with the proper names
# for placeholder variables in the TensorFlow graph.
# Note that the placeholder for y_true_cls is not set
# because it is not used during training.
feed_dict_train = {x: x_batch,
y_true: y_true_batch}
# Run the optimizer using this batch of training data.
# TensorFlow assigns the variables in feed_dict_train
# to the placeholder variables and then runs the optimizer.
session.run(optimizer, feed_dict=feed_dict_train)
展示性能的幫助函數(shù)
測試集數(shù)據(jù)字典被當(dāng)做TensorFlow圖的輸入杉女。注意瞻讽,在TensorFlow圖中,placeholder變量必須使用正確的名字熏挎。
feed_dict_test = {x: data.test.images,
y_true: data.test.labels,
y_true_cls: data.test.cls}
用來打印測試集分類準(zhǔn)確度的函數(shù)速勇。
def print_accuracy():
# Use TensorFlow to compute the accuracy.
acc = session.run(accuracy, feed_dict=feed_dict_test)
# Print the accuracy.
print("Accuracy on test-set: {0:.1%}".format(acc))
函數(shù)用scikit-learn打印并繪制混淆矩陣。
def print_confusion_matrix():
# Get the true classifications for the test-set.
cls_true = data.test.cls
# Get the predicted classifications for the test-set.
cls_pred = session.run(y_pred_cls, feed_dict=feed_dict_test)
# Get the confusion matrix using sklearn.
cm = confusion_matrix(y_true=cls_true,
y_pred=cls_pred)
# Print the confusion matrix as text.
print(cm)
# Plot the confusion matrix as an image.
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
# Make various adjustments to the plot.
plt.tight_layout()
plt.colorbar()
tick_marks = np.arange(num_classes)
plt.xticks(tick_marks, range(num_classes))
plt.yticks(tick_marks, range(num_classes))
plt.xlabel('Predicted')
plt.ylabel('True')
繪制測試集中誤分類圖像的函數(shù)坎拐。
def plot_example_errors():
# Use TensorFlow to get a list of boolean values
# whether each test-image has been correctly classified,
# and a list for the predicted class of each image.
correct, cls_pred = session.run([correct_prediction, y_pred_cls],
feed_dict=feed_dict_test)
# Negate the boolean array.
incorrect = (correct == False)
# Get the images from the test-set that have been
# incorrectly classified.
images = data.test.images[incorrect]
# Get the predicted classes for those images.
cls_pred = cls_pred[incorrect]
# Get the true classes for those images.
cls_true = data.test.cls[incorrect]
# Plot the first 9 images.
plot_images(images=images[0:9],
cls_true=cls_true[0:9],
cls_pred=cls_pred[0:9])
繪制模型權(quán)重的幫助函數(shù)
這個(gè)函數(shù)用來繪制模型的權(quán)重weights
烦磁。畫了10張圖像,訓(xùn)練模型所識別出的每個(gè)數(shù)字對應(yīng)著一張圖哼勇。
def plot_weights():
# Get the values for the weights from the TensorFlow variable.
w = session.run(weights)
# Get the lowest and highest values for the weights.
# This is used to correct the colour intensity across
# the images so they can be compared with each other.
w_min = np.min(w)
w_max = np.max(w)
# Create figure with 3x4 sub-plots,
# where the last 2 sub-plots are unused.
fig, axes = plt.subplots(3, 4)
fig.subplots_adjust(hspace=0.3, wspace=0.3)
for i, ax in enumerate(axes.flat):
# Only use the weights for the first 10 sub-plots.
if i<10:
# Get the weights for the i'th digit and reshape it.
# Note that w.shape == (img_size_flat, 10)
image = w[:, i].reshape(img_shape)
# Set the label for the sub-plot.
ax.set_xlabel("Weights: {0}".format(i))
# Plot the image.
ax.imshow(image, vmin=w_min, vmax=w_max, cmap='seismic')
# Remove ticks from each sub-plot.
ax.set_xticks([])
ax.set_yticks([])
優(yōu)化之前的性能
測試集上的準(zhǔn)確度是9.8%都伪。這是由于模型只做了初始化,并沒做任何優(yōu)化积担,所以它通常將圖像預(yù)測成數(shù)字零陨晶,正如下面繪制的圖像那樣,剛好測試集中9.8%的圖像是數(shù)字零帝璧。
print_accuracy()
Accuracy on test-set: 9.8%
plot_example_errors()
1次迭代優(yōu)化后的性能
在完成一次迭代優(yōu)化之后先誉,模型在測試集上的準(zhǔn)確率從9.8%提高到了40.7%。這意味著它大約10次里面會誤分類6次聋溜,正如下面所顯示的谆膳。
optimize(num_iterations=1)
print_accuracy()
Accuracy on test-set: 40.7%
plot_example_errors()
下面繪制的是權(quán)重。正值為紅色撮躁,負(fù)值為藍(lán)色漱病。這些權(quán)重可以直觀地理解為圖像濾波器。
例如把曼,權(quán)重用來確定一張數(shù)字零的圖像對圓形圖像有正反應(yīng)(紅色)杨帽,對圓形圖像的中間部分有負(fù)反應(yīng)(藍(lán)色)。
類似的嗤军,權(quán)重也用來確定一張數(shù)字一的圖像對圖像中心垂直線段有正反應(yīng)(紅色)注盈,對線段周圍有負(fù)反應(yīng)(藍(lán)色)。
注意到權(quán)重大多看起來跟它要識別的數(shù)字很像叙赚。這是因?yàn)橹蛔隽艘淮蔚峡停礄?quán)重只在100張圖像上訓(xùn)練僚饭。等經(jīng)過上千張圖像的訓(xùn)練之后,權(quán)重會變得更難分辨胧砰,因?yàn)樗鼈冃枰R別出數(shù)字的許多種書寫方法鳍鸵。
plot_weights()
10次優(yōu)化迭代后的性能
# We have already performed 1 iteration.
optimize(num_iterations=9)
print_accuracy()
Accuracy on test-set: 78.2%
plot_example_errors()
plot_weights()
1000次迭代之后的性能
在迭代了1000次之后,模型在十次里面大約只誤識別了一次尉间。如下圖所示偿乖,有些誤識別情有可原,因?yàn)榧词乖谌祟愌劾镎艹埃埠茈y確定圖像(的數(shù)字)贪薪,然而有一些圖像是很明顯的,好的模型應(yīng)該能分辨出來眠副。但這個(gè)簡單的模型無法達(dá)到更好的性能画切,因此需要更為復(fù)雜的模型。
# We have already performed 10 iterations.
optimize(num_iterations=990)
print_accuracy()
Accuracy on test-set: 91.7%
plot_example_errors()
模型經(jīng)過了1000次迭代訓(xùn)練侦啸,每次迭代用到訓(xùn)練集里面的100張圖像槽唾。由于圖像的多樣化,現(xiàn)在權(quán)重變得很難辨認(rèn)光涂,我們可能會懷疑這些權(quán)重是否真的理解數(shù)字是怎么由線條組成的庞萍,或者模型只是記住了許多不同的像素。
plot_weights()
我們也可以打印并繪制出混淆矩陣忘闻,它讓我們看到誤分類的更多細(xì)節(jié)钝计。例如,它展示了描繪著數(shù)字5的圖像有時(shí)會被誤分類成其他可能的數(shù)字齐佳,但大多是3私恬,6或8。
print_confusion_matrix()
[[ 957 0 3 2 0 5 11 1 1 0]
[ 0 1108 2 2 1 2 4 2 14 0]
[ 4 9 914 19 15 5 13 14 35 4]
[ 1 0 16 928 0 28 2 14 13 8]
[ 1 1 3 2 939 0 10 2 6 18]
[ 10 3 3 33 10 784 17 6 19 7]
[ 8 3 3 2 11 14 915 1 1 0]
[ 3 9 21 9 7 1 0 959 2 17]
[ 8 8 8 38 11 40 14 18 825 4]
[ 11 7 1 13 75 13 1 39 4 845]]
現(xiàn)在我們用TensorFlow完成了任務(wù)炼吴,關(guān)閉session本鸣,釋放資源
# This has been commented out in case you want to modify and experiment
# with the Notebook without having to restart it.
session.close()
練習(xí)
下面是一些可能會讓你提升TensorFlow技能的一些建議練習(xí)。為了學(xué)習(xí)如何更合適地使用TensorFlow硅蹦,實(shí)踐經(jīng)驗(yàn)是很重要的荣德。
在你對這個(gè)Notebook進(jìn)行修改之前,可能需要先備份一下童芹。
- 改變優(yōu)化器的學(xué)習(xí)率涮瞻。
- 改變優(yōu)化器,比如用AdagradOptimizer 或 AdamOptimizer假褪。
- 將batch-size改為1或1000署咽。
- 這些改變?nèi)绾斡绊懶阅埽?/li>
- 你覺得這些改變對其他分類問題或數(shù)學(xué)模型有相同的影響嗎?
- 如果你不改變?nèi)魏螀?shù),多次運(yùn)行Notebook生音,會得到完成一樣的結(jié)果嗎宁否?為什么窒升?
- 改變plot_example_errors() 函數(shù),使它打印誤分類的 logits和y_pred值家淤。
- 用
sparse_softmax_cross_entropy_with_logits
代替softmax_cross_entropy_with_logits
异剥。這可能需要改變代碼的多個(gè)地方。探討使用這兩中方法的優(yōu)缺點(diǎn)絮重。 - 不看源碼,自己重寫程序歹苦。
*向朋友解釋程序如何工作青伤。