<jupyter_start><jupyter_text>Read data using pandas<jupyter_code>import pandas as pd
import tensorflow as tf
from tensorflow import keras


SHUFFLE_BUFFER = 500
BATCH_SIZE = 2

csv_file = keras.utils.get_file(
    'heart.csv', 'https://storage.googleapis.com/download.tensorflow.org/data/heart.csv')
    

df: pd.DataFrame = pd.read_csv(csv_file)
df.head()
df.dtypes
target = df.pop('target')
numeric_feature_names = ['age', 'thalach', 'trestbps',  'chol', 'oldpeak']
numeric_features = df[numeric_feature_names]
numeric_features.head()
tf.convert_to_tensor(numeric_features, dtype=tf.float32)
normalizer = keras.layers.Normalization(axis=-1)
normalizer.adapt(numeric_features)
normalizer(numeric_features.loc[:3])
from keras.models import Sequential
from keras.layers import Dense

def get_basic_model():
    model = Sequential([
        normalizer,
        Dense(10, activation='relu'),
        Dense(10, activation='relu'),
        Dense(1),
    ])
    model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])
    return model
model = get_basic_model()
history = model.fit(numeric_features, target, epochs=15, batch_size=BATCH_SIZE)<jupyter_output>Epoch 1/15
152/152 [==============================] - 2s 6ms/step - loss: 0.6358 - accuracy: 0.7294
Epoch 2/15
152/152 [==============================] - 1s 6ms/step - loss: 0.5300 - accuracy: 0.7327
Epoch 3/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4806 - accuracy: 0.7426
Epoch 4/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4570 - accuracy: 0.7525
Epoch 5/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4430 - accuracy: 0.7624
Epoch 6/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4379 - accuracy: 0.7822
Epoch 7/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4335 - accuracy: 0.7855
Epoch 8/15
152/152 [==============================] - 1s 8ms/step - loss: 0.4295 - accuracy: 0.7954
Epoch 9/15
152/152 [==============================] - 1s 6ms/step - loss: 0.4263 - accuracy: 0.7855
Epoch 10/15
152/152 [==============================] - 1s 7ms/step - loss: 0.4238 - accuracy: 0.8053[...]<jupyter_text>With tf.data<jupyter_code>from tensorflow import data


numeric_dataset = data.Dataset.from_tensor_slices((numeric_features, target))

for row in numeric_dataset.take(3):
    print(row)
numeric_batches = numeric_dataset.shuffle(1000).batch(BATCH_SIZE).prefetch(1)

model = get_basic_model()
model.fit(numeric_batches, epochs=15)<jupyter_output>Epoch 1/15
152/152 [==============================] - 2s 8ms/step - loss: 0.7276 - accuracy: 0.6997
Epoch 2/15
152/152 [==============================] - 1s 7ms/step - loss: 0.6628 - accuracy: 0.7261
Epoch 3/15
152/152 [==============================] - 1s 8ms/step - loss: 0.5839 - accuracy: 0.7261
Epoch 4/15
152/152 [==============================] - 1s 7ms/step - loss: 0.5030 - accuracy: 0.7261
Epoch 5/15
152/152 [==============================] - 1s 7ms/step - loss: 0.4741 - accuracy: 0.7261
Epoch 6/15
152/152 [==============================] - 1s 8ms/step - loss: 0.4625 - accuracy: 0.7294
Epoch 7/15
152/152 [==============================] - 1s 7ms/step - loss: 0.4541 - accuracy: 0.7459
Epoch 8/15
152/152 [==============================] - 1s 8ms/step - loss: 0.4474 - accuracy: 0.7492
Epoch 9/15
152/152 [==============================] - 1s 9ms/step - loss: 0.4410 - accuracy: 0.7624
Epoch 10/15
152/152 [==============================] - 1s 7ms/step - loss: 0.4377 - accuracy: 0.7657[...]<jupyter_text>DataFrame with heterogeneous datause dataframe as dict<jupyter_code>numeric_dict_ds = tf.data.Dataset.from_tensor_slices((dict(numeric_features), target))

for row in numeric_dict_ds.take(3):
  print(row)<jupyter_output>({'age': <tf.Tensor: shape=(), dtype=int64, numpy=63>, 'thalach': <tf.Tensor: shape=(), dtype=int64, numpy=150>, 'trestbps': <tf.Tensor: shape=(), dtype=int64, numpy=145>, 'chol': <tf.Tensor: shape=(), dtype=int64, numpy=233>, 'oldpeak': <tf.Tensor: shape=(), dtype=float64, numpy=2.3>}, <tf.Tensor: shape=(), dtype=int64, numpy=0>)
({'age': <tf.Tensor: shape=(), dtype=int64, numpy=67>, 'thalach': <tf.Tensor: shape=(), dtype=int64, numpy=108>, 'trestbps': <tf.Tensor: shape=(), dtype=int64, numpy=160>, 'chol': <tf.Tensor: shape=(), dtype=int64, numpy=286>, 'oldpeak': <tf.Tensor: shape=(), dtype=float64, numpy=1.5>}, <tf.Tensor: shape=(), dtype=int64, numpy=1>)
({'age': <tf.Tensor: shape=(), dtype=int64, numpy=67>, 'thalach': <tf.Tensor: shape=(), dtype=int64, numpy=129>, 'trestbps': <tf.Tensor: shape=(), dtype=int64, numpy=120>, 'chol': <tf.Tensor: shape=(), dtype=int64, numpy=229>, 'oldpeak': <tf.Tensor: shape=(), dtype=float64, numpy=2.6>}, <tf.Tensor: shape=(), dtype=int64, numpy=0>)<jupyter_text>Dictionaries with Kerastwo ways to make Keras model that accepts a dictionary as input1. subclass tf.keras.Model2. Keras functional style<jupyter_code>def stack_dict(inputs, fun=tf.stack):
    values = []
    for key in sorted(inputs.keys()):
        values.append(tf.cast(inputs[key], tf.float32))
    
    return fun(values, axis=-1)

inputs = {}
for name, column in numeric_features.items():
    inputs[name] = keras.Input(shape=(1,), name=name, dtype=tf.float32)
inputs
x = stack_dict(inputs, fun=tf.concat)

normalizer = keras.layers.Normalization(axis=-1)
normalizer.adapt(stack_dict(dict(numeric_features)))

x = normalizer(x)
x = tf.keras.layers.Dense(10, activation='relu')(x)
x = tf.keras.layers.Dense(10, activation='relu')(x)
x = tf.keras.layers.Dense(1)(x)

model = tf.keras.Model(inputs, x)

model.compile(optimizer='adam', 
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'],
              run_eagerly=True)

tf.keras.utils.plot_model(model, rankdir='LR', show_shapes=True)
model.fit(dict(numeric_features), target, batch_size=BATCH_SIZE, epochs=5)
numeric_dict_batches = numeric_dict_ds.shuffle(SHUFFLE_BUFFER).batch(BATCH_SIZE).prefetch(2)
model.fit(numeric_dict_batches, epochs=5)<jupyter_output>Epoch 1/5
152/152 [==============================] - 5s 33ms/step - loss: 0.4496 - accuracy: 0.7624
Epoch 2/5
152/152 [==============================] - 5s 32ms/step - loss: 0.4421 - accuracy: 0.7690
Epoch 3/5
152/152 [==============================] - 5s 32ms/step - loss: 0.4364 - accuracy: 0.7921
Epoch 4/5
152/152 [==============================] - 6s 36ms/step - loss: 0.4324 - accuracy: 0.7888
Epoch 5/5
152/152 [==============================] - 5s 34ms/step - loss: 0.4289 - accuracy: 0.7855<jupyter_text>Full Example<jupyter_code>binary_feature_names = ['sex', 'fbs', 'exang']
categorical_feature_names = ['cp', 'restecg', 'slope', 'thal', 'ca']

inputs = {}
for name, column in df.items():
    if type(column[0]) == str:
        dtype = tf.string
    elif (name in categorical_feature_names or name in binary_feature_names):
        dtype = tf.int64
    else:
        dtype = tf.float32

    inputs[name] = tf.keras.Input(shape=(), name=name, dtype=dtype)
inputs<jupyter_output><empty_output><jupyter_text>make preprocessor<jupyter_code>preprocessed = []

for name in binary_feature_names:
    inp = inputs[name]
    inp = inp[:, tf.newaxis]
    float_value = tf.cast(inp, tf.float32)
    preprocessed.append(float_value)

normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(stack_dict(dict(numeric_features)))

numeric_inputs = {}
for name in numeric_feature_names:
    numeric_inputs[name] = inputs[name]

numeric_inputs = stack_dict(numeric_inputs)
numeric_normalized = normalizer(numeric_inputs)

preprocessed.append(numeric_normalized)

for name in categorical_feature_names:
    vocab = sorted(set(df[name]))
    if type(vocab[0]) is str:
        lookup = tf.keras.layers.StringLookup(vocabulary=vocab, output_mode='one_hot')
    else:
        lookup = tf.keras.layers.IntegerLookup(vocabulary=vocab, output_mode='one_hot')
    
    x = inputs[name][:, tf.newaxis]
    x = lookup(x)
    preprocessed.append(x)
preprocessed_result = tf.concat(preprocessed, axis=-1)
preprocessor = tf.keras.Model(inputs, preprocessed_result)
tf.keras.utils.plot_model(preprocessor, rankdir='LR', show_shapes=True)<jupyter_output><empty_output><jupyter_text>create and train a model<jupyter_code>body = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1),
])
x = preprocessor(inputs)
result = body(x)
model = tf.keras.Model(inputs, result)
model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(
    dict(df), target, epochs=5, batch_size=BATCH_SIZE
)
ds = tf.data.Dataset.from_tensor_slices((
    dict(df),
    target
))
ds = ds.batch(BATCH_SIZE)
history = model.fit(ds, epochs=5)<jupyter_output>Epoch 1/5
152/152 [==============================] - 5s 28ms/step - loss: 0.3228 - accuracy: 0.8086
Epoch 2/5
152/152 [==============================] - 4s 28ms/step - loss: 0.2976 - accuracy: 0.8383
Epoch 3/5
152/152 [==============================] - 5s 30ms/step - loss: 0.2814 - accuracy: 0.8515
Epoch 4/5
152/152 [==============================] - 5s 30ms/step - loss: 0.2696 - accuracy: 0.8581
Epoch 5/5
152/152 [==============================] - 4s 29ms/step - loss: 0.2600 - accuracy: 0.8779
{'fraction_non_alphanumeric': 0.23968962585034015, 'fraction_numerical': 0.08163265306122448, 'mean_word_length': 4.955063291139241, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '507554', 'license': 'MIT', 'n_tokens_mistral': 3889, 'n_tokens_neox': 3297, 'n_words': 668, 'path': 'tftutorials/loadPandas.ipynb', 'repo_name': 'bahalbach/nbaproject', 'size': '9408'}