上面一章我们已经了解了tensorflow2.0的基础了,接下来我们将用Tensorflow2.0来做一个模型,来看看一个基本的部分处理
1.还有TF的官方微信公众号的文章
2.参考连接2
TF部分
#数据
class MNISTLoader():
def __init__(self):
mnist = tf.keras.datasets.mnist
(self.train_data, self.train_label),(self.test_data,self.test_label) = mnist.load_data()
self.train_data = np.expand_dims((self.train_data.astype(np.float32)) / 255.0 , axis=-1)
self.test_data = np.expand_dims((self.test_data.astype(np.float32))/ 255.0 , axis=-1)
self.test_label = self.test_label.astype(np.int32)
self.train_label = self.train_label.astype(np.int32)
self.num_train_data,self.num_test_data = self.train_data.shape[0], self.test_data.shape[0]
def get_batch(self,batch_size):
# 从数据集中随机抽取batch_size个元素返回
index = np.random.randint(0, np.shape(self.train_data)[0], batch_size)
return self.train_data[index,:] ,self.train_label[index]
这一部分主要是用了numpy对数据进行初始化,numpy也是一个很好的东西,基本上很多框架的底层可能就是他了。
#模型部分
class MLP(tf.keras.Model):
def __init__(self):
super().__init__()
self.flatten = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(units=100,activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(units=10)
def call(self,inputs):
x = self.flatten(inputs)
x = self.dense1(x)
x = self.dense2(x)
outputs = tf.nn.softmax(x)
return outputs
模型部分,首先是继承来自与tf.keras.model,还有就是用super可以初始化构造方法,然后就是用了call使用刚刚定义的方法们。下面就是使用上面的东西,然后进行训练与测试等等。。
num_epochs = 5
batch_size = 50
learning_rate =0.001
model = MLP()
data_loader = MNISTLoader()
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
num_batches = int(data_loader.num_train_data // batch_size * num_epochs)
for batch_index in range(num_batches):
X,y = data_loader.get_batch(batch_size)
with tf.GradientTape() as tape:
y_pred = model(X)
loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y,y_pred=y_pred)
loss = tf.reduce_mean(loss)
print("batch %d: loss %.2f" %(batch_index,loss.numpy()))
grads = tape.gradient(loss,model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads,model.variables))
Keras部分
上面是tensorflow目前的训练,可以看到好像没有session这些东西了,下面是基于TF的Keras学习
#更简单的模型构建:
keras_model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(100, activation=tf.nn.relu),
tf.keras.layers.Dense(10),
tf.keras.layers.Softmax()
])
inputs = tf.keras.Input(shape=(28,28,1))
outputs = keras_model(inputs)
model = tf.keras.Model(inputs=inputs,outputs=outputs)
#模型训练的部署
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), #优化器
loss=tf.keras.losses.sparse_categorical_crossentropy, #损失函数
metrics=[tf.keras.metrics.sparse_categorical_accuracy] #模型指标
)
#开始训练你的模型
model.fit(data_loader.train_data, data_loader.train_label, epochs=num_epochs, batch_size=batch_size)
print(model.evaluate(data_loader.test_data, data_loader.test_label))
简单粗暴就是目前大家看到的keras了。
自定义部分
自己定义一个全连接层吧!
class Denseme(tf.keras.layers.Layer):
def __init__(self,units,activation):
super().__init__()
self.units = units
self.activation = activation
def build(self, input_shape):
self.kernel = self.add_weight(name='kernel',
shape=[input_shape[-1], self.units],
initializer='uniform',
trainable=True)
# #self.b = self.add_weight(name='b',
# shape=[input_shape[-1], self.units],
# initializer='uniform',
# trainable=True)
def call(self, inputs):
y_pred = tf.matmul(inputs,self.kernel) #+ self.b
y_pred = self.activation(y_pred)
return y_pred
然后自己调用就好了,主要是这个add_weight这里需要好好写一下!挺好的,嘻嘻
class CNN(tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(
filters=32,
kernel_size=[5,5],
padding='same',
activation=tf.nn.relu
)
self.pool1 = tf.keras.layers.MaxPool2D(
pool_size=[2,2],
strides=2
)
self.conv2 = tf.keras.layers.Conv2D(
filters=64,
kernel_size=[5,5],
padding='same',
activation=tf.nn.relu
)
self.pool2 = tf.keras.layers.MaxPool2D(
pool_size=[2,2],
strides=2
)
self.flatten = tf.keras.layers.Reshape(target_shape=(7*7*64,))
self.flatten1 = tf.keras.layers.Flatten()
self.denseme1 = Denseme(units=1024,activation=tf.nn.relu)
self.denseme2 = tf.keras.layers.Dense(units=10)
def call(self,inputs):
x = self.conv1(inputs)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = self.flatten1(x)
x = self.denseme1(x)
x = self.denseme2(x)
outputs = tf.nn.softmax(x)
return outputs