<jupyter_start><jupyter_text><jupyter_code>!pip install -q keras
import keras
from keras.datasets import boston_housing
(train_data,train_targets),(test_data,test_targets) = boston_housing.load_data()
mean = train_data.mean(axis = 0)
train_data -= mean
std = train_data.std(axis = 0)
train_data /= std
test_data -= mean
test_data /= std
from keras import models
from keras import layers
train_data.shape[1]
def build_models():
  model = models.Sequential()
  model.add(layers.Dense(64,activation='relu',input_shape = (train_data.shape[1],)))
  model.add(layers.Dense(64,activation='relu'))
  model.add(layers.Dense(1))
  model.compile(optimizer = 'rmsprop',loss = 'mse',metrics =['mae'])
  return model
# Note that you compile the network with the mse loss function—mean squared error, the square of the difference between the predictions and the targets. This is a widely used loss function for regression problems.
# You’re also monitoring a new metric during training: mean absolute error (MAE). It’s the absolute value of the difference between the predictions and the targets. For instance, an MAE of 0.5 on this problem would mean your predictions are off by $500 on average.

# Validating your approach using K-fold validation
import numpy as np
k = 4

num_val_samples = len(train_data) //k 
num_epochs = 100
all_scores = []

for i in range(k):
  print('processing fold # ', i)
  val_data = train_data[i * num_val_samples:(i+1) * num_val_samples]
  val_targets = train_targets[i * num_val_samples:(i+1) * num_val_samples]
  

  partial_train_data = np.concatenate([train_data[:i * num_val_samples],
train_data[(i + 1) * num_val_samples:]], axis=0)
  partial_train_targets = np.concatenate( [train_targets[:i * num_val_samples],
train_targets[(i + 1) * num_val_samples:]], axis=0)
  
  model = build_models()
  model.fit(partial_train_data,partial_train_targets,epochs = num_epochs,batch_size=1, verbose=0) 
  val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
  all_scores.append(val_mae)

np.mean(all_scores)
num_epochs = 500
all_mae_histories = []
for i in range(k):
  print('processing fold # ', i)
  val_data = train_data[i * num_val_samples:(i+1) * num_val_samples]
  val_targets = train_targets[i * num_val_samples:(i+1) * num_val_samples]
  

  partial_train_data = np.concatenate([train_data[:i * num_val_samples],
train_data[(i + 1) * num_val_samples:]], axis=0)
  partial_train_targets = np.concatenate( [train_targets[:i * num_val_samples],
train_targets[(i + 1) * num_val_samples:]], axis=0)
  
  model = build_models()
  model.fit(partial_train_data,partial_train_targets,epochs = num_epochs,batch_size=1, verbose=0) 
  val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
  all_mae_histories.append(val_mae)

np.mean(all_mae_histories)
help(history)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11475988700564972, 'fraction_numerical': 0.013771186440677966, 'mean_word_length': 5.995061728395061, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '426710', 'license': 'MIT', 'n_tokens_mistral': 1051, 'n_tokens_neox': 998, 'n_words': 233, 'path': 'Boston_Housing_Price_Data.ipynb', 'repo_name': 'rupakgoyal/deep-learning', 'size': '2832'}