<jupyter_start><jupyter_text>TENSORFLOW 2 LIBRARY    Model Building - Functional API  Configure Learning Environment<jupyter_code># !pip install git+https://github.com/nockchun/rspy --force
import rspy as rsp
rsp.setSystemWarning(off=True)
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import utils, layers, activations, models, losses, optimizers
np.set_printoptions(linewidth=200, precision=2)
print("버전: ", tf.__version__)
print("즉시 실행 모드: ", tf.executing_eagerly())
print("GPU: ", "사용 가능" if tf.test.is_gpu_available() else "사용 불가능")
print("GPU Device: ", tf.config.experimental.get_visible_devices("GPU"))<jupyter_output>버전:  2.4.1
즉시 실행 모드:  True
GPU:  사용 가능
GPU Device:  [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]<jupyter_text>Prepare Data<jupyter_code>iris = sns.load_dataset("iris")
iris.head(3)
iris.species.unique()
iris = iris[iris.species.isin(["setosa", "versicolor"])]
iris.replace({ "species": {"setosa": 0, "versicolor": 1} }, inplace=True)
iris.head()
iris.tail()
data = iris[["sepal_length", "sepal_width", "petal_length", "petal_width"]].values
label = iris[["species"]].values<jupyter_output><empty_output><jupyter_text>Generate Model<jupyter_code>inputs = tf.keras.Input((4,))

hl = layers.Dense(32, activation=activations.sigmoid)(inputs)
hl = layers.Dense(32, activation="relu")(hl)

outputs = layers.Dense(1, activation="sigmoid")(hl)
# inputs = tf.keras.Input((4,))

# hl1 = layers.Dense(32, activation=activations.sigmoid)(inputs)
# hl2 = layers.Dense(32, activation="relu")(inputs)
# reduce_avg = layers.average([hl1, hl2])

# outputs = layers.Dense(1, activation="sigmoid")(reduce_avg)
model = models.Model(inputs=inputs, outputs=outputs)
model.summary()
utils.plot_model(model, 'model.png', show_shapes=True)
model.compile(
    loss=losses.BinaryCrossentropy(),
    optimizer=optimizers.SGD()
)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>history = model.fit(
    data, label, epochs=100
)
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(history.history["loss"])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1608554160855416, 'fraction_numerical': 0.014411901441190145, 'mean_word_length': 7.5396825396825395, 'pattern_counts': {'":': 3, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '172117', 'license': 'Apache-2.0', 'n_tokens_mistral': 852, 'n_tokens_neox': 850, 'n_words': 142, 'path': 'lecture_source/library/TF2_modelling_02.ipynb', 'repo_name': 'nockchun/rsnet', 'size': '2151'}