Keras of tensorflow advanced API

Keras is an open source neural network computing library mainly developed by the Python language.

In the TensorFlow 2 version, Keras was officially identified as the only high-level API interface for TensorFlow, replacing the high-level interfaces such as tf.layers that came with the TensorFlow 1 version. In other words, now only the Keras interface can be used to complete the TensorFlow layer model building and training. In TensorFlow, Keras is implemented in the tf.keras submodule.

In fact, Keras can be understood as a set of high-level API protocols for building and training neural networks. Keras itself has implemented this protocol. It can easily call TensorFlow, CNTK and other backends to complete accelerated calculations; in TensorFlow, a set of Keras protocols is also implemented , Which is tf.keras, but it can only be calculated based on TensorFlow backend, and has better support for TensorFlow. For developers who use TensorFlow, tf.keras can be understood as an ordinary submodule, which is no different from other submodules, such as tf.math, tf.data, etc.

1. Simple application

import tensorflow as tf
from tensorflow import keras
import numpy as np
model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
model.compile(optimizer = 'sgd',loss='mean_squared_error')
xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)
model.fit(xs,ys,epochs=500)
print(model.predict([10.0]))

2. Identify the picture

#下载fashion_mnist数据集
fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
#数据归一化
train_images  = train_images / 255.0
test_images = test_images / 255.0
#定义网络结构
#三层
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(), 
                                    tf.keras.layers.Dense(128, activation=tf.nn.relu), 
                                    tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
#
model.compile(optimizer = tf.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])
#预测
model.fit(train_images, train_labels, epochs=10)

callback

import tensorflow as tf
print(tf.__version__)

class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={
    
    }):
    if(logs.get('loss')<0.4):
      print("\nReached 60% accuracy so cancelling training!")
      self.model.stop_training = True

callbacks = myCallback()
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images=training_images/255.0
test_images=test_images/255.0
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
model.fit(training_images, training_labels, epochs=5, callbacks=[callbacks])

CNN

import tensorflow as tf
print(tf.__version__)
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images/255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(training_images, training_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)

—>

Epoch 1/10
1875/1875 [==============================] - 25s 13ms/step - loss: 0.1494 - accuracy: 0.9551
Epoch 2/10
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0502 - accuracy: 0.9842
Epoch 3/10
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0322 - accuracy: 0.9901
Epoch 4/10
1875/1875 [==============================] - 25s 13ms/step - loss: 0.0223 - accuracy: 0.9925
Epoch 5/10
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0146 - accuracy: 0.9953
Epoch 6/10
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0100 - accuracy: 0.9973
Epoch 7/10
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0081 - accuracy: 0.9972
Epoch 8/10
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0063 - accuracy: 0.9980
Epoch 9/10
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0050 - accuracy: 0.9983
Epoch 10/10
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0046 - accuracy: 0.9984
313/313 [==============================] - 1s 5ms/step - loss: 0.0591 - accuracy: 0.9876
0.9876000285148621

Guess you like

Origin blog.csdn.net/weixin_44127327/article/details/108996217