

apply_gradients ( grads_and_vars = zip ( grads, model. GradientTape () as tape : y_pred = model ( X ) # 调用模型 y_pred = model(X) 而不是显式写出 y_pred = a * X + b loss = tf.

SGD ( learning_rate = 0.01 ) for i in range ( 100 ): with tf. dense ( input ) return output # 以下代码结构与前节类似 model = Linear () optimizer = tf. zeros_initializer () ) def call ( self, input ): output = self. zeros_initializer (), bias_initializer = tf. Dense ( units = 1, activation = None, kernel_initializer = tf. In the inheritance class, we need to rewrite the _init_() (constructor) and call(input) (model call) methods, but we can also add custom methods as needed. Keras models are presented as classes, and we can define our own models by inheriting the Python class tf.keras.Model. Keras has built in a number of predefined layers commonly used in deep learning under tf.keras.layers, while also allowing us to customize the layers. The layers encapsulate various computational processes and variables (e.g., fully connected layers, convolutional layers, pooling layers, etc.), while the model connects the layers and encapsulates them as a whole, describing how the input data is passed through the layers and operations to get the output. There are two important concepts in Keras: Model and Layer. It is officially built-in and fully supported by TensorFlow.

In TensorFlow, it is recommended to build models using Keras ( tf.keras), a popular high-level neural network API that is simple, fast and flexible.
