TensorFlow读书报告

到目前为止,Keras已经与TensorFlow完全整合。Keras团队不再更新或维护Keras的独立版本。所以现在所讨论的Keras,是一个集成在Tensor Flow中的API,而不是一个单独的独立库。

TensorFlow 用图来表示计算任务,图中的节点被称之为operation,缩写成op。
一个节点获得 0 个或者多个张量tensor,执行计算,产生0个或多个张量。
图必须在会话(Session)里被启动,会话(Session)将图的op分发到CPU或GPU之类的设备上,同时提供执行op的方法,这些方法执行后,将产生的张量(tensor)返回。

变量 Variable

上面用到的张量是常值张量(constant)。

变量 Variable,是维护图执行过程中的状态信息的. 需要它来保持和更新参数值,是需要动态调整的。

下面代码中有 tf.initialize_all_variables,是预先对变量初始化, Tensorflow
的变量必须先初始化,然后才有值!而常值张量是不需要的。

  • 使用图(graph)来表示计算任务;
  • 在被称之为会话(Session)的上下文(context)中执行图;
  • 使用tensor(张量)表示数据;
  • 通过变量(Variable)维护状态;
  • 使用feed和fetch可以为任意的操作(arbitrary operation)赋值或者从其中获取数据。
  • TensorFlow是一个编程系统,使用图来表示计算任务。图中的节点被称作op(Operation),op可以获得0个或多个tensor,产生0个或多个tensor。每个tensor是一个类型化的多维数组。例如:可以将一组图像集表示成一个四维的浮点数组,四个维度分别是[batch, height, weight, channels]
  • 图(graph)描述了计算的过程。为了进行计算,图必须在会话中启动,会话负责将图中的op分发到CPU或GPU上进行计算,然后将产生的tensor返回。在Python中,tensor就是numpy.ndarray对象。

 

代码:

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

train_images.shape

len(train_labels)

train_labels

len(test_labels)

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

train_images = train_images / 255.0

test_images = test_images / 255.0

plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])

model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
predictions[0]

np.argmax(predictions[0])
test_labels[0]

def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array, true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])

plt.imshow(img, cmap=plt.cm.binary)

predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'

plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)

def plot_value_array(i, predictions_array, true_label):
predictions_array, true_label = predictions_array, true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)

thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')

i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()

# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()

# Grab an image from the test dataset.
img = test_images[1]

print(img.shape)

# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))

print(img.shape)

predictions_single = probability_model.predict(img)

print(predictions_single)

plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

np.argmax(predictions_single[0])

 

 

 

第六章习题:

全连接:层间神经元完全连接,每个输出神经元可以获取到所有输入神经元的信息,有利于信息汇总,常置于网络末层;连接与连接之间独立参数,大量的连接大大增加模型的参数规模。

局部连接:层间神经元只有局部范围内的连接,在这个范围内采用全连接的方式,超过这个范围的神经元则没有连接;连接与连接之间独立参数,相比于全连接减少了感受域外的连接,有效减少参数规模

区别是否需要大量参数

卷积计算:

卷积的输入输出理解:
        输入单通道图片层时的理解:对于输入图片为32X32的图片,卷积核大小为5X5,卷积核个数为6,步幅( Stride)为1,边界扩充( Padding)为0,公式为: (Input_H + 2*Padding - 卷积核H)/ Stride +1 ,(宽度公式同理) 这里(32+2*0-5)/1 + 1 = 28 ,所以输出的featrue map 大小为28X28 , 总共6个feature map,代表6种特征。

 

        输入是多通道图片时(多feature map)的理解:下图展示了在四个通道上的卷积操作,有两个卷积核,生成两个通道。其中需要注意的是,四个通道上每个通道对应一个卷积核,先将w2忽略,只看w1,那么在w1的某位置(i,j)处的值,是由四个通道上(i,j)处的卷积结果相加,最后得到两个feature map, 即输出层的卷积核核个数为 feature map 的个数。
        下图参数个数:4×2×2×2 = 32个参数,其中4表示4个通道,第一个2表示生成2个通道,最后的2×2表示卷积核大小。

 


池化的目的:对输入的特征图进行压缩,一方面使特征图变小,简化网络计算复杂度;一方面进行特征压缩,提取主要特征。        

激活函数的作用:如果不用激励函数,每一层输出都是上层输入的线性函数,无论神经网络有多少层,输出都是输入的线性组合。
如果使用的话,激活函数给神经元引入了非线性因素,使得神经网络可以任意逼近任何非线性函数,这样神经网络就可以应用到众多的非线性模型中。

局部相应归一化的作用:

局部归一化的动机:在神经生物学有一个概念叫做侧抑制(lateral inhibitio),指的是被激活的神经元抑制相邻神经元。归一化的目的是“抑制”,局部响应归一化就是借鉴侧抑制的思想来实现局部控制,尤其当我们使用RELU的时候这种”侧抑制“很管用。

也就是说你可以理解为对一个位置的值进行变形,变形的结果是该位置的值占邻域内的全部值的一个抽象的比重。

好处:有利于增加泛化能力,做了平滑处理,识别率提高1-2%。LRN层模仿生物神经系统的侧抑制机制,对局部神经元的活动创建竞争机制,使得响应比较大的值相对更大,提高模型泛化能力。Hinton在Imagenet中表明分别提升1.4%和1.2%。

posted @ 2022-04-25 17:33  tan45  阅读(29)  评论(0)    收藏  举报