<jupyter_start><jupyter_text>3.6<jupyter_code>class CustomizedDenseLayer(keras.layers.Layer):
    def __init__(self, units, activation = None, **kwargs):
        self.units = units
        self.activation = keras.layers.Activation(activation)
        super(CustomizedDenseLayer, self).__init__(**kwargs)
        
    def build(self, input_shape):
        """构建layer所需的参数"""
        self.kernel = self.add_weight(name = 'kernel',
                                      shape = (input_shape[1], self.units),
                                      initializer = 'uniform',
                                      trainable = True)
        self.bias = self.add_weight(name = 'bias',
                                    shape = (self.units, ),
                                    initializer = 'zeros',
                                    trainable = True)
        super(CustomizedDenseLayer, self).build(input_shape)
        
    def call(self, x):
        """正向计算"""
        return self.activation(x @ self.kernel + self.bias)
    
    def get_config(self):
        base_config = super().get_config()
        return dict(list(base_config.items()))
    

def customized_mse(y_true, y_pred):
    return tf.reduce_mean(tf.square(y_pred - y_true))


model = keras.models.Sequential([
    CustomizedDenseLayer(30, activation='relu', input_shape = x_train.shape[1:]),
    CustomizedDenseLayer(1)
    ])


# 
model.compile(loss = customized_mse, optimizer = "adam", metrics=["mean_squared_error"])


# 回调函数试验
logdir = ".\callbacks"
if not os.path.exists(logdir):
    os.mkdir(logdir)

output_model_file = os.path.join(logdir, "fashion_mnist_model.h5")

# 回调函数
callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file, save_best_only=True),
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2),
]
model.summary()
history = model.fit(x_train_scaled, y_train, epochs=15, 
                    validation_data=(x_valid_scaled, y_valid),
                   callbacks=callbacks)<jupyter_output>Train on 11610 samples, validate on 3870 samples
Epoch 1/15
11610/11610 [==============================] - 1s 109us/sample - loss: 2.1400 - mean_squared_error: 2.1400 - val_loss: 0.6380 - val_mean_squared_error: 0.6380
Epoch 2/15
11610/11610 [==============================] - 1s 76us/sample - loss: 0.5319 - mean_squared_error: 0.5319 - val_loss: 0.4637 - val_mean_squared_error: 0.4637
Epoch 3/15
11610/11610 [==============================] - 1s 69us/sample - loss: 0.4384 - mean_squared_error: 0.4384 - val_loss: 0.4277 - val_mean_squared_error: 0.4277
Epoch 4/15
11610/11610 [==============================] - 1s 70us/sample - loss: 0.4191 - mean_squared_error: 0.4191 - val_loss: 0.4157 - val_mean_squared_error: 0.4157
Epoch 5/15
11610/11610 [==============================] - 1s 78us/sample - loss: 0.4084 - mean_squared_error: 0.4084 - val_loss: 0.4052 - val_mean_squared_error: 0.4052
Epoch 6/15
11610/11610 [==============================] - 1s 77us/sample - loss: 0.4008 - mean_squared_er[...]<jupyter_text>lambda<jupyter_code>customized_softplus = keras.layers.Lambda(lambda x : tf.nn.softplus(x))<jupyter_output><empty_output><jupyter_text>tf.function && Autograph<jupyter_code>def scaled_elu(z, scale = 1.0, alpha = 1.0):
    is_positive = tf.greater_equal(z, 0.0)
    return scale * tf.where(is_positive, z, alpha * tf.nn.elu(z))

print(scaled_elu(tf.constant(-3.)))
tf_scaled_elu = tf.function(scaled_elu)
tf_scaled_elu
aa = tf_scaled_elu.get_concrete_function(tf.TensorSpec([2], tf.float32))
aa<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17002237136465326, 'fraction_numerical': 0.06543624161073826, 'mean_word_length': 3.178738317757009, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '663749', 'license': 'Apache-2.0', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1233, 'n_words': 202, 'path': 'DeepLearning/LearningTensorFlow2.0/3.x.ipynb', 'repo_name': 'archu2020/python-2', 'size': '3576'}