Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #@title Define the functions that build and train a model
- def build_model(my_learning_rate):
- """Create and compile a simple linear regression model."""
- # Most simple tf.keras models are sequential.
- # A sequential model contains one or more layers.
- model = tf.keras.models.Sequential()
- # Describe the topography of the model.
- # The topography of a simple linear regression model
- # is a single node in a single layer.
- model.add(tf.keras.layers.Dense(units=1,
- input_shape=(1,)))
- # Compile the model topography into code that
- # TensorFlow can efficiently execute. Configure
- # training to minimize the model's mean squared error.
- model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=my_learning_rate),
- loss="mean_squared_error",
- metrics=[tf.keras.metrics.RootMeanSquaredError()])
- return model
- def train_model(model, feature, label, epochs, batch_size):
- """Train the model by feeding it data."""
- # Feed the feature values and the label values to the
- # model. The model will train for the specified number
- # of epochs, gradually learning how the feature values
- # relate to the label values.
- history = model.fit(x=feature,
- y=label,
- batch_size=None,
- epochs=epochs)
- # Gather the trained model's weight and bias.
- trained_weight = model.get_weights()[0]
- trained_bias = model.get_weights()[1]
- # The list of epochs is stored separately from the
- # rest of history.
- epochs = history.epoch
- # Gather the history (a snapshot) of each epoch.
- hist = pd.DataFrame(history.history)
- # Specifically gather the model's root mean
- #squared error at each epoch.
- rmse = hist["root_mean_squared_error"]
- return trained_weight, trained_bias, epochs, rmse
- print("Defined create_model and train_model")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement