<jupyter_start><jupyter_text>The first step is to match names and data-types of the CSV columns<jupyter_code>inputs = {}

for name, column in titanic_features.items():
  dtype = column.dtype
  if dtype == object:
    dtype = tf.string
  else:
    dtype = tf.float32
  
  inputs[name] = tf.keras.Input(shape=(1,), name=name, dtype=dtype)

inputs
numeric_inputs = {name:input for name,input in inputs.items()
                  if input.dtype==tf.float32}
x = keras.layers.Concatenate()(list(numeric_inputs.values()))
norm = keras.layers.experimental.preprocessing.Normalization()
norm.adapt(np.array(titanic[numeric_inputs.keys()]))
all_numeric_inputs = norm(x)

all_numeric_inputs
preprocessed_inputs = [all_numeric_inputs]<jupyter_output><empty_output><jupyter_text>Next convert all string into numbers<jupyter_code>for name, input in inputs.items():
  if input.dtype == tf.float32:
    continue
  lookup = keras.layers.experimental.preprocessing.StringLookup(
      vocabulary=np.unique(titanic_features[name])
  )
  one_hot = keras.layers.experimental.preprocessing.CategoryEncoding(
      max_tokens=lookup.vocab_size()
  )

  x = lookup(input)
  x = one_hot(x)
  preprocessed_inputs.append(x)
preprocessed_inputs_cat = keras.layers.Concatenate()(preprocessed_inputs)
titanic_preprocessing = tf.keras.Model(inputs, preprocessed_inputs_cat)
keras.utils.plot_model(model=titanic_preprocessing, rankdir="LR", dpi=72, 
                       show_shapes=True)
# Convert into np.array
titanic_features_dict = {name: np.array(value) 
                         for name, value in titanic_features.items()}
features_dict = {name:values[:1] for name, values in titanic_features_dict.items()}
titanic_preprocessing(features_dict)
def titanic_model(preprocessing_head, inputs):
  body = tf.keras.Sequential([
    keras.layers.Dense(64),
    keras.layers.Dense(1)
  ])

  preprocessed_inputs = preprocessing_head(inputs)
  result = body(preprocessed_inputs)
  model = keras.Model(inputs, result)

  model.compile(loss=tf.losses.BinaryCrossentropy(from_logits=True),
                optimizer=tf.optimizers.Adam())
  
  return model

titanic_model = titanic_model(titanic_preprocessing, inputs)
titanic_model.fit(x=titanic_features_dict, y=titanic_labels, epochs=10)<jupyter_output>Epoch 1/10
20/20 [==============================] - 0s 1ms/step - loss: 0.5685
Epoch 2/10
20/20 [==============================] - 0s 1ms/step - loss: 0.5017
Epoch 3/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4676
Epoch 4/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4495
Epoch 5/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4358
Epoch 6/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4303
Epoch 7/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4251
Epoch 8/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4225
Epoch 9/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4212
Epoch 10/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4207
{'fraction_non_alphanumeric': 0.20597686375321336, 'fraction_numerical': 0.05012853470437018, 'mean_word_length': 4.267343485617597, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '613522', 'license': 'Apache-2.0', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1043, 'n_words': 196, 'path': 'Important-Notes/Loading Data/loading_data_titanic.ipynb', 'repo_name': 'Ace5584/Machine-Learning-Notes', 'size': '3112'}