You will build a regression model using the deep learning Keras library, and then you…
- User
You will build a regression model using the deep learning Keras library, and then you will experiment with increasing the number of training epochs and changing number of hidden layers and you will see how changing these parameters impacts the performance of the model. I will send the steps separately for you with each task until completion, you will provide code and explanation for each instruction.
The following code has already been loaded to download the dataset and split the data into predictors and target: [code block] concrete_data = pd.read_csv('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0101EN/labs/data/concrete\_data.csv') concrete_data_columns = concrete_data.columns
predictors = concrete_data[concrete_data_columns[concrete_data_columns != 'Strength']] # all columns except Strength target = concrete_data['Strength'] # Strength column [/code block]
The first task is listed below: A. Build a baseline model Use the Keras library to build a neural network with the following:
- One hidden layer of 10 nodes, and a ReLU activation function
- Use the adam optimizer and the mean squared error as the loss function.
- Randomly split the data into a training and test sets by holding 30% of the data for testing. You can use the train_test_splithelper function from Scikit-learn.
- Train the model on the training data using 50 epochs.
- Evaluate the model on the test data and compute the mean squared error between the predicted concrete strength and the actual concrete strength. You can use the mean_squared_error function from Scikit-learn.
- Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.
- Report the mean and the standard deviation of the mean squared errors.
- Assistant
A. Build a baseline model:
Here's the code to implement the steps mentioned in the task:
python - User
Great. Next task is listed below: B. Normalize the data
Repeat task A but use a normalized version of the data. Recall that one way to normalize the data is by subtracting the mean from the individual predictors and dividing by the standard deviation.
How does the mean of the mean squared errors compare to that from task A?
- Assistant
B. Normalize the data:
Here's the code to implement the task:
makefile# Normalize the data predictors_norm = (predictors - predictors.mean()) / predictors.std() # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train and evaluate the model mse_list = [] for i in range(50): # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train the model model = regression_model() model.fit(X_train, y_train, epochs=50, verbose=0) # Evaluate the model y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) mse_list.append(mse) # Report the mean and the standard deviation of the mean squared errors mse_array = np.array(mse_list) mean_mse = np.mean(mse_array) std_mse = np.std(mse_array) print("Mean of Mean Squared Errors: ", mean_mse) print("Standard Deviation of Mean Squared Errors: ", std_mse) - User
Great. Next task is listed below: C. Increate the number of epochs (5 marks) Repeat Part B but use 100 epochs this time for training. How does the mean of the mean squared errors compare to that from Step B?
- Assistant
C. Increase the number of epochs:
Here's the code to implement the task:
makefile# Normalize the data predictors_norm = (predictors - predictors.mean()) / predictors.std() # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train and evaluate the model mse_list = [] for i in range(50): # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train the model model = regression_model() model.fit(X_train, y_train, epochs=100, verbose=0) # Evaluate the model y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) mse_list.append(mse) # Report the mean and the standard deviation of the mean squared errors mse_array = np.array(mse_list) mean_mse = np.mean(mse_array) std_mse = np.std(mse_array) print("Mean of Mean Squared Errors: ", mean_mse) print("Standard Deviation of Mean Squared Errors: ", std_mse) - User
Great. The last task is listed below: D. Increase the number of hidden layers (5 marks) Repeat part B but use a neural network with the following instead:
- Three hidden layers, each of 10 nodes and ReLU activation function. How does the mean of the mean squared errors compare to that from Step B?
- Assistant
D. Increase the number of hidden layers:
Here's the code to implement the task:
makefile# Normalize the data predictors_norm = (predictors - predictors.mean()) / predictors.std() # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train and evaluate the model mse_list = [] for i in range(50): # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.3) # Train the model model = regression_model_3_layers() model.fit(X_train, y_train, epochs=50, verbose=0) # Evaluate the model y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) mse_list.append(mse) # Report the mean and the standard deviation of the mean squared errors mse_array = np.array(mse_list) mean_mse = np.mean(mse_array) std_mse = np.std(mse_array) print("Mean of Mean Squared Errors: ", mean_mse) print("Standard Deviation of Mean Squared Errors: ", std_mse) - User
what's the code for the function regression_model_3_layers()?
- Assistant
Here's the code for the function
regression_model_3_layers:cssfrom keras.models import Sequential from keras.layers import Dense def regression_model_3_layers(): model = Sequential() model.add(Dense(10, activation='relu', input_shape=(predictors_norm.shape[1],))) model.add(Dense(10, activation='relu')) model.add(Dense(10, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mean_squared_error') return modelThe function
regression_model_3_layerscreates a neural network with three hidden layers each of 10 nodes and ReLU activation function. The input shape is specified as(predictors_norm.shape[1],), which means it takes the number of columns in the predictors as the input shape. The model is compiled with theadamoptimizer and the mean squared error loss function.